CodersSC
CodersSC

Reputation: 710

Base Class for multiple classes in different projects

This is more of a design question.

Currently I have multiple classes (in different projects) which have different roles but there is one common method which performs the same duty. I was thinking about having a base class for all theses classes so each of them could inherit this class and implement this method to save duplication.

My question is should I have one base class for all the classes which are in multiple projects, or should I have one base class per project?

Thanks,

Upvotes: 5

Views: 1154

Answers (2)

laurisvr
laurisvr

Reputation: 2822

In general 1 class for all your projects is a lot better than creating the same class for each project.

Actually I dare to say that in almost any case, reusing the same code, rather than copying it. Always is better

In general static libraries are the best tool to solve the problem you are currently facing.

EDIT:

If you are working in visual studio i would solve this as follows. Create a project containing the base class declaration and definition. This projects should be of the type static library.

The other projects should include the directory containing the header file in inclusion dirs. And the directory containing the lib file in the additional dependency directories.

Upvotes: 3

Alex
Alex

Reputation: 842

I would define an interface that contains that one shared method. I would then put that interface in it's own project. Then have you concrete implementations reference that project and implement the interface.

Something like this..

  • Repositories.proj
    • IRepository
  • EntityFrameworkImpl.proj
    • Repository : IRepository
  • NHibernateImpl.proj
    • Repository : IRepository

Upvotes: 3

Related Questions