Ejemplo n.º 1
0
        public void AddDbContext <TContext>(DbContextOption option)
            where TContext : BaseDbContext, IDbContextCore
        {
            TContext context = (TContext)Activator.CreateInstance(typeof(TContext), option);

            ServiceCollection.AddSingleton <IDbContextCore>(context);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 初始化Ioc容器
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        private IServiceProvider InitIoc(IServiceCollection services)
        {
            var connectionString = Configuration.GetConnectionString("MsSqlServer");
            //var connectionString = Configuration.GetConnectionString("MySql");
            var dbContextOption = new DbContextOption
            {
                ConnectionString = connectionString,
                DbType           = DbTypeEnum.MSSQLSERVER,
                //DbType = DbTypeEnum.MYSQL,
                ModelAssemblyName = "Ses.AspNetCore.Entities",
            };

            //var codeGenerateOption = new CodeGenerateOption
            //{
            //    ModelsNamespace = "Zxw.Framework.Website.Models",
            //    IRepositoriesNamespace = "Zxw.Framework.Website.IRepositories",
            //    RepositoriesNamespace = "Zxw.Framework.Website.Repositories",
            //    IServicsNamespace = "Zxw.Framework.Website.IServices",
            //    ServicesNamespace = "Zxw.Framework.Website.Services"
            //};
            AutofacContainer.Register(Configuration);            //注册配置
            AutofacContainer.Register(dbContextOption);          //注册数据库配置信息
            //AutofacContainer.Register(codeGenerateOption);//注册代码生成器相关配置信息
            AutofacContainer.Register(typeof(DefaultDbContext)); //注册EF上下文
            AutofacContainer.RegisterGeneric(typeof(IRepository <,>), typeof(BaseRepository <,>));
            AutofacContainer.RegisterGeneric(typeof(IBaseService <,>), typeof(BaseService <,>));
            AutofacContainer.Register("Ses.AspNetCore.Services", "Ses.AspNetCore.IServices");//注册service

            return(AutofacContainer.Build(services));
        }
Ejemplo n.º 3
0
        private static void AddMainDb(string container, string dbConnectionString, string providerName, string entityAssmbly, bool isFromDB, string defaultContainer, ILoggerFactory loggerFactory = null)
        {
            //var entityAssmbly = config.GetSection("appSettings").GetValue<string>("ENTITY_ASSMBLY");
            DbContextOption dbContextOption = null;

            if (isFromDB)
            {
                dbContextOption = GetMainDbContextOption(new DbContextOption
                {
                    Container          = container,
                    ConnectionString   = dbConnectionString,
                    ModuleAssemblyName = entityAssmbly,
                    Provider           = providerName,
                    LogggerFactory     = loggerFactory
                }, defaultContainer);
                //dbContextOption.ModuleAssemblyName = entityAssmbly;
            }
            else
            {
                dbContextOption = new DbContextOption
                {
                    Container          = defaultContainer,
                    ConnectionString   = dbConnectionString,
                    ModuleAssemblyName = entityAssmbly,
                    Provider           = providerName,
                    LogggerFactory     = loggerFactory
                };
            }

            DbConfiguration.Configure(dbContextOption);
        }
Ejemplo n.º 4
0
        private IDbContextCore GetDbContext(DatabaseType dbType, DbContextOption option)
        {
            IDbContextCore dbContext;

            switch (dbType)
            {
            case DatabaseType.MySQL:
                dbContext = new MySqlDbContext(option);
                break;

            case DatabaseType.PostgreSQL:
                dbContext = new PostgreSQLDbContext(option);
                break;

            case DatabaseType.Oracle:
                dbContext = new OracleDbContext(option);
                break;

            default:
                dbContext = new SqlServerDbContext(option);
                break;
            }

            return(dbContext);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// IoC初始化
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        private IServiceProvider InitIoC(IServiceCollection services)
        {
            var connectionString = Configuration.GetConnectionString("MsSqlServer");
            var dbContextOption  = new DbContextOption
            {
                ConnectionString  = connectionString,
                ModelAssemblyName = "Zxw.Framework.Website.Models",
                DbType            = DbType.MSSQLSERVER
            };
            var codeGenerateOption = new CodeGenerateOption
            {
                ModelsNamespace        = "Zxw.Framework.Website.Models",
                IRepositoriesNamespace = "Zxw.Framework.Website.IRepositories",
                RepositoriesNamespace  = "Zxw.Framework.Website.Repositories",
                IServicsNamespace      = "Zxw.Framework.Website.IServices",
                ServicesNamespace      = "Zxw.Framework.Website.Services"
            };

            IoCContainer.Register(Configuration);                                                               //注册配置
            IoCContainer.Register(dbContextOption);                                                             //注册数据库配置信息
            IoCContainer.Register(codeGenerateOption);                                                          //注册代码生成器相关配置信息
            IoCContainer.Register(typeof(DefaultDbContext));                                                    //注册EF上下文
            IoCContainer.Register("Zxw.Framework.Website.Repositories", "Zxw.Framework.Website.IRepositories"); //注册仓储
            IoCContainer.Register("Zxw.Framework.Website.Services", "Zxw.Framework.Website.IServices");         //注册service
            return(IoCContainer.Build(services));
        }
Ejemplo n.º 6
0
        static void InitIoC()
        {
            var dbContextOption = new DbContextOption
            {
                ConnectionString  = ConfigHelper.GetConnectionString("mssqlserver"),
                ModelAssemblyName = "Zxw.Framework.Sample.Models"
            };
            var codeGenerateOption = new CodeGenerateOption
            {
                ModelsNamespace        = "Zxw.Framework.Sample.Models",
                IRepositoriesNamespace = "Zxw.Framework.Sample.IRepositories",
                RepositoriesNamespace  = "Zxw.Framework.Sample.Repositories",
                IServicsNamespace      = "Zxw.Framework.Sample.IServices",
                ServicesNamespace      = "Zxw.Framework.Sample.Services"
            };

            IoCContainer.Register(dbContextOption);
            IoCContainer.Register(codeGenerateOption);
            IoCContainer.Register <DefaultDbContext>();

            #region 此段代码在Repository和Service层代码生成之后再启用

            //IoCContainer.RegisterImplementationAssemblyAsInterface("Zxw.Framework.Sample.Repositories", "Zxw.Framework.Sample.IRepositories");
            //IoCContainer.RegisterImplementationAssemblyAsInterface("Zxw.Framework.Sample.Services", "Zxw.Framework.Sample.IServices");

            #endregion

            IoCContainer.Build();
        }
Ejemplo n.º 7
0
        public ProductContext(DbContextOption <ProductContext> option)

        {
            if (option is null)
            {
                throw new ArgumentNullException(nameof(option));
            }
        }
 //public EntityFrameworkContext(DbContextOptions options) : base(options) { }
 public EntityFrameworkContext(DbContextOption option)
 {
     if (option == null)
     {
         throw new ArgumentNullException(nameof(option));
     }
     if (string.IsNullOrEmpty(option.CommandString))
     {
         throw new ArgumentNullException(nameof(option.CommandString));
     }
     _option = option;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// ctor
 /// </summary>
 /// <param name="connectString"></param>
 public UnitDbContext(DbContextOption option)
 {
     if (option == null)
     {
         throw new ArgumentNullException(nameof(option));
     }
     if (string.IsNullOrEmpty(option.ConnectionString))
     {
         throw new ArgumentNullException(nameof(option.ConnectionString));
     }
     _option = option;
 }
Ejemplo n.º 10
0
        //private readonly string _connstr = Global.QueryDB;
        //public DapperContext()
        //{
        //}

        //public DapperContext(string connstr)
        //{

        //    _connstr = connstr;
        //}
        public DapperContext(DbContextOption option)
        {
            if (option == null)
            {
                throw new ArgumentNullException(nameof(option));
            }
            if (string.IsNullOrEmpty(option.QueryString))
            {
                throw new ArgumentNullException(nameof(option.QueryString));
            }
            _option = option;
        }
Ejemplo n.º 11
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="option"></param>
 public DefaultDbContext(DbContextOption option) : base(option.ConnectionString)
 {
     if (option == null)
     {
         throw new ArgumentNullException(nameof(option));
     }
     if (string.IsNullOrEmpty(option.ModelAssemblyName))
     {
         throw new ArgumentNullException(nameof(option.ModelAssemblyName));
     }
     _option = option;
     this.Configuration.AutoDetectChangesEnabled = false;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="option"></param>
 protected BaseDbContext(IOptions <DbContextOption> option)
 {
     if (option == null)
     {
         throw new ArgumentNullException(nameof(option));
     }
     if (string.IsNullOrEmpty(option.Value.ConnectionString))
     {
         throw new ArgumentNullException(nameof(option.Value.ConnectionString));
     }
     //if (string.IsNullOrEmpty(option.Value.ModelAssemblyName))
     //    throw new ArgumentNullException(nameof(option.Value.ModelAssemblyName));
     _option = option.Value;
 }
Ejemplo n.º 13
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="option"></param>
 public DefaultDbContext(DbContextOption option)
 {
     if (option == null)
     {
         throw new ArgumentNullException(nameof(option));
     }
     if (string.IsNullOrEmpty(option.ConnectionString))
     {
         throw new ArgumentNullException(nameof(option.ConnectionString));
     }
     if (string.IsNullOrEmpty(option.ModelAssemblyName))
     {
         throw new ArgumentNullException(nameof(option.ModelAssemblyName));
     }
     _option = option;
 }
        private static void AddRouteDb(string defaultContainer, string defaultDbConnectionString, string defaultProviderName, ILoggerFactory loggerFactory = null)
        {
            var option = new DbContextOption
            {
                Container        = defaultContainer,
                ConnectionString = defaultDbConnectionString,
                Provider         = defaultProviderName,
                LogggerFactory   = loggerFactory
            };

            var list = option.DbContextOption(GetDefRouteDbContextOptions(option));

            if (list.Count() > 0)
            {
                DbConfiguration.Configure(list);
            }
        }
Ejemplo n.º 15
0
        public IActionResult GetDataTables([FromBody] GenerateOption input)
        {
            try
            {
                var option = new DbContextOption()
                {
                    ConnectionString = input.ConnectionString,
                    IsOutputSql      = true
                };
                var dbContext = GetDbContext(input.DbType, option);
                var tables    = dbContext.GetCurrentDatabaseTableList().Where(m => m.Columns.Any(n => n.IsPrimaryKey)).ToList();

                tables?.ForEach(x =>
                {
                    if (!input.KeepPrefix && !input.Prefixes.IsNullOrWhiteSpace())
                    {
                        var prefixes = input.Prefixes.Split(',');
                        foreach (var prefix in prefixes)
                        {
                            if (x.TableName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                            {
                                x.Alias = x.TableName.Replace(prefix, "", StringComparison.OrdinalIgnoreCase);
                                break;
                            }
                        }
                    }

                    if (input.IsPascalCase)
                    {
                        x.Alias = (x.Alias.IsNullOrEmpty() ? x.TableName : x.Alias).ToPascalCase();
                    }

                    foreach (var column in x.Columns)
                    {
                        column.Alias = input.IsPascalCase ? column.ColName.ToPascalCase() : column.ColName;
                    }
                });
                return(Json(ExcutedResult.SuccessResult(tables)));
            }
            catch (Exception e)
            {
                Log4NetHelper.WriteError(GetType(), e);
                return(Json(ExcutedResult.FailedResult($"数据库连接失败,具体原因如下:{e.Message}")));
            }
        }
Ejemplo n.º 16
0
        public IDbContext Resolve(DbContextOption option)
        {
            if (option.DbContextType != null)
            {
                return((IDbContext)ObjectHelper.CreateInstance(option.DbContextType, new object[] { option.IOMode, option.ConnectionStringName }));
            }

            switch (option.RepositoryType)
            {
            case RepositoryType.MSSQL:
                return(this.CreateDefaultDbContext(option));

            case RepositoryType.MongoDb:
                return(new App.Common.Data.MongoDB.MongoDbContext(option.IOMode, connectionName: option.ConnectionStringName));

            default:
                throw new InvalidOperationException("common.errors.unsupportedTyeOdDbContext");
            }
        }
Ejemplo n.º 17
0
 public PermissionService(DbContextOption option,
                          IRepository <SysUser, Guid> repository1,
                          IRepository <SysRole, Guid> repository2,
                          IRepository <SysClaim, Guid> repository3,
                          IRepository <UserClaim, Guid> repository4,
                          IRepository <RoleClaim, Guid> repository5,
                          IRepository <Department, Guid> repository6,
                          IRepository <DepartmentClaim, Guid> repository7,
                          IRepository <UserRole, Guid> repository8)
 {
     _option              = option;
     _userRepository      = repository1;
     _roleRepository      = repository2;
     _claimRepository     = repository3;
     _userclaimRepository = repository4;
     _roleclaimRepository = repository5;
     _deptRepository      = repository6;
     _deptclaimRepository = repository7;
     _userRoleRepository  = repository8;
 }
Ejemplo n.º 18
0
        private IServiceProvider InitIoC(IServiceCollection services)
        {
            var commandString   = Configuration.GetConnectionString("CommandDB");
            var queryString     = Configuration.GetConnectionString("QueryDB");
            var dbContextOption = new DbContextOption
            {
                CommandString = commandString,
                QueryString   = queryString
            };

            IoCContainer.Register(Configuration);   //注册配置
            IoCContainer.Register(dbContextOption); //注册数据库配置信息
            IoCContainer.Register(typeof(DapperContext));
            IoCContainer.Register(typeof(EntityFrameworkContext));
            IoCContainer.Register(typeof(DapperRepositoryBase <>).Assembly, "QueryRepository");            //注册仓储
            IoCContainer.Register(typeof(EntityFrameworkRepositoryBase <>).Assembly, "CommandRepository"); //注册仓储
            IoCContainer.Register(typeof(EntityFrameworkRepositoryBase <>), typeof(IEntityFrameworkCommandRepository <>));
            IoCContainer.Register(typeof(DapperRepositoryBase <>), typeof(IDapperQueryRepository <>));
            IoCContainer.Register(typeof(BaseService).Assembly, "Service");
            return(IoCContainer.Build(services));
        }
        protected IUnitOfWork CreateUnitOfWork <TAggregate>() where TAggregate : IBaseAggregateRoot
        {
            ILogger         logger = IoC.Container.Resolve <ILogger>();
            DbContextOption dbContextOption;
            string          aggregateName = typeof(TAggregate).FullName;
            Type            dbContextType = null;

            AggregateOption option   = Configurations.Configuration.Current.Aggregates[aggregateName];
            RepositoryType  repoType = Configurations.Configuration.Current.Repository.DefaultRepoType;

            if (option != null)
            {
                repoType      = option.RepoType;
                dbContextType = repoType == RepositoryType.MSSQL ? this.GetDbContextType <TAggregate>() : null;
            }

            if (option == null)
            {
                logger.Info("There is no for {0}, using default setting for UnitOfWork", aggregateName);
                dbContextOption = new DbContextOption(
                    IOMode.Write,
                    Configurations.Configuration.Current.Repository.DefaultRepoType,
                    connectionStringName: Configuration.Current.Repository.DefaultConnectionStringName,
                    dbContextType: dbContextType
                    );
            }
            else
            {
                dbContextOption = new DbContextOption(
                    IOMode.Write,
                    option.RepoType,
                    connectionStringName: string.IsNullOrWhiteSpace(option.ConnectionStringName) && option.RepoType == Configuration.Current.Repository.DefaultRepoType ? Configuration.Current.Repository.DefaultConnectionStringName : option.ConnectionStringName,
                    dbContextType: dbContextType
                    );
            }
            return(new UnitOfWork(dbContextOption));
        }
Ejemplo n.º 20
0
 public dbTest(ITestOutputHelper output)
 {
     _output        = output;
     _dbContext     = Ioc.DefaultContainer.GetService <IDbContextCore>();
     _contextOption = Ioc.DefaultContainer.GetService <IOptions <DbContextOption> >().Value;
 }
 public MySqlDbContext(DbContextOption option) : base(option)
 {
 }
 public OracleDbContext(DbContextOption option) : base(option)
 {
 }
Ejemplo n.º 23
0
 public MongoDbContext(DbContextOption option) : base(option)
 {
 }
Ejemplo n.º 24
0
 public PostgreSQLDbContext(DbContextOption option) : base(option)
 {
 }
 /// <summary>
 /// 注入数据库上下文
 /// </summary>
 /// <typeparam name="IT"></typeparam>
 /// <typeparam name="T"></typeparam>
 /// <param name="services"></param>
 /// <param name="option">数据库上下文配置参数</param>
 /// <returns></returns>
 public static IServiceCollection AddDbContext <IT, T>(this IServiceCollection services, DbContextOption option) where IT : IDbContextCore where T : BaseDbContext, IT
 {
     if (services == null)
     {
         throw new ArgumentNullException(nameof(services));
     }
     if (option == null)
     {
         throw new ArgumentNullException(nameof(option));
     }
     services.AddSingleton(option);
     return(services.AddDbContext <IT, T>(option));
 }
 public SqlServerDbContext(DbContextOption option) : base(option)
 {
 }
Ejemplo n.º 27
0
 protected BaseDbContext(DbContextOption option)
 {
     Option = option ?? throw new ArgumentNullException(nameof(option));
 }
Ejemplo n.º 28
0
 public static IServiceCollection AddDbContext <IT, T>(this IServiceCollection services, DbContextOption option) where IT : IDbContextCore where T : BaseDbContext, IT
 {
     if (services == null)
     {
         throw new ArgumentNullException(nameof(services));
     }
     if (option == null)
     {
         throw new ArgumentNullException(nameof(option));
     }
     services.Configure <DbContextOption>(options =>
     {
         options.IsOutputSql       = option.IsOutputSql;
         options.ConnectionString  = option.ConnectionString;
         options.ModelAssemblyName = option.ModelAssemblyName;
         options.TagName           = option.TagName;
     });
     return(services.AddDbContext <IT, T>());
 }
Ejemplo n.º 29
0
 public InMemoryDbContext(DbContextOption option) : base(option)
 {
 }
 public SQLiteDbContext(DbContextOption option) : base(option)
 {
 }