Question

So after learning a little bit about programming to interface and some other inheritance related topic I have another question.

Databases are usually parent child tables. Many application have 2-4 levels for heirarchy, like referral, referralitem, referralbillingitem etc and some lookup tables. Now if I model this as classes I will have

Class Referral
{
List<ReferralLineItems> rli;

}
Class ReferrralLineItem
{
List<ReferralBillingLineItem> rbli;
}

Where can I apply Interface or abstract class in this case?

Thanks!

I have seen many senior architects creating Interface for IReferralBLC, and IReferralLineItemBLC etc to encapsulate business logic. These interfaces almost always have only one implementation like ReferralBLC, ReferralItemBLC etc, so why is interface needed?

Was it helpful?

Solution

As far as domain entities are concerned, the benefit of having them implement interfaces is very debatable. Some even see it as an anti-pattern.

The objects in your example share a couple of notable characteristics that should make you think twice before adding interfaces on top of them :

  • They are anemic, i.e. don't have any behavior. Interfaces are used for defining contracts, messaging protocols between objects. If there's no message to send, i.e. no behavior to use, they lose a large part of their interest.

  • They aren't likely to be injected as dependencies into other objects. You won't need to mock up the object in unit tests and thus the common parent abstraction for mock and real class, which is typically embodied in an interface, isn't really needed.

OTHER TIPS

They exist for the same reason they existed in the first place, loose coupling. You can easily change the implementation classes without modifying those interfaces (perhaps use a rule engine for the implementation), plus testing which is always important and also this describes the business in the language of the domain. On the other hand, the idea of Object Relational Mapping has been discussed and still under discussion in the computer science world because of the conceptual difference between the Object Model and the Relational Model which is referred to as the Impedance Mismatch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top