【发布时间】:2017-01-09 20:33:11
【问题描述】:
从我记事起,我就一直在使用带有依赖注入的经典 3 层设计模式:
Presentation -> Service class -> Repository,我总是遇到同样的问题:一些大的 Service 类有太多的责任和代码冗余。
这么多年过去了,我的思想已经被这种设计模式毒害了,所以我需要一些建议来转向新的东西。我一直在阅读有关其他设计模式的一些资料,但有很多选择,我看不到从我的存储库模式到许多其他模式之一的转换。
基本示例:
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...
}
MachineService 类最终将成为 SuperService 类,负责从我的域模型创建和更新许多不同的对象。因为当机器对象发生某些事情时,需要发生许多其他事情。 我通常有更多的服务类,但由于 DI,我无法在服务类之间进行引用。这会产生代码冗余。 对 Repository 模式上瘾者有何建议?
编辑: 我以工作单元结束。在这里查看我的解决方案: https://stackoverflow.com/questions/41548169/rate-my-implementation-of-unif-of-work-with-repository-pattern-and-ef-core
【问题讨论】:
-
您要解决的问题是什么??
-
RIP 你的实现。
标签: c# .net design-patterns repository-pattern