Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should interfaces be in a separate project from their implementation?

My question isn't so much about use of interfaces but more of a project organization nature.

Note: I am using VisualStudio in a multi-layered application.

Should my Interface files live in a separate project from their implementations? My initial thought is that it would be useful to separate out all my service interfaces into their own project (and a project for my initial implementations) so that down the road the implementation/concrete project may be removed and replaced with a new one if necessary.

To clarify with an example: Suppose I have a business layer Interface called IBusinessService which lives in the MyApp.Business.Services namespace. My implementation FooBusinessService would exist in the same namespace, but a different project in VisualStudio. If later on the implementation needed to be reworked, a developer could remove the reference to the FooService.proj and replace it with a reference to BarService.proj.

This seems like it would declutter the app solution by allowing you to reference a project with only interfaces without also acquiring concrete implementations (which may be obsolete or of no use to you), but am I missing something?

like image 821
Ben Elder Avatar asked Oct 28 '09 12:10

Ben Elder


People also ask

Should interfaces be in separate project?

Because interfaces can be implemented by multiple components, it's good practice to put them in a separate assembly from that of the implementing components.

Why do we separate interface from implementation?

Defines an interface in a separate package from its implementation. As you develop a system, you can improve the quality of its design by reducing the coupling between the system's parts. A good way to do this is to group the classes into packages and control the dependencies between them.

Do I need to use an interface when only one class will ever implement it?

Interfaces can be implemented by multiple classes. There is no rule that only one class can implement these. Interfaces provide abstraction to the java.

Can an interface have multiple implementations?

Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.


1 Answers

I'm with you. I prefer to put my interfaces in a separate project AND in a different namespace. The classic example is with data access classes. You want to be able to code an MSSQL version and a MySQL version, both implementing the same interface. As such, I prefer that the interface definition be in a separate assembly/project. Here's an example of how I lay out assemblies and namespaces:

  • Elder.DataAccess.Core - contains the interfaces and common utilities
  • Elder.DataAccess.MSSQL - specific MSSQL implementations of the interfaces
  • Elder.DataAccess.MySQL - specific MySQL implementations of the interfaces

This allows me to modify the implementations without touching the project that contains the interface definitions. This helps me with version control and change tracking, too. There might be other ways to skin this cat, so I'll be eager to see other folks' answers.

like image 74
Scott Fletcher Avatar answered Nov 16 '22 01:11

Scott Fletcher