Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I put my interface definition in same namespace as its implementation

Tags:

c#

interface

If I define an interface ITestInterface and then immediately create a class that implements that interface for usage within an application is it ok to keep the class and interface in the same namespace or should they be seperate. i.e. Test.Interfaces and Test.Interfaces.Implementation.

Both my interface and its implementation will be in its own assembly so I'm not looking to create another one just to contain the interface itself.

This is particular related to c# however I guess it can cover any language.

like image 224
dreza Avatar asked Mar 20 '12 20:03

dreza


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.

What is Interface namespace?

Interface NamespaceAn interface that contains information about a namespace. Namespaces are accessed from a StartElement.

Can an interface provide implementation?

If a class fails to implement one or more abstract methods from the interface, the compiler will complain. A class may also implement multiple interfaces. Because an interface does not provide implementation for its methods, it cannot be instantiated.

Why would you ever use an interface?

You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following: Capturing similarities among unrelated classes without artificially forcing a class relationship.


2 Answers

It's probably better to use the established conventions of the .NET predefined classes. For example, looking in the System.Collections.Generic namespace we can see that both IDictionary and Dictionary are there. So probably putting them in the same namespace is the best idea.

Also, since both the interface and the implementation most likely serve the same purpose, it's better to group them in the same namespace.

like image 81
Tudor Avatar answered Oct 16 '22 05:10

Tudor


This is particular related to c# however I guess it can cover any language.

It's typical in Java to have the two in the same package. The one exception I can think of might be a DAO interface in a package persistence and different implementations in subpackages under that (e.g. jdbc, hibernate, jdo, etc.)

You can think of a public interface as being the API that's exposed from the package. I can see where implementation classes might be package private, preventing users from accessing the implementation via anything other than the interface. Public factory methods would have to be provided to grant access.

like image 33
duffymo Avatar answered Oct 16 '22 07:10

duffymo