Paul D'Ambra
Paul D'Ambra

Reputation: 7824

dotNet should a class library only expose an interface or an implementation too

I'm teaching myself as I go and am the only developer at our organisation. I've started splitting particular tasks off into class libraries so that as organisation needs grow I can re-use that code.

I tend to have for e.g. IMailingThingie and MailingThingie in the class library since when I add a reference to the library both resolve.

I also feel like this reduces duplication of code but has led to some hoop jumping (for example to accommodate daily MailingThingie rules and Monthly MailingThingie rules)

Is it better practice to expose only the interface in the class library and code the implementations on a project by project basis?

I'm using C# dotNet but I guess the architectural decision at this level is language neutral...

SolutionRoot1
--ClassLibrary
----IMailing
----Mailing
--Project1
----Reference
--Project2
----Reference

or

SolutionRoot1
--ClassLibrary
----IMailing
--Project1
----Reference
----Mailing
--Project2
----Reference
----Mailing

Upvotes: 0

Views: 770

Answers (1)

Dave Van den Eynde
Dave Van den Eynde

Reputation: 17415

It's better to share implementation if that leads to code re-use. If you're jumping through hoops because of differing business rules, you should look at inheritance to allow you to share the basics and allow each project to define the specifics.

The best practise is to understand the difference between sharing an interface and sharing an implementation so you can choose the best solution given the problem.

Upvotes: 1

Related Questions