Ejemplo n.º 1
0
        /// <summary>
        /// 添加SQLServer数据库服务。
        /// </summary>
        /// <param name="builder">构建服务实例。</param>
        /// <param name="options">数据源选项。</param>
        /// <returns>返回服务集合实例。</returns>
        public static IServiceBuilder AddSqlServer(this IServiceBuilder builder, Action <DatabaseOptions> options)
        {
            Check.NotNull(builder, nameof(builder));
            Check.NotNull(options, nameof(options));
            var source = new DatabaseOptions();

            options(source);

            return(builder.AddServices(services => services
                                       .AddSingleton <IDatabase, SqlServerDatabase>()
                                       .Configure <DatabaseOptions>(o =>
            {
                o.ConnectionString = source.ConnectionString;
                o.Prefix = source.Prefix?.Trim();
                o.Provider = "SqlServer";
            })
                                       .AddSingleton(typeof(IDbContext <>), typeof(DbContext <>))
                                       .AddTransient <IDataMigrator, DataMigrator>()
                                       .AddTransient <IMigrationRepository, SqlServerMigrationRepository>()
                                       .AddTransient <IMigrationsSqlGenerator, SqlServerMigrationsSqlGenerator>()
                                       .AddSingleton <IQuerySqlGenerator, SqlServerQuerySqlGenerator>()
                                       .AddSingleton <ITypeMapper, SqlServerTypeMapper>()
                                       .AddSingleton <ISqlHelper, SqlServerHelper>()
                                       .AddSingleton <IMemberTranslator, SqlServerCompositeMemberTranslator>()
                                       .AddSingleton <IMethodCallTranslator, SqlServerCompositeMethodCallTranslator>()
                                       .AddSingleton <IExpressionFragmentTranslator, SqlServerCompositeExpressionFragmentTranslator>()
                                       .AddSingleton <IExpressionVisitorFactory, SqlServerExpressionVisitorFactory>()));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 添加后台服务。
 /// </summary>
 /// <param name="builder">服务构建实例。</param>
 /// <returns>服务构建实例。</returns>
 public static IServiceBuilder AddTaskServices(this IServiceBuilder builder) =>
 builder.AddServices(services =>
 {
     services.TryAddEnumerable(ServiceDescriptor.Transient <IDataMigration, TaskDataMigration>());
     services.TryAddEnumerable(ServiceDescriptor.Transient <IHostedService, TaskHostedService>());
     services.AddSingleton <ITaskManager, TaskManager>();
 });
Ejemplo n.º 3
0
 /// <summary>
 /// 添加文件存储服务。
 /// </summary>
 /// <param name="builder">服务构建实例。</param>
 /// <returns>返回服务构建实例。</returns>
 public static IServiceBuilder AddMediaStorages(this IServiceBuilder builder)
 {
     return(builder.AddServices(services =>
     {
         services.AddSingleton <IMediaDirectory, DefaultMediaDirectory>();
         services.AddTransient <IDataMigration, DefaultMediaDataMigration>();
     }));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 附加上模型特性实例。
        /// </summary>
        /// <typeparam name="TModelUIAttribute">模型特性类型。</typeparam>
        /// <typeparam name="TModel">模型类型。</typeparam>
        /// <param name="builder">服务构建实例对象。</param>
        /// <param name="action">Razor页面转换操作方法。</param>
        /// <returns>返回服务构建实例对象。</returns>
        public static IServiceBuilder AddModelUI <TModelUIAttribute, TModel>(this IServiceBuilder builder, Action <PageConventionCollection, IPageApplicationModelConvention> action)
            where TModel : class
            where TModelUIAttribute : ModelUIAttribute
        {
            var convention = new ModelUIConvention <TModelUIAttribute, TModel>();

            return(builder.AddServices(services => services.PostConfigureAll <RazorPagesOptions>(options => action(options.Conventions, convention))));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 配置服务方法。
 /// </summary>
 /// <param name="builder">容器构建实例。</param>
 public void ConfigureServices(IServiceBuilder builder)
 {
     builder.AddServices(services =>
     {
         var identityBuilder = services.AddIdentity <TUser, TRole>();
         ConfigureIdentityServices(identityBuilder);
     });
     ConfigureIdentityServices(builder);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 配置服务方法。
 /// </summary>
 /// <param name="builder">容器构建实例。</param>
 public void ConfigureServices(IServiceBuilder builder)
 {
     builder.AddServices(services =>
     {
         services.AddIdentity <TUser, TRole>();
         ConfigureIdentityServices(new IdentityBuilder(typeof(TUser), typeof(TRole), services));
     });
     ConfigureIdentityServices(builder);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 添加通知服务。
 /// </summary>
 /// <param name="builder">服务构建实例。</param>
 /// <returns>返回构建实例。</returns>
 public static IServiceBuilder AddNotification(this IServiceBuilder builder)
 {
     return(builder.AddServices(services =>
     {
         services.AddSingleton <INotificationManager, DefaultNotificationManager>();
         services.AddSingleton <INotifier, DefaultNotifier>();
         services.TryAddEnumerable(ServiceDescriptor.Singleton <ITaskService, DefaultNotificationTaskService>());
         services.TryAddEnumerable(ServiceDescriptor.Transient <IDataMigration, DefaultNotificationDataMigration>());
     }));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 添加字典组件。
 /// </summary>
 /// <typeparam name="TSettingDictionaryManager">字典实现类。</typeparam>
 /// <param name="builder">服务构建实例。</param>
 /// <returns>服务构建实例。</returns>
 public static IServiceBuilder AddSettingDictionary <TSettingDictionaryManager>(this IServiceBuilder builder)
     where TSettingDictionaryManager : class, ISettingDictionaryManager
 {
     return(builder.AddServices(services =>
     {
         services.TryAddEnumerable(ServiceDescriptor
                                   .Transient <IDataMigration, SettingDictionaryDataMigration>());
         services.TryAddSingleton <ISettingDictionaryManager, TSettingDictionaryManager>();
     }));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// 添加权限模块。
 /// </summary>
 /// <typeparam name="TRole">角色类型。</typeparam>
 /// <typeparam name="TUserRole">用户角色类型。</typeparam>
 /// <param name="builder">服务构建实例。</param>
 /// <returns>返回服务构建实例。</returns>
 public static IServiceBuilder AddPermissions <TRole, TUserRole>(this IServiceBuilder builder)
     where TUserRole : IUserRole
     where TRole : RoleBase
 {
     return(builder.AddServices(services =>
     {
         services.TryAddEnumerable(ServiceDescriptor.Transient <IDataMigration, DefaultPermissionDataMigration <TRole> >());
         services.AddSingleton <IAuthorizationHandler, PermissionAuthorizationHandler>();
         services.AddSingleton <IPermissionManager, DefaultPermissionManager <TRole, TUserRole> >();
     }));
 }
Ejemplo n.º 10
0
 private static void BuildServices(IServiceBuilder builder, IEnumerable <Type> exportedTypes)
 {
     builder.AddServices(services =>
     {
         foreach (var source in exportedTypes)
         {
             if (typeof(IServiceConfigurer).IsAssignableFrom(source))
             {
                 var service = Activator.CreateInstance(source) as IServiceConfigurer;
                 service?.ConfigureServices(builder);
             }
             else if (typeof(IHostedService).IsAssignableFrom(source))
             {
                 //后台任务
                 services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), source));
             }
             else //注册类型
             {
                 var interfaceTypes = source.GetInterfaces()
                                      .Where(itf => typeof(IService).IsAssignableFrom(itf));
                 foreach (var interfaceType in interfaceTypes)
                 {
                     if (typeof(ISingletonService).IsAssignableFrom(interfaceType))
                     {
                         services.TryAddSingleton(interfaceType, source);
                     }
                     else if (typeof(IScopedService).IsAssignableFrom(interfaceType))
                     {
                         services.TryAddScoped(interfaceType, source);
                     }
                     else if (typeof(ISingletonServices).IsAssignableFrom(interfaceType))
                     {
                         services.TryAddEnumerable(ServiceDescriptor.Singleton(interfaceType, source));
                     }
                     else if (typeof(IScopedServices).IsAssignableFrom(interfaceType))
                     {
                         services.TryAddEnumerable(ServiceDescriptor.Scoped(interfaceType, source));
                     }
                     else if (typeof(IServices).IsAssignableFrom(interfaceType))
                     {
                         services.TryAddEnumerable(ServiceDescriptor.Transient(interfaceType, source));
                     }
                     else
                     {
                         services.TryAddTransient(interfaceType, source);
                     }
                 }
             }
         }
     });
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 添加字典组件。
 /// </summary>
 /// <param name="builder">服务构建实例。</param>
 /// <param name="includeDictionary">是否组成字典实例。</param>
 /// <returns>服务构建实例。</returns>
 public static IServiceBuilder AddSettings(this IServiceBuilder builder, bool includeDictionary = false)
 {
     builder.AddServices(services =>
     {
         services.TryAddEnumerable(ServiceDescriptor
                                   .Transient <IDataMigration, SettingsDataMigration>());
         services.TryAddSingleton <ISettingsManager, SettingsManager>();
     });
     if (includeDictionary)
     {
         builder.AddSettings <SettingDictionaryManager>();
     }
     return(builder);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// 配置服务方法。
 /// </summary>
 /// <param name="builder">容器构建实例。</param>
 public virtual void ConfigureServices(IServiceBuilder builder)
 {
     builder.AddServices(services =>
     {
         services.AddIdentity <TUser, TRole>();
         services.AddScoped(sp => sp.GetRequiredService <IHttpContextAccessor>().HttpContext.GetUser <TUser>() ?? _anonymous);
         services.AddScoped(service => service.GetRequiredService <ISettingsManager>().GetSettings <TSettings>());
         services.AddScoped(service => service.GetRequiredService <ISettingsManager>().GetSettings <ScoreSettings>());
         services.AddSingleton <IPermissionManager, PermissionManager <TRole, TUserRole> >();
         ConfigureServices(new IdentityBuilder(typeof(TUser), typeof(TRole), services));
         ConfigureCookieServices(services, builder.Configuration.GetSection("Cookies"));
     })
     .AddTransients <IDataMigration, DefaultPermissionDataMigration <TRole> >();
 }
Ejemplo n.º 13
0
        /// <summary>
        /// 添加数据加密密钥服务。
        /// </summary>
        /// <param name="builder">服务器集合。</param>
        /// <param name="directory">存储文件夹。</param>
        /// <returns>返回服务器接口集合。</returns>
        public static IServiceBuilder AddDataProtection(this IServiceBuilder builder, string directory = "../storages/keys")
        {
            DirectoryInfo info;

            try
            {
                info = new DirectoryInfo(directory);
            }
            catch
            {
                directory = Path.Combine(Directory.GetCurrentDirectory(), directory);
                info      = new DirectoryInfo(directory);
            }

            return(builder.AddServices(services => services.AddDataProtection()
                                       .PersistKeysToFileSystem(info)
                                       .ProtectKeysWithDpapi()));
        }
Ejemplo n.º 14
0
 /// <summary>
 /// 添加数据库迁移服务。
 /// </summary>
 /// <param name="builder">服务构建实例。</param>
 /// <returns>服务构建实例。</returns>
 public static IServiceBuilder AddDataMigration(this IServiceBuilder builder)
 {
     return(builder.AddServices(services =>
                                services.TryAddEnumerable(ServiceDescriptor.Transient <IHostedService, MigrationBackgroundService>())));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// 添加静态资源目录。
 ///
 /// 注意:
 /// 1.资源目录为wwwroot,项目只能有一个wwwroot目录,为了不和其他程序集冲突,在wwwroot目录下文件夹最好和Areas目录下的文件夹一样。
 /// 2.由于VS打包资源文件会把目录非法字符转换为“_”,如:“lib/font-awesome/css/font-awesome.min.css”将被转换为“lib/font_awesome/css/font-awesome.min.css”,
 ///   所以尽量不要在文件夹上面使用非字母名称。
 /// </summary>
 /// <typeparam name="TAssemblyResourceType">程序集资源类型。</typeparam>
 /// <param name="builder">服务集合。</param>
 /// <returns>服务集合。</returns>
 /// <remarks>
 /// 1.编辑“.csproj”项目文件,添加以下代码段(将文件夹设置为嵌入资源):
 /// <code>
 /// <![CDATA[
 ///  <ItemGroup>
 ///    <EmbeddedResource Include = "wwwroot\**" />
 ///  </ItemGroup>
 /// ]]>
 /// </code>
 /// 2.实现接口<see cref="IServiceConfigurer"/>,调用本方法注册。
 /// </remarks>
 public static IServiceBuilder AddResources <TAssemblyResourceType>(this IServiceBuilder builder)
 {
     return(builder.AddServices(services => services.ConfigureOptions(typeof(ResourceOptions <TAssemblyResourceType>))));
 }