Base class for several classes in different projects

It is rather a design issue.

Currently, I have several classes (in different projects) that have different roles, but there is one common method that performs the same function. I was thinking of a base class for all abstract classes, so each of them could inherit this class and implement this method to maintain duplication.

My question is: should I have one base class for all classes that are in several projects, or should I have one base class for each project?

Thanks,

+5
source share
2 answers

I would define an interface containing this generic method. Then I would include this interface in my own project. Then, your specific implementations refer to this project and implement the interface.

Something like that..

  • Repositories.proj
    • IRepository
  • EntityFrameworkImpl.proj
    • Repository: IRepository
  • NHibernateImpl.proj
    • Repository: IRepository
+3
source

In general, 1 class for all your projects is much better than creating one class for each project.

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

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

EDIT:

If you work in visual studio, I would solve it as follows. Create a project containing the declaration and definition of the base class. These projects must be a type of static library.

Other projects should include a directory containing the header file in dirs attachments. And the directory containing the lib file in the additional dependency directories.

+3
source

All Articles