Ejemplo n.º 1
0
        static IGeneralFactory CreateGeneral(Assembly providerAssembly)
        {
            var type = providerAssembly.GetTypes().Where(f => f.GetInterface(typeof(IGeneralFactory).Name) != null).FirstOrDefault();

            CheckNotNull.NotNull(typeof(IGeneralFactory), $"程序集中 {providerAssembly.FullName} 找不到 IGeneralFactory 的实现。");
            return((IGeneralFactory)Activator.CreateInstance(type));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///  初始化数据库连接
        /// </summary>
        /// <param name="options"></param>
        public static void InitConnection(StagingOptions options)
        {
            CheckNotNull.NotNull(options, nameof(options));
            CheckNotNull.NotEmpty(options.ConnectionMaster, nameof(options.ConnectionMaster));

            Options = options;

            // 初始化主库连接实例
            ConnectionStringConfiguration conn = new ConnectionStringConfiguration()
            {
                ConnectionString = options.ConnectionMaster,
                DbConnection     = new Npgsql.NpgsqlConnection(options.ConnectionMaster)
            };

            InstanceMaster = new MasterExecute(options.Logger, conn);

            // 初始化从库连接实例
            List <ConnectionStringConfiguration> connList = GetSlaves(options.ConnectionSlaves);

            if (connList != null)
            {
                InstanceSlave = new SlaveExecute(options.Logger, connList);
            }

            if (options.CacheOptions != null && options.CacheOptions.Cache != null)
            {
                CacheManager = new CacheManager(options.CacheOptions);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 增加一个查询参数
        /// </summary>
        /// <param name="parameters">输入参数</param>
        /// <returns></returns>
        public QueryContext <T> AddParameter(NpgsqlParameter parameter)
        {
            CheckNotNull.NotNull(parameter, nameof(parameter));

            ParamList.Add(parameter);
            return(this);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///  封装多个查询结果集,以管道的形式
        /// </summary>
        /// <param name="master">是否在主库执行查询</param>
        /// <param name="contexts">查询上下文对象</param>
        /// <returns></returns>
        public static List <List <dynamic> > ExecutePipeLine(bool master, params IQueryPipe[] contexts)
        {
            CheckNotNull.NotEmpty(contexts, nameof(contexts));

            StringBuilder      sb         = new StringBuilder();
            List <DbParameter> parameters = new List <DbParameter>();

            foreach (var ctx in contexts)
            {
                sb.AppendLine(ctx.CommandText);
                sb.Append(";");
                parameters.AddRange(ctx.ParamList);
            }

            var cmdText  = sb.ToString();
            int pipeLine = contexts.Length;
            List <List <dynamic> > result = new List <List <dynamic> >();

            if (master)
            {
                CheckNotNull.NotNull(InstanceMaster, nameof(InstanceMaster));

                InstanceMaster.ExecuteDataReaderPipe(dr =>
                {
                    ExcutePipeResult(contexts, dr, pipeLine, result);
                }, CommandType.Text, cmdText, parameters.ToArray());
            }
            else
            {
                CheckNotNull.NotNull(InstanceSlave, nameof(InstanceSlave));

                InstanceSlave.ExecuteDataReaderPipe(dr =>
                {
                    ExcutePipeResult(contexts, dr, pipeLine, result);
                }, CommandType.Text, cmdText, parameters.ToArray());
            }

            return(result);
        }
Ejemplo n.º 5
0
        public void Initializer(StagingOptions options)
        {
            CheckNotNull.NotNull(options, nameof(options));
            CheckNotNull.NotNull(options.Name, nameof(options.Name));

            Options = options;
            if (options.CacheOptions != null && options.CacheOptions.Cache != null)
            {
                CacheManager = new CacheManager(options.CacheOptions);
            }

            InternalDbContext.Initializer(Options);
            var properties = this.GetType().GetProperties();

            foreach (var pi in properties)
            {
                if (pi.PropertyType.Name == "DbSet`1")
                {
                    var piObject = Activator.CreateInstance(pi.PropertyType);
                    pi.SetValue(this, piObject);
                    var genericType       = pi.PropertyType.GenericTypeArguments[0];
                    var childreProperties = pi.PropertyType.GetProperties();
                    foreach (var children in childreProperties)
                    {
                        var interfaceType = InternalDbContext.GetInterfaceType(children.PropertyType.Name, genericType);
                        if (interfaceType != null)
                        {
                            children.SetValue(piObject, Activator.CreateInstance(interfaceType, this));
                        }
                    }
                }
            }
            InternalDbContext.SERVICES.TryGetValue(typeof(IStagingConnection).Name, out Type iop);
            Options.Connection = (IStagingConnection)Activator.CreateInstance(iop);
            this.Refresh(Options.Master, Options.Slaves);
        }