For as long as I can remember I have been using the classic 3 layer design pattern with Dependency Injection:
只要我记得我一直在使用经典的3层设计模式和依赖注入:
Presentation -> Service class -> Repository, and I always end up with the same problem: A few big Service classes that have way too much responsibility and code redundancy.
演示 - >服务类 - >存储库,我总是遇到同样的问题:一些大的服务类负有太多的责任和代码冗余。
My mind have been poisoned by this design pattern after all these years, so I need a bit of advice to move on to something new. I have been doing some reading about other design patterns but there are so many choices and I fail to see the transition from my repository pattern to one of the many other patterns.
经过这么多年,我的思维已经被这种设计模式所毒害了,所以我需要一些建议来转向新的东西。我一直在阅读有关其他设计模式的一些内容,但是有很多选择,我没有看到从我的存储库模式转换到许多其他模式之一。
Basic example:
基本示例:
public void CreateMachine(Machine machine)
{
this.repository.Insert(machine);
}
public void StartMachine(Guid machineid)
{
Machine machine = repository.GetMachine(machineId);
machine.Started = true;
repository.UpdateMachine(machine);
MachineEvent mEvent = new MachineEvent(machineId);
mEvent.MachineId = machineId;
mEvent.EventType = MachineEventEnum.MachineStarted;
this.eventRepository.Insert(mEvent);
If (machine.IsBigMachine == true)
{
// Do something more
}
// many more objects to create or update...
}
The MachineService class will end up as a SuperService class, having the responsibility to create and update many different objects from my domain model. Because when something happens to an Machine object, then many other things need to happen. I usually have many more service classes but because of DI, I cant have a reference inbetween the service classes. That creates code redundancy. Any advice for a Repository pattern addict?
MachineService类最终将作为SuperService类,负责从我的域模型创建和更新许多不同的对象。因为当Machine对象发生某些事情时,需要发生许多其他事情。我通常有更多的服务类,但由于DI,我不能在服务类之间引用。这创建了代码冗余。对存储库模式上瘾的任何建议?
EDIT: I ended with Unit Of Work. See my solution here: https://*.com/questions/41548169/rate-my-implementation-of-unif-of-work-with-repository-pattern-and-ef-core
编辑:我以工作单位结束。请在此处查看我的解决方案:https://*.com/questions/41548169/rate-my-implementation-of-unif-of-work-with-repository-pattern-and-ef-core
1 个解决方案
#1
4
Having a super class has nothing to do with Service pattern or repository pattern or DI. It can happen anywhere with any pattern. It is a problem on its own that needs to be dealt with accordingly. It can be solved with applying proper class design guidelines, like SOLID. The most common cause of this problem is not adhering to S in SOLID. That is, if you design your class - service class is not exception - with a well-defined single purpose, you can avoid having these god classes.
拥有超类与服务模式或存储库模式或DI无关。它可以在任何地方发生任何模式。这是一个需要相应处理的问题。它可以通过应用适当的类设计指南来解决,例如SOLID。导致此问题的最常见原因是不遵守SOLID中的S.也就是说,如果你设计你的类 - 服务类不是例外 - 具有明确定义的单一目的,你可以避免拥有这些神类。
In your case think of breaking down your classes into smaller classes. But, don't start from refactoring the code. Start at a higher level, for example some box and arrow (simple class diagram) to visualize entities you will have and the relationship among them. It is important not to delve into code while you are at this stage. Apply SOLID at this stage. Once this stage is thought through, then you can refactor existing code according to it. Without having this high-level view, things almost always get complicated.
在您的情况下,请考虑将您的类分解为更小的类。但是,不要从重构代码开始。从更高的层次开始,例如一些框和箭头(简单的类图),以可视化您将拥有的实体以及它们之间的关系。在此阶段,不要深入研究代码是很重要的。在此阶段应用SOLID。一旦考虑了这个阶段,您就可以根据它重构现有代码。没有这种高级别的观点,事情几乎总是变得复杂。
#1
4
Having a super class has nothing to do with Service pattern or repository pattern or DI. It can happen anywhere with any pattern. It is a problem on its own that needs to be dealt with accordingly. It can be solved with applying proper class design guidelines, like SOLID. The most common cause of this problem is not adhering to S in SOLID. That is, if you design your class - service class is not exception - with a well-defined single purpose, you can avoid having these god classes.
拥有超类与服务模式或存储库模式或DI无关。它可以在任何地方发生任何模式。这是一个需要相应处理的问题。它可以通过应用适当的类设计指南来解决,例如SOLID。导致此问题的最常见原因是不遵守SOLID中的S.也就是说,如果你设计你的类 - 服务类不是例外 - 具有明确定义的单一目的,你可以避免拥有这些神类。
In your case think of breaking down your classes into smaller classes. But, don't start from refactoring the code. Start at a higher level, for example some box and arrow (simple class diagram) to visualize entities you will have and the relationship among them. It is important not to delve into code while you are at this stage. Apply SOLID at this stage. Once this stage is thought through, then you can refactor existing code according to it. Without having this high-level view, things almost always get complicated.
在您的情况下,请考虑将您的类分解为更小的类。但是,不要从重构代码开始。从更高的层次开始,例如一些框和箭头(简单的类图),以可视化您将拥有的实体以及它们之间的关系。在此阶段,不要深入研究代码是很重要的。在此阶段应用SOLID。一旦考虑了这个阶段,您就可以根据它重构现有代码。没有这种高级别的观点,事情几乎总是变得复杂。