views:

80

answers:

2

I was just looking into the source code of an existing project which uses nHibernate and found that there are interfaces created for each entity classes. E.g ICustomer for Customer class. I was just wondering what can be the advantage of this pattern as ICustomer contains mainly the properties and very few methods.

+9  A: 

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

I don't see a need for interfaces if your domain objects won't require different implementations. Only introduce them if dynamic proxy generation or aspects or changing implementations are necessary.

duffymo
+1 because it's too easy to use interfaces everywhere. Having said that, be ready to extract an interface if you need to use the 'very few methods' anywhere, or if the Customer object's constructor enforces any business rules. You don't want to have to create a valid concrete Customer instance just to unit test some other class.
Jeff Sternal
Interfaces are also notorious for versioning problems. If you don't specifically need to define an interface, don't.
Will
A: 

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

JoeGeeky