UoW和存储库+服务层

| 我正在使用以下T4​​创建我的存储库和UoW: http://blogs.microsoft.co.il/blogs/gilf/archive/2010/07/05/repository-and-unit-of-work-t4-template-for-entity-framework.aspx 现在,我正在尝试添加服务层。我能够完成这样的事情:
public ActionResult Index()
{
    using (DataEntities context = new DataEntities())
    {
        UnitOfWork uow = new UnitOfWork(context);

        //Service
        ClientService cli = new ClientService(uow);
        var col = cli.getActive();

        //Map results to ViewModel
        var list = AutoMapper.Mapper.Map<IEnumerable<Client>, IEnumerable<ClientListViewModel>>(col);

        return View(list);
    }
}
这很好,但是... 在体系结构上将UoW实例传递到服务层是否正确? (我在其ctor中使用IUnitOfWork) 我尝试在服务层内移动上下文和UoW,但是当我尝试将结果映射到控制器中的ViewModel时,上下文不可用。 谢谢!     
已邀请:
我会说不,不是。再说一次,我不是工作单元的忠实拥护者-我觉得它知道太多了。我会将必要的存储库传递给您创建的服务。通常,我最终使用特殊的\“ GetService \”或\“ CreateService \”,但这可能对您有用...(我正在写此徒手画,因此可能无法构建)
Public class DoSomethingCoolService : IDoSomethingCoolService
{

     private IRepository<SomethingINeed> _neededRepository;

     public DoSomethingCoolService(connectionOrContext)
     {
          //setup
     }

     public DoSomethingCoolService(IRepository<SomethingINeed> neededRepository)
     {
          _neededRepository = neededRepository;
     }

     public List<SomethingINeed> ReturnWhatIWant()
     {
          _neededRepository.Where(x => x.WhatIWant = true);
     }

}
我个人不喜欢这样。我更喜欢这样的东西...
public interface IGetService<T>
{
    //usual get suspects here
}

public class GetService<T> : IGetService<T>
{
    private IRepository<T> _repository;
    GetService(IRepository<T> repository)

    //use repository to call gets
}
现在,对于复杂的东西...
public interface IGetClientService : IGetService<Client>
{
     List<Client> GetClientsForSomething(int someId);
}

public class GetClientService : GetService<Client>, IGetClientService
{
        private IRepository<Client> _repository;
        GetClientService(IRepository<Client> repository) : base(repository)

        public List<Client> GetClientsForSomething(int someId)
        {
              //some crazy cool business logic stuff here you want to test!
        }
}
然后在我的控制器内部,我只依赖于IGetClientService,并在必要时使用它。易于测试,易于制造不依赖于它的另一个。 这有意义吗?     

要回复问题请先登录注册