Example #1
0
        /// <summary>
        /// 根据配置文件获取数据库类型,并返回对应的工厂接口
        /// </summary>
        /// <param name="obj">初始化参数,可为连接字符串或者DbContext</param>
        /// <returns></returns>
        public static IRepository GetRepository(Object obj = null, DatabaseType?dbType = null, string entityNamespace = null)
        {
            IRepository   res              = null;
            DatabaseType  _dbType          = GetDbType(dbType);
            Type          dbRepositoryType = Type.GetType("YK.DataRepository." + DbProviderFactoryHelper.DbTypeToDbTypeStr(_dbType) + "Repository");
            List <object> paramters        = new List <object>();

            void BuildParamters()
            {
                if (obj.IsNullOrEmpty())
                {
                    return;
                }

                if (obj is DbContext)
                {
                    paramters.Add(obj);
                    return;
                }
                else if (obj is string)
                {
                    paramters.Add(obj);
                    paramters.Add(entityNamespace);
                }
            }

            BuildParamters();
            res = _dbrepositoryContainer.Resolve <IRepository>(_dbType.ToString(), paramters.ToArray());
            return(res);
        }
        /// <summary>
        /// 根据配置文件获取数据库类型,并返回对应的工厂接口
        /// </summary>
        /// <param name="conString">链接字符串</param>
        /// <param name="dbType">数据库类型</param>
        /// <returns></returns>
        public static IRepository GetRepository(string conString = null, DatabaseType?dbType = null)
        {
            conString = conString.IsNullOrEmpty() ? GlobalSwitch.DefaultDbConName : conString;
            conString = DbProviderFactoryHelper.GetConStr(conString);
            dbType    = dbType.IsNullOrEmpty() ? GlobalSwitch.DatabaseType : dbType;
            Type dbRepositoryType = Type.GetType("Coldairarrow.DataRepository." + DbProviderFactoryHelper.DbTypeToDbTypeStr(dbType.Value) + "Repository");

            return(Activator.CreateInstance(dbRepositoryType, new object[] { conString }) as IRepository);
        }
        /// <summary>
        /// 根据配置文件获取数据库类型,并返回对应的工厂接口
        /// </summary>
        /// <param name="conString">链接字符串</param>
        /// <param name="dbType">数据库类型</param>
        /// <returns></returns>
        public static IRepository GetRepository(string conString = null, DatabaseType?dbType = null)
        {
            conString = conString.IsNullOrEmpty() ? GlobalSwitch.DefaultDbConName : conString;
            conString = DbProviderFactoryHelper.GetConStr(conString);
            dbType    = dbType.IsNullOrEmpty() ? GlobalSwitch.DatabaseType : dbType;
            Type dbRepositoryType = Type.GetType("Coldairarrow.DataRepository." + DbProviderFactoryHelper.DbTypeToDbTypeStr(dbType.Value) + "Repository");

            var repository = Activator.CreateInstance(dbRepositoryType, new object[] { conString }) as IRepository;

            //请求结束自动释放
            try
            {
                AutofacHelper.GetScopeService <IDisposableContainer>().AddDisposableObj(repository);
            }
            catch
            {
            }

            return(repository);
        }
Example #4
0
        /// <summary>
        /// 根据配置文件获取数据库类型,并返回对应的工厂接口
        /// </summary>
        /// <param name="conString">链接字符串</param>
        /// <param name="dbType">数据库类型</param>
        /// <returns></returns>
        public static IRepository GetRepository(string conString, DatabaseType?dbType)
        {
            //conString = conString.IsNullOrEmpty() ? "sql" : conString;
            //conString = DbProviderFactoryHelper.GetConStr(conString);
            dbType = dbType.IsNullOrEmpty() ?  DatabaseType.SqlServer : dbType;

            Type dbRepositoryType = Type.GetType("Andy.Mes.Persistence." + DbProviderFactoryHelper.DbTypeToDbTypeStr(dbType.Value) + "Repository");

            var repository = Activator.CreateInstance(dbRepositoryType, new object[] { conString }) as IRepository;

            //请求结束自动释放
            try
            {
                //AutofacHelper.GetScopeService<IDisposableContainer>().AddDisposableObj(repository);
            }
            catch
            {
            }

            return(repository);
        }