Interfaces in class files

Should my interface and concrete implementation of that interface be split into two separate files?

+1


source to share


7 replies


If you want other classes to implement this interface, that would probably be a good idea, if only for cleanliness. Anyone looking at your interface doesn't have to look at its implementation every time.



+10


source


If there is only one implementation: why an interface?



If there is more than one implementation: where do you put the rest?

+3


source


If in different files you mean different xxx.cs files in your assembly, then usually due to my own practices I would say yes, but it depends on the house standards you are using. If you are just programming for yourself, then I would say that this is a good coding practice, it keeps everything clean and easy to read. The fewer blocks of code in any given file, the easier something is to follow (within reason), obviously you can start getting partial classes where things can start laughing if you don't dominate it.

Typically, I save my projects in a logical folder structure, where parts of the project can be separated into DAL or BM folders, and there I can have multiple logically named folders, each containing multiple files: one interface, one implementation, and any helper classes specific to them.

However, with all that said, your team / own best practice should be accepted if you work on a development team.

+1


source


Individual files ... FTW! You might want to create separate projects / assemblies depending on how much you extend your code. At least it should probably be in a separate namespace.

The whole point of an interface is that the code that uses the interface doesn't care about the implementation. Therefore, they should be as closely related as possible that they will not be there if they are in the same file.

But as @balabaster points out, it depends on what your team is practicing (although these are not always "best practices").

0


source


Yes, for the classes they are named partial class

, see the link text

0


source


The general rule is yes. An interface means it can be implemented by other classes and is easier and easier to manage when they are explicitly in separate files.

What's more, depending on the level of separation and isolation of your application, you might even want to host your interfaces in your own project . The consuming projects will then reference the interface project, rather than every assembly that implements implementations of that interface.

0


source


Yes, even if you give counter arguments such as only one implementation, or he / she stipulates that there will only be one implementation for a long time, or he / she is the only user / developer, etc. If there are multiple implementations, multiple users, etc., then obviously you want to keep them in separate files. So why should you treat it differently in just one implementation?

0


source







All Articles