public static Setup ( Action |
||
configCallback | Action |
|
Результат | void |
public static void UseDbLocalizationProvider(this IAppBuilder builder, Action <ConfigurationContext> setup = null) { // setup default implementations ConfigurationContext.Current.TypeFactory.ForQuery <AvailableLanguages.Query>().SetHandler <AvailableLanguages.Handler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetTranslation.Query>().SetHandler <GetTranslation.Handler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetAllResources.Query>().SetHandler <GetAllResources.Handler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetAllTranslations.Query>().SetHandler <GetAllTranslations.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <CreateNewResource.Command>().SetHandler <CreateNewResource.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <DeleteResource.Command>().SetHandler <DeleteResource.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <CreateOrUpdateTranslation.Command>().SetHandler <CreateOrUpdateTranslation.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <ClearCache.Command>().SetHandler <ClearCache.Handler>(); if (setup != null) { ConfigurationContext.Setup(setup); } var synchronizer = new ResourceSynchronizer(); synchronizer.DiscoverAndRegister(); }
public static IAppBuilder UseDbLocalizationProvider(this IAppBuilder builder, Action <ConfigurationContext> setup = null) { // setup default implementations ConfigurationContext.Current.TypeFactory.ForQuery <AvailableLanguages.Query>().SetHandler <AvailableLanguages.Handler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetTranslation.Query>().SetHandler <GetTranslation.Handler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetAllResources.Query>().SetHandler <GetAllResources.Handler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetAllTranslations.Query>().SetHandler <GetAllTranslations.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <CreateNewResource.Command>().SetHandler <CreateNewResource.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <DeleteResource.Command>().SetHandler <DeleteResource.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <CreateOrUpdateTranslation.Command>().SetHandler <CreateOrUpdateTranslation.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <ClearCache.Command>().SetHandler <ClearCache.Handler>(); if (setup != null) { ConfigurationContext.Setup(setup); } var synchronizer = new ResourceSynchronizer(); synchronizer.DiscoverAndRegister(); // set model metadata providers if (ConfigurationContext.Current.ModelMetadataProviders.ReplaceProviders) { // set current provider if (ModelMetadataProviders.Current == null) { if (ConfigurationContext.Current.ModelMetadataProviders.UseCachedProviders) { ModelMetadataProviders.Current = new CachedLocalizedMetadataProvider(); } else { ModelMetadataProviders.Current = new LocalizedMetadataProvider(); } } else { if (ConfigurationContext.Current.ModelMetadataProviders.UseCachedProviders) { ModelMetadataProviders.Current = new CompositeModelMetadataProvider <CachedLocalizedMetadataProvider>(ModelMetadataProviders.Current); } else { ModelMetadataProviders.Current = new CompositeModelMetadataProvider <LocalizedMetadataProvider>(ModelMetadataProviders.Current); } } for (var i = 0; i < ModelValidatorProviders.Providers.Count; i++) { var provider = ModelValidatorProviders.Providers[i]; if (!(provider is DataAnnotationsModelValidatorProvider)) { continue; } ModelValidatorProviders.Providers.RemoveAt(i); ModelValidatorProviders.Providers.Insert(i, new LocalizedModelValidatorProvider()); break; } } return(builder); }
public static IAppBuilder UseDbLocalizationProvider(this IAppBuilder builder, Action <ConfigurationContext> setup = null) { // setup default implementations ConfigurationContext.Current.TypeFactory.ForQuery <AvailableLanguages.Query>().SetHandler <AvailableLanguagesHandler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetTranslation.Query>().SetHandler <GetTranslationHandler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetAllResources.Query>().SetHandler <GetAllResourcesHandler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetAllResources.Query>().DecorateWith <CachedGetAllResourcesHandler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetResource.Query>().SetHandler <GetResourceHandler>(); ConfigurationContext.Current.TypeFactory.ForQuery <GetAllTranslations.Query>().SetHandler <GetAllTranslationsHandler>(); ConfigurationContext.Current.TypeFactory.ForQuery <DetermineDefaultCulture.Query>().SetHandler <DetermineDefaultCulture.Handler>(); ConfigurationContext.Current.TypeFactory.ForCommand <CreateNewResource.Command>().SetHandler <CreateNewResourceHandler>(); ConfigurationContext.Current.TypeFactory.ForCommand <DeleteResource.Command>().SetHandler <DeleteResourceHandler>(); ConfigurationContext.Current.TypeFactory.ForCommand <RemoveTranslation.Command>().SetHandler <RemoveTranslationHandler>(); ConfigurationContext.Current.TypeFactory.ForCommand <CreateOrUpdateTranslation.Command>().SetHandler <CreateOrUpdateTranslationHandler>(); ConfigurationContext.Current.TypeFactory.ForCommand <ClearCache.Command>().SetHandler <ClearCacheHandler>(); ConfigurationContext.Current.CacheManager = new HttpCacheManager(); if (setup != null) { ConfigurationContext.Setup(setup); } ConfigurationContext.Current.DbContextConnectionString = ConfigurationManager.ConnectionStrings[ConfigurationContext.Current.Connection].ConnectionString; var synchronizer = new ResourceSynchronizer(); synchronizer.DiscoverAndRegister(); // set model metadata providers if (ConfigurationContext.Current.ModelMetadataProviders.ReplaceProviders) { // set current provider if (ModelMetadataProviders.Current == null) { if (ConfigurationContext.Current.ModelMetadataProviders.UseCachedProviders) { ModelMetadataProviders.Current = new CachedLocalizedMetadataProvider(); } else { ModelMetadataProviders.Current = new LocalizedMetadataProvider(); } } else { if (ConfigurationContext.Current.ModelMetadataProviders.UseCachedProviders) { ModelMetadataProviders.Current = new CompositeModelMetadataProvider <CachedLocalizedMetadataProvider>(ModelMetadataProviders.Current); } else { ModelMetadataProviders.Current = new CompositeModelMetadataProvider <LocalizedMetadataProvider>(ModelMetadataProviders.Current); } } for (var i = 0; i < ModelValidatorProviders.Providers.Count; i++) { var provider = ModelValidatorProviders.Providers[i]; if (!(provider is DataAnnotationsModelValidatorProvider)) { continue; } ModelValidatorProviders.Providers.RemoveAt(i); ModelValidatorProviders.Providers.Insert(i, new LocalizedModelValidatorProvider()); break; } } // in cases when there has been already a call to LoclaizationProvider.Current (some static weird things) // and only then setup configuration is ran - here we need to reset instance once again with new settings LocalizationProvider.Initialize(); return(builder); }
/// <summary> /// This si the method you are likely to call if you want to use LocalizationProvider in your ASP.NET MVC application. /// </summary> /// <param name="builder">AppBuilder instance</param> /// <param name="setup">Your custom setup lambda</param> /// <returns>The same app builder instance to support chaining</returns> public static IAppBuilder UseDbLocalizationProvider(this IAppBuilder builder, Action <ConfigurationContext> setup = null) { var sw = new Stopwatch(); sw.Start(); var ctx = ConfigurationContext.Current; // setup default implementations ctx.TypeFactory.ForQuery <AvailableLanguages.Query>().SetHandler <DefaultAvailableLanguagesHandler>(); ctx.TypeFactory.ForQuery <GetAllResources.Query>().DecorateWith <CachedGetAllResourcesHandler>(); ctx.TypeFactory.ForQuery <GetAllTranslations.Query>().SetHandler <GetAllTranslationsHandler>(); ctx.TypeFactory.ForQuery <DetermineDefaultCulture.Query>().SetHandler <DetermineDefaultCulture.Handler>(); ctx.TypeFactory.ForCommand <ClearCache.Command>().SetHandler <ClearCacheHandler>(); ctx.CacheManager = new HttpCacheManager(); // custom callback invoke here (before rest of the config is finished) if (setup != null) { ConfigurationContext.Setup(setup); } // also we need to make sure that invariant culture is last in the list of fallback to invariant is true if (ctx.EnableInvariantCultureFallback) { ctx.FallbackCultures.Then(CultureInfo.InvariantCulture); } // if we need to sync - then it's good time to do it now var sync = new Synchronizer(); sync.SyncResources(ctx.DiscoverAndRegisterResources); // set model metadata providers if (ctx.ModelMetadataProviders.ReplaceProviders) { if (ctx.ModelMetadataProviders.SetupCallback != null) { ctx.ModelMetadataProviders.SetupCallback(); } else { // set current provider if (ModelMetadataProviders.Current == null) { if (ctx.ModelMetadataProviders.UseCachedProviders) { ModelMetadataProviders.Current = new CachedLocalizedMetadataProvider(); } else { ModelMetadataProviders.Current = new LocalizedMetadataProvider(); } } else { if (ctx.ModelMetadataProviders.UseCachedProviders) { ModelMetadataProviders.Current = new CompositeModelMetadataProvider <CachedLocalizedMetadataProvider>(ModelMetadataProviders.Current); } else { ModelMetadataProviders.Current = new CompositeModelMetadataProvider <LocalizedMetadataProvider>(ModelMetadataProviders.Current); } } for (var i = 0; i < ModelValidatorProviders.Providers.Count; i++) { var provider = ModelValidatorProviders.Providers[i]; if (!(provider is DataAnnotationsModelValidatorProvider)) { continue; } ModelValidatorProviders.Providers.RemoveAt(i); ModelValidatorProviders.Providers.Insert(i, new LocalizedModelValidatorProvider()); break; } } } sw.Stop(); ctx.Logger?.Debug($"DbLocalizationProvider overall initialization took: {sw.ElapsedMilliseconds}ms"); return(builder); }