The service level should contain business operations and should be separated from the data access level (storage). The service layer provides business operations that can consist of several CRUD operations. These CRUD operations are performed by repositories. So, for example, you may have a business transaction that will transfer a certain amount of money from one account to another, and to complete this business transaction you first need to make sure that the sender account has sufficient reserves, debit the sender account and receiver account Service operations can also represent the boundaries of SQL transactions, which means that all elementary CRUD operations performed inside a business operation must be inside a transaction, and either they must succeed or roll back in case of an error.
, :
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);
}
}