zoukankan      html  css  js  c++  java
  • 基础才是重中之重~泛型类的静态构造方法可不是只执行一次呀

    回到目录

    最近做了一个数据库的读写分离项目,使用到了DbCommand拦截器,在程序开发过程中没有发现什么特别的问题,而当开发完成后,在进行测试阶段时,一个偶然的机会让我发现了,原来我的拦截器注入不只是注入一次,而是每种类型的仓储都会注入一次,这个问题事实上是相关严重的一件事,如果你的拦截器处理逻辑很多,那么,这将是非常消耗性能的。

    原因,静态构造方法对泛型类不是唯一的,而是相互独立的

      public abstract class DbContextRepository<TEntity> :
             ISpecificationRepository<TEntity>
              where TEntity : class
        {
            #region Constructors
    
            /// <summary>
            /// 静态构造方法对每个TEntity是独立的,有多少类型初始化,这个方法就执行多少次
            /// </summary>
            static DbContextRepository()
            {
             //泛型类的静态构造方法...
            }
     }

    结果,每个类型初始化时,都会向拦截器字典中添加一条

       IRepository<WebManageUsers> userWrite = new BackgroundRepositoryBase<WebManageUsers>(db);
       IRepository<WebManageMenus> menuWrite = new BackgroundRepositoryBase<WebManageMenus>(db);
      //每次初始化,静态构造方法都会被执行

    事实上,这是可以理解的,因为泛型类本身就是未定义的,当你初始化它时,具体的类型才被运行时得知,这时,在第一次使用它时,“这个类”的静态构造方法才会被执行,这是完全没问题的,可能开发人员有时就忽略了这一点。

    解决,使用反射来实现自己的按需添加

        /// <summary>
        /// DbCommand拦截器扩展
        /// </summary>
        public static class DbCommandInterceptorExtensions
        {
            /// <summary>
            /// 将DbCommand的拦截器以单例的形式添加到DbInterception静态对象中
            /// </summary>
            /// <param name="action"></param>
            public static void UsingSingletonInterceptor(DbCommandInterceptor interceptor)
            {
                #region SQL语句拦截器,拦截器只加载一次
                var property = typeof(DbCommandDispatcher).GetProperty("InternalDispatcher", BindingFlags.Instance | BindingFlags.NonPublic);
                if (property != null)
                {
                    var val = property.GetValue(System.Data.Entity.Infrastructure.Interception.DbInterception.Dispatch.Command);
                    if (val != null)
                    {
                        var list = val.GetType().GetField("_interceptors", BindingFlags.Instance | BindingFlags.NonPublic);
                        if (list != null)
                        {
                            var listVal = list.GetValue(val) as List<System.Data.Entity.Infrastructure.Interception.IDbCommandInterceptor>;
                            if (listVal != null)
                            {
                                if (listVal.FirstOrDefault(i => i.ToString() == interceptor.GetType().ToString()) == null)
                                {
                                    System.Data.Entity.Infrastructure.Interception.DbInterception.Add(interceptor);
                                }
                            }
                        }
                    }
                }
                #endregion
            }
        }

    调用这很方便

      EntityFrameworks.Data.Core.Extensions.DbCommandInterceptorExtensions.UsingSingletonInterceptor(new CommandInterceptor());

    OK,这样我们的每个拦截器在DbInterception对象中都只会出现一次,再也不会出现一个拦截器被执行多次的情况了,呵呵。

    回到目录

  • 相关阅读:
    MySQL事务隔离级别和MVCC
    windows环境下查看端口是否被占用
    jar找不到问题解决
    Java注解
    Fastjson
    mybatis
    Idea快捷键
    Date与String的相互转换
    Windows快捷键
    [转]linux awk命令详解
  • 原文地址:https://www.cnblogs.com/lori/p/4223463.html
Copyright © 2011-2022 走看看