Open Closed

Need to trigger a event when a tenant logins and add a dynamic menu #6604


User avatar
0
Sraman created
  • ABP Framework version: v8.0.0
  • UI Type: MVC
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

Looking to trigger an event when a tenant login and load a dynamic menu using cache or any other custom method instead of IMenuContributor. So basically, looking for two things,

  1. Event handler which invokes when a tenant logins
  2. Based on the tenant Id want to perform a external api get call and save those data to an existing DB.
  3. Finally want to load the dynamic menu with the data pulled from get api call.

Need a proper way to tackle this. Not sure using IMenuContributor to load a dynamic menu is a good practice.


3 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can consider using distributed cache to store the dynamic menus.

    For example:

    public class MyMenuContributor : IMenuContributor
    {
        private readonly IDistributedCache<DynamicMenusCacheItem> _menusCache;
         
        public MyMenuContributor(IConfiguration configuration)
        {
            _configuration = configuration;
        }
    
        public async Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            if (context.Menu.Name == StandardMenus.Main)
            {
                await ConfigureMainMenuAsync(context);
            }
        }
    
        private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
            var dynamicMenus = await _menusCache.GetOrAddAsync(
                    nameof(DynamicMenusCacheItem), //Cache key
                    async () => await GetFromRemoteAPI(),
                    () => new DistributedCacheEntryOptions
                    {
                        AbsoluteExpiration = DateTimeOffset.Now.AddHours(1)
                    }
                );
                
            //...
        }
        
        private Task<DynamicMenusCacheItem> GetFromRemoteAPI()
        {
            //...
        }
    }
    
    public class DynamicMenusCacheItem
    {
       ....
    }
    
  • User Avatar
    0
    Sraman created

    Hi, And also how can I trigger an external api call and save the response to DB. Is it a good practice to do that from menu contributor?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I think there is no problem, you can abstract some services if you need to. for example:

    • Create IDynamicMenuStore interface
    • Create DynamicMenuStore to implement IDynamicMenuStore interface
    public interface IDynamicMenuStore
    {
         Task<List<DynamicMenuItems>> GetMenuAsync();
    }
    
    public class DynamicMenuStore: IDynamicMenuStore, ITransientDependency
    {
         public Task<List<DynamicMenuItems>> GetMenuAsync()
         {
             ....
         }
    }
    
    public class MyMenuContributor : IMenuContributor
    {
        private readonly IDynamicMenuStore _dynamicMenuStore;
         
        public MyMenuContributor(IDynamicMenuStore dynamicMenuStore)
        {
            _dynamicMenuStore = dynamicMenuStore;
        }
    
        public async Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            if (context.Menu.Name == StandardMenus.Main)
            {
                await ConfigureMainMenuAsync(context);
            }
        }
    
        private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
            var dynamicMenus = await _dynamicMenuStore.GetMenuAsync();
                
            //...
        }
    }
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09