Base solution for your next web application
Open Closed

How to overwrite the implementation of IMultiTenantLocalizationSource? #8954


User avatar
0
OriAssurant created

Hi Abp Zero,

We're using .net Core 2.1 and Angular 8.

For certain reason I would like to overwrite MultiTenantLocalizationSource with my own CustomMultiTenantLocalizationSource.

I tried to use following code in the PreInitialize() in ProjectCoreModule but it didn't work as other replaced services:

Configuration.ReplaceService<IMultiTenantLocalizationSource, CustomMultiTenantLocalizationSource>(DependencyLifeStyle.Transient);

Could you please advise how I coud replace the IMultiTenantLocalizationSource service?

Thank you


6 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    IMultiTenantLocalizationSource is not injected anywhere, so you can't replace its implementation that way.

    MultiTenantLocalizationSource is instantiated directly in EnableDbLocalization in LanguageManagementConfig.cs#L40.

    You can comment out the following line in your WebCoreModule and instantiate your own implementation.

    //Use database for language management
    Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
    
  • User Avatar
    0
    OriAssurant created

    Thank you Aaron.

    Hmm... then could I do this? Not sure if ILanguageManagementConfig is injected...

    Configuration.ReplaceService<ILanguageManagementConfig, CustomLanguageManagementConfig>();
    Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
    

    Thank you, Robin

  • User Avatar
    0
    aaron created
    Support Team

    Hi Robin, it is injected and you could do that.

  • User Avatar
    0
    OriAssurant created

    Thank you Aaron. But somehow it's still not replace the service and going into the replaced implementation.

    EnableDbLocalization() is still calling the default one instead of the CustomLanguageManagementConfig.EnableDbLocalization(). Seems LanguageManagementConfig has been initialized together with Configuration.Modules before everything else.

    == Basically I had to use:

    Configuration.ReplaceService<ILanguageManagementConfig, CustomLanguageManagementConfig>();
    (new CustomLanguageManagementConfig(Configuration.IocManager, Configuration)).EnableDbLocalization();
    

    Is there any concern of calling customized EnableDbLocalization() in this way?

  • User Avatar
    0
    aaron created
    Support Team

    I would recommend Option 1.2 below.

    Option 1: Register custom ILanguageManagementConfig

    Seems LanguageManagementConfig has been initialized together with Configuration.Modules before everything else.

    Good conjecture. More accurately, ILanguageManagementConfig is instantiated on the first call to Configuration.Modules.Zero().

    • Configuration.Modules.Zero() is first called in YourProjectCoreModule.PreInitialize.
    • ReplaceService only runs in AbpKernalModule.Initialize, before other modules' Initialize.

    Therefore, calling ReplaceService for this in YourProjectWebCoreModule.PreInitialize is too late.

    Option 1.1: Directly register as default before ILanguageManagementConfig is instantiated

    You can directly register CustomLanguageManagementConfig as the default implementation before that first call.

    IocManager.IocContainer.Register(                        // Add this
        Component.For<ILanguageManagementConfig>()           // Add this
            .ImplementedBy<CustomLanguageManagementConfig>() // Add this
            .IsDefault()                                     // Add this
    );                                                       // Add this
    
    //Declare entity types                                            // Existing code
    Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant); // Existing code
    
    Option 1.2: Add service before ILanguageManagementConfig is registered

    You can AddSingleton to IServiceCollection in your Web project's ConfigureServices.

    services.AddSingleton<ILanguageManagementConfig, CustomLanguageManagementConfig>();
    

    Option 2: Instantiate custom ILanguageManagementConfig without replacing

    Is there any concern of calling customized EnableDbLocalization() in this way?

    In general, it is not preferable since Configuration.Modules.Zero().LanguageManagement would have the unexpected implementation if used elsewhere — but it's not used elsewhere.

    Therefore, in this case, there is no concern.

  • User Avatar
    0
    OriAssurant created

    Thank you Aaron! That's very detailed and helpful. Wondering why I forgot the IsDefault() at the first place. It's working now!