我有一个通过 Onion 架构和 DDD 实现的解决方案,例如eShopOnContainers。
但是我有一个问题要做,所以我决定与你分享。我试图通过一个例子来解释它。你想我有一个像IOrderRepositoryinterface
这样的名字IOrderRepository
public interface IOrderRepository : IRepository<Order>
{
Order Add(Order order); // first issue
Task<OrderDTO> GetAsync(int orderId); // second issue
}
首要问题
我实现了 OrderRepository 之Add
类的方法,但我需要在方法中使用额外的参数,例如以下代码:Add
public Order Add(Order order, int par1, int par2)
{
// I need a DTO to persist order
OrderDTO orderDTO = new OrderDTO({Order = order, Par1 = par1, Par2 = par2 });
// Call a method with orderDTO parameter as a service to insert order
}
如您所见,IOrderRepository
由于我需要额外的参数,因此实施是错误的。
第一个问题的错误解决方案
我有两个错误的解决方案来解决这个问题。
1- 调整IOrderRepository
通过添加如下参数来更改输入IOrderRepository
参数:
public interface IOrderRepository : IRepository<Order>
{
Order Add(Order order, int par1, int par2);
}
据我所知,没有任何业务规则par1 and par2
来实现 DDD,首先我应该指定IRepository
,但是通过使用这个解决方案,我将基础设施层问题放在了错误架构的领域层中。
2-IOrderRepository
进入基础设施层
我可以放入IOrderRepository
基础设施层,但这是另一个错误的架构,因为据我所知,这种接口应该位于Domain layer
.
我的第一个问题
1-如何在基础设施层的存储库的方法中使用额外的参数来实现参数与域层之间没有任何连接的域层的IRepository?
第二期
正如您在 中看到的IOrderRepository
,我应该实现GetAsync
返回OrderDTO
包含Order
和额外参数的方法。据我所知,我不能在域层中使用 DTO(数据传输对象)。我想不出办法来处理它。
我的第二个问题
2-我如何返回
OrderRepository
基础设施层中的 DTO 方法,但我不能IOrderRepository
在域层中应用它。
在此先感谢您的时间。