库和服务层之间的区别? [英] Difference between Repository and Service Layer?

查看:169
本文介绍了库和服务层之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在面向对象的设计模式,存储库模式和服务层之间的区别是什么呢?

In OOP Design Patterns, what is the difference between the Repository Pattern and a Service Layer?

我工作的一个ASP.NET MVC 3应用程序,并且想了解这些设计模式,但我的大脑只是没有得到它...但!!

I am working on an ASP.NET MVC 3 app, and am trying to understand these design patterns, but my brain is just not getting it...yet!!

推荐答案

库层为您提供了抽象对数据访问的级别。而不是写的

Repository Layer gives you additional level of abstraction over data access. Instead of writing

var context = new DatabaseContext();
return CreateObjectQuery<Type>().Where(t => t.ID == param).First();

要得到数据库中的单个项目,可以使用库接口

to get a single item from database, you use repository interface

public interface IRepository<T>
{
    IQueryable<T> List();
    bool Create(T item);
    bool Delete(int id);
    T Get(int id);
    bool SaveChanges();
}

和调用获取(ID)。存储库层暴露了基本的 CRUD 操作。

and call Get(id). Repository layer exposes basic CRUD operations.

服务层暴露的商业逻辑,它使用存储库。例如服务可能看起来像:

Service layer exposes business logic, which uses repository. Example service could look like:

public interface IUserService
{
    User GetByUserName(string userName);
    string GetUserNameByEmail(string email);
    bool EditBasicUserData(User user);
    User GetUserByID(int id);
    bool DeleteUser(int id);
    IQueryable<User> ListUsers();
    bool ChangePassword(string userName, string newPassword);
    bool SendPasswordReminder(string userName);
    bool RegisterNewUser(RegisterNewUserModel model);
}

列表()库的方法返回的所有用户, ListUsers() IUserService的可以返回唯一的,用户有权访问。

While List() method of repository returns all users, ListUsers() of IUserService could return only ones, user has access to.

在ASP.NET MVC + EF + SQL SERVER,我有这种通信流量:

In ASP.NET MVC + EF + SQL SERVER, I have this flow of communication:

观点与LT; - 控制器 - >服务层 - >存储库层 - > EF - > SQL服务器

服务层 - >库层 - > EF 这部分机型上运行

观点与LT; - 控制器 - >服务层该零件视图模型运行

编辑:

流程示例/订单/ ByClient / 5(我们希望看到为了特定的客户端):

Example of flow for /Orders/ByClient/5 (we want to see order for specific client):

public class OrderController
{
    private IOrderService _orderService;

    public OrderController(IOrderService orderService)
    {
        _orderService = orderService; // injected by IOC container
    }

    public ActionResult ByClient(int id)
    {
        var model = _orderService.GetByClient(id);
        return View(model); 
    }
}

这是接口,用于订购服务:

This is interface for order service:

public interface IOrderService
{
    OrdersByClientViewModel GetByClient(int id);
}

此接口返回查看模式:

public class OrdersByClientViewModel
{
     CientViewModel Client { get; set; } //instead of ClientView, in simple project EF Client class could be used
     IEnumerable<OrderViewModel> Orders { get; set; }
}

这是接口的实现。它采用模型类和资源库创建视图模型:

This is interface implementation. It uses model classes and repository to create view model:

public class OrderService : IOrderService
{
     IRepository<Client> _clientRepository;
     public OrderService(IRepository<Client> clientRepository)
     {
         _clientRepository = clientRepository; //injected
     }

     public OrdersByClientViewModel GetByClient(int id)
     {
         return _clientRepository.Get(id).Select(c => 
             new OrdersByClientViewModel 
             {
                 Cient = new ClientViewModel { ...init with values from c...}
                 Orders = c.Orders.Select(o => new OrderViewModel { ...init with values from o...}     
             }
         );
     }
}

这篇关于库和服务层之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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