Is it a good idea to create an interface for each domain object?

I was just researching the source code of an existing project that uses nHibernate and found that there are interfaces for every entity class. For example, ICustomer for the Customer class . I'm just wondering what might be the benefit of this model, since ICustomer contains mostly properties and very few methods.

+2


a source to share


2 answers


I would say no. Interfaces separate behavior from implementation so that the latter can be changed without affecting the clients of the interface.



I see no need for interfaces unless your domain objects require different implementations. Enter them only if dynamic proxy generation or aspects or changing implementations are needed.

+9


a source


I disagree with previous comments ... Although when combined with patterns like Inversion of Control (IoC) and Dependency Injection, it makes it much easier to isolate such layers from each other. This simplifies Unit Testing, Mocking, and can also lead to more loosely coupled architectures. This can be achieved without interfaces, although you need to make sure that you don't seal your classes and make the members virtual so you can still mock, generate intercepted proxies, etc. Finally, using interfaces forces you to abandon the assumptions associated with any one particular implementation and instead focus on the contract represented by the interface definition.



+1


a source







All Articles