.NetCore study notes: Third, the AOP-based transaction management AspectCore

AOP (Aspect Oriented Programming), by way during the pre-compiled and run dynamic agent technology to implement a unified program functions maintenance. AOP is a continuation of OOP is a functional programming Yan Shengfan type. AOP can use to isolate each part of the business logic such that the business logic to reduce the degree of coupling between the parts, improve the reusability of the program, while improving efficiency of development.

AspectCore provides a new lightweight and modular Aop solution, now supports Asp.Net Core.

Implement AOP class TransactionalAttribute:

. 1  ///  <Summary> 
2  /// provide transactional units of work consistency
 . 3  ///  </ Summary> 
. 4  public  class TransactionalAttribute: AbstractInterceptorAttribute
 . 5  {
 . 6      IUnitOfWork _unitOfWork { GET ; SET ;}
 . 7  
. 8      public  the async  the override the Task the Invoke (AspectContext context, AspectDelegate Next)
 . 9      {
 10          the try 
. 11          {
 12 is              _unitOfWork context.ServiceProvider.GetService = ( typeof (IUnitOfWork)) AS IUnitOfWork;
13             _unitOfWork.BeginTransaction();
14             await next(context);
15             _unitOfWork.Commit();
16         }
17         catch (Exception)
18         {
19             _unitOfWork.Rollback();
20             throw;
21         }
22     }
23 }

It should be a reference AspectCore.Abstractions.dll library code UnitOfWork class has explained in previous articles: .NetCore study notes: First, UnitOfWork unit of work .

When reading and writing database will _unitOfWork.DbTransaction added. Execute the following is an example of Dapper:

 _unitOfWork.DbConnection.Execute(sql, param: param, transaction: _unitOfWork.DbTransaction, commandType: commandType);

Add the configuration for a Web application:

 1 public class Program
 2 {
 3     public static void Main(string[] args)
 4     {
 5         CreateHostBuilder(args).Build().Run();
 6     }
 7 
 8     public static IHostBuilder CreateHostBuilder(string[] args) =>
 9         Host.CreateDefaultBuilder(args)
10         .ConfigureWebHostDefaults(webBuilder =>
11         {
12             webBuilder.UseStartup<Startup>();
13         })          
14         .UseDynamicProxy();//aspcectcore
15 }

In netcore3.1 the configuration is relatively simple, just add UseDynamicProxy in the Program class () on it.

In such matters we need to use local use [Transactional] label click on it.

 1 public class TestService : ITestService
 2 {
 3     ITestDomain _testDomain { get; set; }
 4     public TestService(ITestDomain testDomain)
 5     {
 6         this._testDomain = testDomain;
 7     }
 8 
 9     public Test Get(string id)
10     {
11         var test = _testDomain.Get(id);
12         return test;
13     }
14 
15     [Transactional]
16     public void Insert(List<Test> list)
17     {
18         _testDomain.InsertList(list);
19     }
20 }

ITestDomain upper code implemented:

public  class TestDomain: ITestDomain
{
    IRepository<Test> _testRepository { get; set; }
    public TestDomain(IRepository<Test> testRepository)
    {
        this._testRepository = testRepository;
    }

    public Test Get(string id)
    {
        return _testRepository.Get(id);
    }

    public void Insert(List<Test> list)
    {
        if (list == null)
            return;
        foreach(var item in list)
            _testRepository.Insert(item);
    }
}

The code fragment in Repository in the previous article has explained: .NetCore study notes: Second, based Dapper generic Repository .

This realization of the AOP affairs.

Guess you like

Origin www.cnblogs.com/letnet/p/12118762.html