Ejemplo n.º 1
0
        public static IEnumerable <Type> GetDerivedTypes(IEnumerable <Assembly> assemblies, Type type)
        {
            MicroStrutLibraryExceptionHelper.IsNull(assemblies, typeof(ReflectionHelper).FullName, LogLevel.Error, "可用程序集为空!");
            MicroStrutLibraryExceptionHelper.IsNull(type, typeof(ReflectionHelper).FullName, LogLevel.Error, "类型为空!");

            //获取可用类型
            return(assemblies.SelectMany(assembly =>
            {
                return assembly.GetTypes().Where(t =>
                {
                    if (type == t)
                    {
                        return false;
                    }

                    TypeInfo tInfo = t.GetTypeInfo();

                    if (tInfo.IsAbstract || !tInfo.IsClass || !tInfo.IsPublic)
                    {
                        return false;
                    }

                    return type.IsAssignableFrom(t);
                });
            }));
        }
Ejemplo n.º 2
0
        public DbContextOptionsBuilder UseDb(DbContextOptionsBuilder builder, DbConnectionStringSettings connectionStringSettings, DbConnectionProviderSettings connectionProviderSettings)
        {
            MicroStrutLibraryExceptionHelper.IsNull(connectionStringSettings, typeof(SqlServerDbContextOptionsBuilderProvider).FullName, LogLevel.Error, "链接字符串配置为空!");
            MicroStrutLibraryExceptionHelper.IsNull(connectionProviderSettings, typeof(SqlServerDbContextOptionsBuilderProvider).FullName, LogLevel.Error, "链接提供者配置为空!");

            return(builder.UseSqlite(connectionStringSettings.ConnectionString));
        }
Ejemplo n.º 3
0
        private static IEnumerable <Assembly> GetReferencedAssemblies(IAssemblyFinder finder, Type type)
        {
            MicroStrutLibraryExceptionHelper.IsNull(finder, typeof(ReflectionHelper).FullName, LogLevel.Error, "类型查找器为空!");
            MicroStrutLibraryExceptionHelper.IsNull(type, typeof(ReflectionHelper).FullName, LogLevel.Error, "类型为空!");

            Assembly typeAssembly = type.GetTypeInfo().Assembly;

            return(finder.GetAssemblies().Where(assembly =>
            {
                if (assembly.IsDynamic)
                {
                    return false;
                }

                if (assembly.FullName == typeAssembly.FullName)
                {
                    return true;
                }

                return assembly.GetReferencedAssemblies().Any(assemblyName =>
                {
                    return assemblyName.FullName == typeAssembly.FullName;
                });
            }));
        }
        public static DbContextOptionsBuilder UseDb(this DbContextOptionsBuilder builder, DbConnectionConfiguration configuration, string dbName)
        {
            MicroStrutLibraryExceptionHelper.IsNull(configuration, typeof(DbContextOptionsBuilderExtensions).FullName, LogLevel.Error, "配置为空!");
            MicroStrutLibraryExceptionHelper.IsNullOrEmpty(dbName, typeof(DbContextOptionsBuilderExtensions).FullName, LogLevel.Error, "链接数据库为空!");

            DbConnectionStringSettings connectionStringSettings = configuration.ConnectionStrings.SingleOrDefault(s => s.Name == dbName);

            MicroStrutLibraryExceptionHelper.IsNull(connectionStringSettings, typeof(DbContextOptionsBuilderExtensions).FullName, LogLevel.Error, $"链接数据库{dbName}对应的配置节为空!");
            DbConnectionProviderSettings providerSettings = configuration.Providers.SingleOrDefault(s => s.Name == connectionStringSettings.ProviderName);

            MicroStrutLibraryExceptionHelper.IsNull(providerSettings, typeof(DbContextOptionsBuilderExtensions).FullName, LogLevel.Error, $"链接数据库{dbName}对应的提供者配置为空!");

            Type providerType = Type.GetType(providerSettings.Type);
            IDbContextOptionsBuilderProvider provider;

            if (string.IsNullOrEmpty(providerSettings.Parameter))
            {
                provider = (IDbContextOptionsBuilderProvider)Activator.CreateInstance(providerType);
            }
            else
            {
                provider = (IDbContextOptionsBuilderProvider)Activator.CreateInstance(providerType, providerSettings.Parameter);
            }
            return(provider.UseDb(builder, connectionStringSettings, providerSettings));
        }
        /// <summary>
        /// 得到枚举类型的dictionary
        /// </summary>
        /// <param name="enumTypeStr">要获取的枚举类型的名称。</param>
        /// <param name="useEnumString">使用枚举的string值</param>
        /// <returns></returns>
        public static Dictionary <string, string> ToDictionary(string enumTypeStr, bool useEnumString = true)
        {
            Type t = Type.GetType(enumTypeStr);

            MicroStrutLibraryExceptionHelper.IsNull(t, typeof(EnumTypeHelper).Namespace, LogLevel.Error, "枚举类型配置错误(系统类型可以通过GetType().AssemblyQualifiedName获得):" + enumTypeStr);

            return(ToDictionary(t, useEnumString));
        }
        public static DbContextOptionsBuilder UseDb <TContext>(this DbContextOptionsBuilder builder, DbConnectionConfiguration configuration) where TContext : DbContextBase
        {
            var attribute = typeof(TContext).GetTypeInfo().GetCustomAttribute <EntityDbNameAttribute>(false);

            MicroStrutLibraryExceptionHelper.IsNull(attribute, typeof(DbContextOptionsBuilderExtensions).FullName, LogLevel.Error, "名称属性为空!");

            return(UseDb(builder, configuration, attribute.Name));
        }
        private static IConfigurationSection GetSectionByEntry(IConfigurationRoot root, Type type)
        {
            MicroStrutLibraryExceptionHelper.IsNull(type, typeof(IConfigurationRootExtensions).FullName, LogLevel.Error, $"类型为空!");

            ConfigurationEntryAttribute attribute = type.GetTypeInfo().GetCustomAttribute <ConfigurationEntryAttribute>(false);

            MicroStrutLibraryExceptionHelper.IsNull(attribute, typeof(IConfigurationRootExtensions).FullName, LogLevel.Error, $"属性类型为空!");

            return(root.GetSection(attribute.Path));
        }
Ejemplo n.º 8
0
        public DbContextOptionsBuilder UseDb(DbContextOptionsBuilder builder, DbConnectionStringSettings connectionStringSettings, DbConnectionProviderSettings connectionProviderSettings)
        {
            MicroStrutLibraryExceptionHelper.IsNull(connectionStringSettings, typeof(SqlServerDbContextOptionsBuilderProvider).FullName, LogLevel.Error, "链接字符串配置为空!");
            MicroStrutLibraryExceptionHelper.IsNull(connectionProviderSettings, typeof(SqlServerDbContextOptionsBuilderProvider).FullName, LogLevel.Error, "链接提供者配置为空!");

            //SQLServer 2008 R2以以下版本
            builder.UseSqlServer(connectionStringSettings.ConnectionString, optionBuilder => optionBuilder.UseRowNumberForPaging());

            return(builder.UseSqlServer(connectionStringSettings.ConnectionString));
        }
Ejemplo n.º 9
0
        public static List <Tuple <string, string, Type> > GetTypeNameList(IAssemblyFinder finder, Type type)
        {
            MicroStrutLibraryExceptionHelper.IsNull(finder, typeof(ReflectionHelper).FullName, LogLevel.Error, "类型查找器为空!");
            MicroStrutLibraryExceptionHelper.IsNull(type, typeof(ReflectionHelper).FullName, LogLevel.Error, "类型为空!");

            //获取可用程序集
            IEnumerable <Assembly> assemblies = GetReferencedAssemblies(finder, type);

            //获取可用类型
            return(GetTypeNameList(assemblies, type));
        }
        public static object GetByEntry(this IConfigurationRoot root, Type type)
        {
            MicroStrutLibraryExceptionHelper.IsNull(type, typeof(IConfigurationRootExtensions).FullName, LogLevel.Error, $"类型为空!");

            ConfigurationEntryAttribute attribute = type.GetTypeInfo().GetCustomAttribute <ConfigurationEntryAttribute>(false);

            MicroStrutLibraryExceptionHelper.IsNull(attribute, typeof(IConfigurationRootExtensions).FullName, LogLevel.Error, $"属性类型为空!");

            var data = root.GetSection(attribute.Path).Get(type);

            if (data == null && attribute.Default)
            {
                data = Activator.CreateInstance(type);
            }

            return(data);
        }
        public static IServiceCollection AddCaching(this IServiceCollection services, IConfigurationRoot root)
        {
            MicroStrutLibraryExceptionHelper.IsNull(root, typeof(IServiceCollectionExtensions).FullName, LogLevel.Error, "根配置为空!");

            IServiceProvider provider = services.BuildServiceProvider();
            IOptions <CacheConfiguration> cacheConfigurationOptions = provider.GetRequiredService <IOptions <CacheConfiguration> >();

            Dictionary <string, Type> registerTypes = GetRegisterTypes(provider);

            ICacheRegister memoryCacheInstance = GetMemoryCacheInstance(provider, registerTypes, cacheConfigurationOptions);

            memoryCacheInstance.Configure(services);

            ICacheRegister distributedCacheInstance = GetDistributedCacheInstance(provider, registerTypes, cacheConfigurationOptions);

            distributedCacheInstance.Configure(services);

            return(services);
        }
        public void Save(string appCode, string fileDirectory, string fileName, Stream stream)
        {
            MicroStrutLibraryExceptionHelper.TrueThrow(stream.Length > options.Value.MaxContentSize, this.GetType().FullName, LogLevel.Error, $"{fileName}文件大小超出{options.Value.MaxContentSize}字节的长度限制。");

            string dir = this.GetDirectory(appCode);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (!Directory.Exists(Path.Combine(dir, fileDirectory)))
            {
                Directory.CreateDirectory(Path.Combine(dir, fileDirectory));
            }

            string path = Path.Combine(dir, fileDirectory, fileName);

            using (var fileStream = File.Create(path))
            {
                stream.CopyTo(fileStream);
            }
        }
        public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfigurationRoot root)
        {
            MicroStrutLibraryExceptionHelper.IsNull(root, typeof(IServiceCollectionExtensions).FullName, LogLevel.Error, "根配置为空!");

            services.AddSingleton <IConfigurationRoot>(root);

            IServiceProvider provider = services.BuildServiceProvider();

            IAssemblyFinder assemblyFinder = provider.GetRequiredService <IAssemblyFinder>();

            var registerType      = typeof(IConfigurationRegister);
            var registerInstances = ReflectionHelper.GetDerivedTypes(assemblyFinder, registerType).Select(type =>
            {
                return((IConfigurationRegister)Activator.CreateInstance(type));
            });

            foreach (var register in registerInstances.ToList())
            {
                register.Register(services, root);
            }

            return(services);
        }
Ejemplo n.º 14
0
        public override void Load()
        {
            MicroStrutLibraryExceptionHelper.IsNull(this.config.TableName, this.GetType().FullName, LogLevel.Error, "TableName配置为空。");
            MicroStrutLibraryExceptionHelper.IsNull(this.config.ConnectionString, this.GetType().FullName, LogLevel.Error, "ConnectionString。");
            MicroStrutLibraryExceptionHelper.IsNull(this.config.ConnectionProvider, this.GetType().FullName, LogLevel.Error, "ConnectionProvider。");

            var connectionProviderType     = Type.GetType(this.config.ConnectionProvider.Type);
            var connectionProviderInstance = (IDbFactoryProvider)Activator.CreateInstance(connectionProviderType);
            var connectionProviderFactory  = connectionProviderInstance.GetFactory(this.config.ConnectionProvider.Parameter);

            List <DbConfigurationInfo> list = new List <DbConfigurationInfo>();

            using (DbConnection connection = connectionProviderFactory.CreateConnection())
            {
                var command = connection.CreateCommand();

                command.CommandText = $"SELECT APP_CODE AS {nameof(DbConfigurationInfo.AppCode)},CONFIG_CODE AS {nameof(DbConfigurationInfo.ConfigCode)},CONFIG_CONTENT AS {nameof(DbConfigurationInfo.ConfigContent)} FROM {this.config.TableName} WHERE  APP_CODE = @emtpyCode OR APP_CODE = @appCode ORDER BY APP_CODE DESC";

                var emptyParameter = command.CreateParameter();

                emptyParameter.ParameterName = "emtpyCode";
                emptyParameter.Value         = "";

                var appParameter = command.CreateParameter();

                appParameter.ParameterName = "appCode";
                appParameter.Value         = this.config.AppCode ?? "";

                command.Parameters.Add(emptyParameter);
                command.Parameters.Add(appParameter);

                //Open
                connection.ConnectionString = this.config.ConnectionString.ConnectionString;

                connection.Open();

                using (var reader = command.ExecuteReader())
                {
                    var appCodeIndex       = reader.GetOrdinal(nameof(DbConfigurationInfo.AppCode));
                    var configCodeIndex    = reader.GetOrdinal(nameof(DbConfigurationInfo.ConfigCode));
                    var configContentIndex = reader.GetOrdinal(nameof(DbConfigurationInfo.ConfigContent));

                    while (reader.Read())
                    {
                        //此处约定,特定优于一般,因此后面来的一般不予考虑
                        if (!list.Exists(conf => conf.ConfigCode == reader.GetString(configCodeIndex)))
                        {
                            list.Add(new DbConfigurationInfo
                            {
                                AppCode       = reader.GetString(appCodeIndex),
                                ConfigCode    = reader.GetString(configCodeIndex),
                                ConfigContent = reader.GetString(configContentIndex)
                            });
                        }
                    }
                }
            }

            //转换
            this.Data = new JsonConfigurationParser().Parse(list.ToDictionary(o => o.ConfigCode, o => o.ConfigContent));
        }
        public static IConfigurationBuilder AddDbSource(this IConfigurationBuilder builder, IOptions <DbSourceConfiguration> options)
        {
            MicroStrutLibraryExceptionHelper.IsNull(options, typeof(IConfigurationBuilderExtensions).FullName, LogLevel.Error, "配置为空!");

            return(builder.Add(new DbConfigurationSource(options.Value)));
        }