/// <summary> /// 添加SQLServer数据库服务。 /// </summary> /// <param name="builder">构建服务实例。</param> /// <param name="options">数据源选项。</param> /// <returns>返回服务集合实例。</returns> public static IMozliteBuilder AddSqlServer(this IMozliteBuilder 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>())); }
/// <summary> /// 添加MVC服务。 /// </summary> /// <param name="builder">当前服务集合构建实例。</param> /// <param name="action">MVC选项实例化方法。</param> /// <returns>返回MVC构建实例。</returns> public static IMozliteBuilder AddMozliteMvc(this IMozliteBuilder builder, Action <IMvcBuilder> action = null) { return(builder.AddServices(services => { //防止添加用户活动日志的时候未注册HTTP上下文实例。 services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); var mvc = services .AddMvc() .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization() .AddRazorOptions(options => { //网站的程序集名称,约定扩展程序集名称必须为“网站程序集名称.Extensions.当前扩展区域名称” var assemblyName = Assembly.GetEntryAssembly().GetName().Name; ViewLocation(options, assemblyName); PageLocation(options); }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); action?.Invoke(mvc); })); }
/// <summary> /// 添加语言支持。 /// </summary> /// <returns>返回服务构建实例。</returns> /// <param name="builder">服务构建实例。</param> public static IMozliteBuilder AddSupportedCultures(this IMozliteBuilder builder) { var defaultLanguage = builder.Configuration["Cultures:Default"] ?? "zh-CN"; var supportedCultures = builder.Configuration.GetSupportedCultures().Keys.Select(x => new CultureInfo(x)).ToArray(); return(builder.AddServices(services => { services.AddLocalization(options => options.ResourcesPath = "Resources"); services.Configure <RequestLocalizationOptions>(options => { options.DefaultRequestCulture = new RequestCulture(defaultLanguage); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.RequestCultureProviders = new List <IRequestCultureProvider> { new QueryStringRequestCultureProvider(), new CookieRequestCultureProvider() }; }); })); }