Base solution for your next web application
Open Closed

Documentation for initializing new tenants #10153


User avatar
0
hra created

Prerequisites

AspNetZero .NET Core/Angular 10.2

I have added numerous new tables to my schema, which must be initialized for each new tenant created.

Do you have a recommended hooking process/convention for "initializing" default state for a new tenant?

I have scanned the documentation, and the best I could find, was reference to seeding tables during migration - which doesnt address seeding/initializing when a host admin clicks "new tenant".

Thanks,


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry for the late reply. It is better to do it in CreateWithAdminUserAsync method of TenantManager.

  • User Avatar
    1
    payoff created

    @hra I had a similar requirement.

    Generally we've adopted a strategy to keep the base ANZ code upgradeable, that consists in writing all additional implementation and overrides in a separated project that defines its own Module. With this premise, for customizing operations during new tenant registration I've ended up inheriting from TenantRegistrationAppService, marking its RegisterTenant method as virtual and overriding it in my inheriting class:

    public class MyCustomTenantRegistrationAppService : TenantRegistrationAppService
    {
            ....
    
            /// <summary>
            ///     Customize tenant registration operations
            /// </summary>
            [UnitOfWork]
            public override async Task<RegisterTenantOutput> RegisterTenant(RegisterTenantInput input){
                    ...
                    var registerTenantOutput = await base.RegisterTenant(input);
                    ...
            }
    }
    

    The overriding implementation must be registered as default during the module's PreInitialize method:

    /// <summary>
    ///     Application layer module of the application.
    /// </summary>
    [DependsOn(typeof(MyCompanyApplicationSharedModule), typeof(MyCompanyCoreModule))]
    public class MyCompanyAppModule : AbpModule
    {
        ...
        public override void PreInitialize()
        {
           ...
            // register tenant registration app service override
            Configuration.ReplaceService(typeof(TenantRegistrationAppService), () => {
                Configuration.IocManager.IocContainer.Register(Component.For<TenantRegistrationAppService>()
                    .ImplementedBy<MyCustomTenantRegistrationAppService>()
                    .IsDefault());
            });
           ...
        }
        ...
    }
    

    If you do not use implementations separation in a dedicated module, you can simply modify TenantRegistrationAppService.RegisterTenant.

  • User Avatar
    0
    hra created

    Hi @payoff,

    Thank you for your suggestion. This is exactly the kind of best-practice I was looking for.

    Regards,