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,
- Event handler which invokes when a tenant logins
- Based on the tenant Id want to perform a external api get call and save those data to an existing DB.
- 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)
-
0
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 { .... }
-
0
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?
-
0
Hi,
I think there is no problem, you can abstract some services if you need to. for example:
- Create
IDynamicMenuStore
interface - Create
DynamicMenuStore
to implementIDynamicMenuStore
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(); //... } }
- Create