将 DomainModel 添加到 UI 项目引用(域驱动设计)

Add DomainModel to UI project references ( Domain Driven Design)

在 DDD 分层架构中,假设我们有 UI 层通过基于 ViewModel 的应用程序服务与领域模型对话。应用程序服务通过构造函数注入获取通用存储库。

现在,在 UI 注入通用存储库的层中,我们需要将 DomainModel.dll 添加到 UI 项目,因为通用存储库的类型参数是在域模型中定义的。

将 DomainModel DLL 添加到 UI 层是否正确,或者应该在 UI 层中仅 AppService.dll 引用?

例如:

 //DomainModel.dll 

 public class StudentEntity
 {
  public long ID {get;set;}
  public string FirstName {get;set;}
  ...
 }

public interface IRepository<T>  where T :class
{
 void Insert(T enity);
 ...
}

//ApplicationService.dll

public class StudentService
{
 private IRepository<StudentEntity> _studentRep;

 public StudentService(IRepository<StudentEntity> studentRep)
 {
  _studentRep=studentRep;
 } 
 ...
}
//UI layer 

public class Main
{

     public class Program
     {
        public void Main(string[] args)
        {
            var studentService=new StudentService(??????);
// here for injecting Generic Repository we need to add domainModel.Dll to ui for access type parameter of generic repository

        }
    }

}

此致

您是否在 UI 层中使用域对象(模型、接口)?

如果是,那么您将需要对您的域库的引用。

如果否,那么您将能够创建一个单独的 bootstrap 项目来处理所有 DI 并公开其自己的一组对象供您 UI 处理;你的 UI 可以参考这个。

就个人而言,我认为域库应该在解决方案中随处引用,毕竟它是洋葱的中心。

意见:如果您最终得到一个由 poco 模型组成的域层,这些模型将 1:1 映射到您的数据库表和一堆通用的 CRUD 存储库,并依赖服务 类 来完成所有工作那么你可能没有在做 DDD,只是在你的 DAL 中添加了一些不必要的抽象。