栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Net Core AOP EF Core事务的统一管理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Net Core AOP EF Core事务的统一管理

除了使用Net Core 内置的 一些AOP 咱们也可以使用一些AOP 插件
1  事务的AOP 其实也可以简单的理解成一个过滤器了 执行前  执行中 执行后 
using AspectCore.DynamicProxy;
   /// 
    ///  事务  主要针对业务层 数据库处理事务
    ///  注意事项 如需要在控制器层面使用 则实现的Action 变成虚方法即可进入
    /// 
    public class TransactionInterceptorAttribute : AbstractInterceptorAttribute
    {
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var sampledbContext = (LifeSampledbContext)context.ServiceProvider.GetService(typeof(LifeSampledbContext));
            if (sampledbContext.Database.CurrentTransaction == null)
            {
                await sampledbContext.Database.BeginTransactionAsync();
                try
                {
                    await next(context);
                    sampledbContext.Database.CommitTransaction();
                }
                catch (Exception ex)
                {
                    sampledbContext.Database.RollbackTransaction();
                    throw;
                }
            }
            else
            {
                await next(context);
            }
        }
    }
2 在 StrapUp 中   services.AddDependencyInjection();
 3 在 Program 中
      public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging((context, builder) =>
                {
                    var path = Path.Combine(Directory.GetCurrentDirectory(), "Log4net.config");
                    builder.AddLog4Net(path);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup();
                }).UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709148.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号