I am working on a multi-user web application. At the beginning of each request, the web application will use either a URL or a cookie to determine the tenant ID of the current request. I want to make this available for my data layer so that it can automatically include the identifier in SQL queries without the need to explicitly pass it through the layers for each query. However, I do not want my data level to be dependent on System.Web or used only in a web application.
So, I created an interface in the data layer ITenantIDProvider
public interface ITenantIDProvider
{
Guid GetTenantID();
}
And it can be implemented something like this:
public class TenantIDProvider : ITenantIDProvider
{
public Guid GetFacilityID()
{
return (Guid)HttpContext.Current.Items["TenantID"];
}
}
, Items, , . -, , .
- TenantIDProvider , EntityFramework IDbContextInterceptor:
public static void SetTenantIDProvider(ITenantIDProvider tenantIDProvider)
{
System.Data.Entity.Infrastructure.Interception.DbInterception.Add(new MyContextInterceptor(tenantIDProvider));
}
DbInterception.Add , , , , . , tenantIDProvider (, , ), ? , , , , , . , /, , .