DDD架构ASP.NET MVC2项目 [英] DDD Architecture for ASP.NET MVC2 Project

查看:165
本文介绍了DDD架构ASP.NET MVC2项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用领域驱动开发(DDD)与实体框架4.我的新ASP.NET MVC2项目做一些研究,我想出了用自己的类项目中的每个层下面的图层约定后:

I am trying to use Domain Driven Development (DDD) for my new ASP.NET MVC2 project with Entity Framework 4. After doing some research I came up with the following layer conventions with each layer in its own class project:

MyCompany.Domain

     public class User
    {
        //Contains all the properties for the user entity
    }

    public interface IRepository<T> where T : class
    {
        IQueryable<T> GetQuery();
        IQueryable<T> GetAll();
        IQueryable<T> Find(Func<T, bool> condition);
        T Single(Func<T, bool> condition);
        T First(Func<T, bool> condition);
        T GetByID(int id);
        void Delete(T entity);
        void Add(T entity);
        void Attach(T entity);
        void SaveChanges();
    }

  public interface IUserRepository: IRepository<User> {}

    public class UserService
    {
        private IUserRepository _userRepository;
        public UserService(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }
    // This class will hold all the methods related to the User entity
    }

MyCompany.Repositories

public class UserRepository : IRepository<User>
{
    // Repository interface implementations
}

MyCompany.Web - >这是MVC2项目

MyCompany.Web --> This is the MVC2 Project

目前我的存储库层保存了域层的参考。从我的理解注入UserRepository一个到UserService类工作得很好的单元测试,我们可以伪造用户库通过。因此,与这种架构,它看起来像我的Web项目需要有这两个我的域名和存储库层的引用。但是,这是一个有效的?由于历史上的presentation层只有到业务逻辑层的参考。

Currently my Repositories layer holds a reference to the Domain layer. From my understanding injecting a UserRepository to the UserService class works very well with unit testing as we can pass in fake user repositories. So with this architecture it looks like my Web project needs to have a references to both my Domain and Repositories layers. But is this a valid? Because historically the presentation layer only had a reference to the Business Logic layer.

推荐答案

这是非常有效的,而事实上非常相似,我们有设置。

This is very valid, and in fact very similar to the setup we have.

然而,在你的问题,你有 IRepository 域中的项目,我想这把你的资料库组装。

However in your question, you've got IRepository in the domain project, i would put this in your Repositories assembly.

您的层应该有你的域实体和商业逻辑。
您的层应具备的通用库接口,这个具体的实现,每一个聚合根。

Your Domain layer should have your domain entities and business logic. Your Repository layer should have the generic repository interface, and concrete implementations of this, one for each aggregate root.

我们也有一个服务的UI层之间的中介(控制器)和存储库。这使得一个中心位置把它不会在存储库属于逻辑 - 之类的东西分页和验证

We also have a Service layer mediating between the UI (Controllers) and the Repository. This allows a central location to put logic which does not belong in the Repository - things like Paging and Validation.

这种方式,用户界面​​不会引用资源库,只有​​服务层。

This way, the UI does not reference the Repository, only the Service Layer.

我们还可以使用DI注入EntityFrameworkRepository到我们的服务层和MockRepository到我们的测试项目。

We also use DI to inject the EntityFrameworkRepository into our Service Layer, and a MockRepository into our test project.

您似乎是在正确的轨道上,但因为它似乎想要去DDD-全的路,你有没有考虑过实施的工作单位的模式来管理多个存储库共享相同的背景?

You seem to be on the right track, but since it seems to want to go "DDD-All-The-Way", have you considered implementing the Unit of Work pattern to manage multiple repositories sharing the same context?

这篇关于DDD架构ASP.NET MVC2项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆