7
I am updating a system that has eleven tables that represent a single entity, I believe by implementation error, as an example 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 n switchs
defining which entity should be used. The cases
have the same code, with the exception of the repositories and services used.
Database and entity refactor is not currently an option.
Against this background, what would be the best approach to deal with this situation? The code made 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 Pattern design that can facilitate this work?
The project architecture is in Onion with MVC presentation layer. The MVC layer accesses the services that in turn encapsulate the system’s business rule. A common problem of this implementation is that whenever something involves these entities and tables, a switch needs to be made 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.
}
}
}
Which version of . Net is the project?
– Malkaviano
At first glance, it seems to me a case for a Factory, could give us more information?
– Intruso
is using MVC 4. @Intruder, could specify what information you would like to know?
– Vinícius
All Services also implement the same interface?
– Tobias Mesquita
Each service implements a different interface, @Tobymosque
– Vinícius
I advise you to make this fact very explicit in your question, and give different names to the method of each service, I believe that others have had this same intention, because the answers are following this line of reasoning.
– Tobias Mesquita
services implement a common interface?
– Intruso
As reported to @Tobymosque, services implement different interfaces.
– Vinícius
Are these services in a local library or are they web services (eg: WCF)? Is it possible for all services to implement a.common interface? Or are they really very different procedures for each entity?
– Tobias Mesquita
@Tobymosque, the procedures are similar yes, with few changes in the business rule, however, due to the fact that there are 12 tables and 12 entities, the whole system was designed as if these functionalities were separate.
– Vinícius
@Vinícius, all right, the interfaces are different, but the attributes and methods have different signatures? Or it could be encapsulated in a subsystem and let the polymorphism do the rest through a facade (facade)?
– Intruso
I agree with @Intruder that this may look like Stabbing, despite having a Bad Smell requesting refactoring... :) Following is a legal link to the pattern: http://www.dofactory.com/net/facade-designpattern
– Pedreiro
@Intruder, attributes vary little and methods could be common. Unfortunately, today is not like this, but for the features that will come can be done. The facade seems to be a good idea, although this approach runs away from Onion architecture. Creating a facade would not be similar to creating a common service for all these tables?
– Vinícius
@Vinicius, to choose, think about the long term. I suggested this (facade) because you said it is not possible to refactor, but the work of concentrating the complexity of creating objects in your scenario I think should be done with a Factory. If the methods and attributes are clearly similar, it is very likely that refactoring is easy by including an interface and causing services to inherit from it, depending on the similarity, you would only clean up the scattered Witches and Factory does the rest. Doing the service will have the same effect as the Facade, but the facade is a design pattern.
– Intruso