c# - Is it the best practice to extract an interface for every class? -


I have seen the code where each section has an interface that implements it.

Sometimes there is no common interface for them all.

They are just there and they are used instead of concrete objects.

They do not provide a common interface for the two classes and they are specific to the domain

No, it is not the best practice to remove the interface for every class

It may actually be reversed, however, the interfaces are useful for some reasons:

  • Test support (socks, stubs).
  • Implementation Intangible (forward on IoC / DI)
  • Collaborative things such as C- and Contra-Virus support in C #

To achieve these goals, the interface regards is a good practice (and in fact it is necessary for the last point). Depending on the size of the project, you will find that you do not have to talk to the interface at any time or you are continuously removing the interface for one of the reasons mentioned above.

We maintain a large application, some parts of it are great and some people suffer from lack of meditation. We often reuse ourselves to pull one type of interface so that it can be testable or we can change the implementation while reducing the effect of that change. We also do this to reduce the "coupling" effect which can be implemented by concrete type of error if you are not strict on your public API (interface can represent only public API so that it is naturally tough for us Ho).

He said, it is possible for intangible behavior without interface and it is possible to type type tests without the need for interface, so they are not required for the above It is only that those functions will work effectively against most frameworks / library interfaces used to assist you.


I will leave my old answer for reference.

The interface defines a public contract that people implementing the interface want to implement this agreement. Consumers see only public agreements, it means that the details of the implementation are leveled from the consumer.

Instant Access Unit Testing for these days. The interface is fake, stub, easy to fake, you give it its name.

Another immediate use is dependency injection . A registered concrete type for an interface is provided for using one type of interface. The type does not particularly care about implementation, so it can clearly ask for the interface. This allows you to change the implementation without affecting many codes (the impact area is very small, as long as the contract remains the same).

I am not troubled for very small projects, because I have medium projects to worry about important key items, and for big projects there is an interface for almost every class. It is almost always to support the test, but in some cases of injection, or the intuitive behavior of reducing code repeat behavior.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -