I'm updating a system that has eleven tables that represent a single entity, I believe by implementation error, as shown below:
Entidade: Foo
Tabela Foo1 - Campo1, Campo2, Campo3
Tabela Foo2 - Campo1, Campo2, Campo3
Tabela Foo3 - Campo1, Campo2, Campo3, Campo4
[...]
Because of this implementation, there are eleven entities in the system that inherit from Foo, with their respective repositories and services. This implementation has had a direct impact on the code, which has n switchs
that defines which entity should be used. The cases
has the same code, except for the repositories and services used.
Database and entity refactoring is currently not an option.
Given this scenario, what would be the best approach to deal with this situation? The code so far should not be changed, but there is a great effort to create new functionalities that depend on this architecture of N tables and N entities that should represent only one. Is there any design pattern that can make this work easier?
The project architecture is in Onion with MVC Presentation Layer. The MVC layer accesses the services that in turn encapsulate the system business rule. A common problem with this implementation is that whenever something involves these entities and tables, a switch needs to be done to define which service will be called to communicate with the database.
public class FooService{
private service1;
private service2;
//demais services das entidades
//construtor
public void Inserir(Entidade foo)
{
switch(bar.TipoItem)
{
case(TipoItemEnum.Tabela1):
service1.RealizarAlgumProcesso(foo);
break;
case(TipoItemEnum.Tabela2):
service2.RealizarAlgumProcesso(foo);
break;
//demais cases para os demais services.
}
}
}