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

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

问题描述

在 OOP 设计模式中,存储库模式和服务层有什么区别?

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 为您提供了对数据访问的额外抽象级别.而不是写

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();
}

并调用Get(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);
}

虽然repository的List()方法返回所有用户,但IUserService的ListUsers()只能返回一个,用户可以访问.

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:

视图 <- 控制器 -> 服务层 -> 存储库层 -> EF -> SQL Server

Service layer -> Repository layer -> EF 这部分对模型进行操作.

Service layer -> Repository layer -> EF This part operates on models.

Views <- Controllers -> Service layer 这部分操作在视图模型上.

Views <- Controllers -> Service layer This part operates on view models.

/Orders/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); 
    }
}

这是订单服务的接口:

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天全站免登陆