什么是ASP.NET MVC服务层的一个很好的例子? [英] What is a good example of a service layer in ASP.NET MVC?

查看:90
本文介绍了什么是ASP.NET MVC服务层的一个很好的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道什么是一个很好的例子(提供一些code)在ASP.NET MVC?

i would love to know what is a good example (provide some code) of a service layer in ASP.NET MVC ?


  • 究竟应该包括并包含?

  • 从什么它应该脱钩?

感谢。

推荐答案

服务层应包括业务操作,它应该从数据访问层(库)脱钩。服务层暴露这可能是由多个CRUD操作的业务运营。这些CRUD操作都是由库执行。因此,例如,你可以有一个业务操作,它会从一个账户转移一些钱到另一个,以执行此经营,你需要首先确保发件人帐户有足够的规定,借记发件人帐户和信贷接收帐户。该服务的操作也可以重新present SQL事务这意味着所有的业务操作中执行基本CRUD操作应该是一个事务中,要么所有的人都应该在错误的情况下成功或回滚的界限。

The service layer should contain business operations and it should be decoupled from the data access layer (repositories). The service layer exposes business operations which could be composed of multiple CRUD operations. Those CRUD operations are performed by the repositories. So for example you could have a business operation which would transfer some amount of money from one account to another and in order to perform this business operation you will need first to ensure that the sender account has sufficient provisions, debit the sender account and credit the receiver account. The service operations could also represent the boundaries of SQL transactions meaning that all the elementary CRUD operations performed inside the business operation should be inside a transaction and either all of them should succeed or rollback in case of error.

为了去耦可以使用接口基础数据访问层的服务层:

In order to decouple the service layer from the underlying data access layer you could use interfaces:

public class BankService
{
    private readonly IAccountsRepository _accountsRepository;
    public OrdersService(IAccountsRepository accountsRepository)
    {
        _accountsRepository = accountsRepository;
    }

    public void Transfer(Account from, Account to, decimal amount)
    {
        _accountsRepository.Debit(from, amount);
        _accountsRepository.Credit(to, amount);
    }
}

这篇关于什么是ASP.NET MVC服务层的一个很好的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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