0

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Good to know about different perpective
1

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.

1 Comment

@MeProgramming you're welcome, but i have to say i agree with guillaume31 as that is what i do for domain entities (i don't use interfaces for them). My answer was mainly aimed to explain Why some architects do (and not because i do it). That doesn't mean you should never do it, there are some useful situations in which they make sense, always use the best tool for the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.