Hi,
Is there an easy way for me to create functionality similar to ICurrentTenant that would allow me to implement an ICurrentCompany interface that functions just like ICurrentTenant such as the ability to change it at will (from a blazor WASM project). I have a layered blazor WASM solution and I have the requirement to store(in a cookie) the 'current company' (an guid id representing a record in the DB which is a 3 character value) that is set when a user of our application chooses a 'company' from the dropdown in the UI for our application.
Thanks
2 Answer(s)
-
0
To implement an
ICurrentCompany
service similar toICurrentTenant
in a layered Blazor WASM solution, you can follow these steps:- Define the ICurrentCompany Interface:
public interface ICurrentCompany { Guid? CompanyId { get; } string CompanyCode { get; } Task ChangeAsync(Guid companyId, string companyCode); }
Implement the Service:
- Store the selected company in a cookie (or local storage) on the client.
- On the server, read the cookie and set the current company context for each request.
Example implementation for Blazor WASM (client-side):
public class CurrentCompany : ICurrentCompany { private readonly IJSRuntime _jsRuntime; private const string CompanyCookieName = "CurrentCompany"; public Guid? CompanyId { get; private set; } public string CompanyCode { get; private set; } public CurrentCompany(IJSRuntime jsRuntime) { _jsRuntime = jsRuntime; } public async Task ChangeAsync(Guid companyId, string companyCode) { CompanyId = companyId; CompanyCode = companyCode; await _jsRuntime.InvokeVoidAsync("BlazorCookies.Set", CompanyCookieName, companyId.ToString()); // Optionally store companyCode as well } public async Task InitializeAsync() { var companyIdString = await _jsRuntime.InvokeAsync<string>("BlazorCookies.Get", CompanyCookieName); if (Guid.TryParse(companyIdString, out var companyId)) { CompanyId = companyId; } } }
- Use a JS interop library (like BlazorCookies) to manage cookies in Blazor WASM.
- Register the Service:
builder.Services.AddScoped<ICurrentCompany, CurrentCompany>();
Set the Company from the UI:
- When a user selects a company from the dropdown, call
ChangeAsync
on yourICurrentCompany
service.
- When a user selects a company from the dropdown, call
Server-Side:
- On the backend, create a middleware or a custom provider to read the company cookie from the request and set the company context for the current user/session.
- You can use a custom data filter similar to ABP's multi-tenancy filter to restrict data by company.
Data Filtering:
- Implement a custom data filter for company-based filtering, similar to how ABP handles multi-tenancy:
public interface ICompanyFilter { Guid? CompanyId { get; } }
- Apply this filter to your entities and queries.
References for Custom Filters and Claims:
This approach aligns with ABP conventions for context services and data filtering, allowing you to change the current company at runtime and persist the selection in a cookie, similar to how tenants are managed.
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.
-
0
hi
This article has a similar feature.
You can check it
https://abp.io/community/articles/switching-between-organization-units-i5tokpzt#gsc.tab=0
Thanks.