Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc UoW&;存储库&x2B;服务层_Asp.net Mvc_Service_Layer_Unit Of Work - Fatal编程技术网

Asp.net mvc UoW&;存储库&x2B;服务层

Asp.net mvc UoW&;存储库&x2B;服务层,asp.net-mvc,service,layer,unit-of-work,Asp.net Mvc,Service,Layer,Unit Of Work,我正在使用以下T4创建我的存储库&UoW: 现在我尝试添加一个服务层。我能够完成这样的事情: public ActionResult Index() { using (DataEntities context = new DataEntities()) { UnitOfWork uow = new UnitOfWork(context); //Service ClientService cli = new ClientServi

我正在使用以下T4创建我的存储库&UoW:

现在我尝试添加一个服务层。我能够完成这样的事情:

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);
    }
}
public ActionResult Index()
{
使用(DataEntities上下文=新DataEntities())
{
UnitOfWork uow=新的UnitOfWork(上下文);
//服务
ClientService cli=新的ClientService(uow);
var col=cli.getActive();
//将结果映射到ViewModel
var list=AutoMapper.Mapper.Map(col);
返回视图(列表);
}
}
这很好,但是

将UoW实例传递到服务层的架构是否正确?
(我在它的ctor中使用IUnitOfWork)

我尝试在服务层内移动上下文&UoW,但当我尝试将结果映射到控制器中的ViewModel时,上下文不可用


谢谢

我认为不,不是。再说一次,我不是工作单元的超级粉丝——我觉得它知道的太多了。我会将必要的存储库传递给您创建的服务。通常,我会以特殊的“GetService”或“CreateService”结束,但这可能适合您。。。(我在写意,所以它可能无法构建)

公共类DoSomethingCoolService:IDoSomethingCoolService
{
私人电子储蓄——需要的储蓄;
公共DoSomethingCoolService(连接或上下文)
{
//设置
}
公共DoSomethingCoolService(IRepository neededRepository)
{
_needededrepository=needededrepository;
}
公共列表ReturnWhatIWant()
{
_needededrepository.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
}
公共接口IGetService
{
//把嫌疑犯带到这里来
}
公共类GetService:IGetService
{
私人IRepository_存储库;
GetService(IRepository存储库)
//使用存储库调用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:IGetService
{
列出GetClientsForSomething(int-someId);
}
公共类GetClientService:GetService、IGetClientService
{
私人IRepository_存储库;
GetClientService(IRepository存储库):基本(存储库)
公共列表GetClientsForSomething(int-someId)
{
//这里有一些非常酷的商业逻辑,你想测试一下!
}
}
然后在我的控制器中,我只需要依赖IGetClientService,并在必要时使用它。易于测试,易于制作另一个不依赖它的产品


这有意义吗?

我认为没有意义。再说一次,我不是工作单元的超级粉丝——我觉得它知道的太多了。我会将必要的存储库传递给您创建的服务。通常,我会以特殊的“GetService”或“CreateService”结束,但这可能适合您。。。(我在写意,所以它可能无法构建)

公共类DoSomethingCoolService:IDoSomethingCoolService
{
私人电子储蓄——需要的储蓄;
公共DoSomethingCoolService(连接或上下文)
{
//设置
}
公共DoSomethingCoolService(IRepository neededRepository)
{
_needededrepository=needededrepository;
}
公共列表ReturnWhatIWant()
{
_needededrepository.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
}
公共接口IGetService
{
//把嫌疑犯带到这里来
}
公共类GetService:IGetService
{
私人IRepository_存储库;
GetService(IRepository存储库)
//使用存储库调用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:IGetService
{
列出GetClientsForSomething(int-someId);
}
公共类GetClientService:GetService、IGetClientService
{
私人IRepository_存储库;
GetClientService(IRepository存储库):基本(存储库)
公共列表GetClientsForSomething(int-someId)
{
//这里有一些非常酷的商业逻辑,你想测试一下!
}
}
然后在我的控制器中,我只需要依赖IGetClientService,并在必要时使用它。易于测试,易于制作另一个不依赖它的产品

这有什么意义吗