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.
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.
Interface NamespaceAn interface that contains information about a namespace. Namespaces are accessed from a StartElement.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With