在 AspNetBoilerPlate 中为来自主机的租户添加一个实体的默认值
Add default value for one entity for tenants from host in AspNetBoilerPlate
场景:
我的多租户项目基于 ASPNetBoilerPlate。
我有一个“工作”实体,它是 IMayHaveTenant。每个租户都必须看到主机中的默认作品以及他的作品无处不在。我必须怎么做?
我需要一些代码,例如:tenantId == id || tenantId is null
答案错误:
using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant)){ }
这个 returns 其他租户也适用。
创建一个界面,继承IMayHaveTenant
如果你想要它的其他内置功能:
public interface IMayHaveTenantSharedWithHost : IMayHaveTenant
{
}
实现该接口:
public class Work : Entity, IMayHaveTenantSharedWithHost
{
public int? TenantId { get; set; }
// ...
}
覆盖 AbpDbContext
子类中的 CreateFilterExpression
并处理该接口:
protected override Expression<Func<TEntity, bool>> CreateFilterExpression<TEntity>()
where TEntity : class
{
Expression<Func<TEntity, bool>> expression = null;
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> softDeleteFilter = e => !IsSoftDeleteFilterEnabled || !((ISoftDelete) e).IsDeleted;
expression = expression == null ? softDeleteFilter : CombineExpressions(expression, softDeleteFilter);
}
// if (typeof(IMayHaveTenant).IsAssignableFrom(typeof(TEntity)))
if (typeof(IMayHaveTenantSharedWithHost).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> mayHaveTenantFilter = e => !IsMayHaveTenantFilterEnabled || ((IMayHaveTenant)e).TenantId == CurrentTenantId || ((IMayHaveTenant)e).TenantId == null;
expression = expression == null ? mayHaveTenantFilter : CombineExpressions(expression, mayHaveTenantFilter);
}
else if (typeof(IMayHaveTenant).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> mayHaveTenantFilter = e => !IsMayHaveTenantFilterEnabled || ((IMayHaveTenant)e).TenantId == CurrentTenantId;
expression = expression == null ? mayHaveTenantFilter : CombineExpressions(expression, mayHaveTenantFilter);
}
if (typeof(IMustHaveTenant).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> mustHaveTenantFilter = e => !IsMustHaveTenantFilterEnabled || ((IMustHaveTenant)e).TenantId == CurrentTenantId;
expression = expression == null ? mustHaveTenantFilter : CombineExpressions(expression, mustHaveTenantFilter);
}
return expression;
}
场景:
我的多租户项目基于 ASPNetBoilerPlate。
我有一个“工作”实体,它是 IMayHaveTenant。每个租户都必须看到主机中的默认作品以及他的作品无处不在。我必须怎么做?
我需要一些代码,例如:tenantId == id || tenantId is null
答案错误:
using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant)){ }
这个 returns 其他租户也适用。
创建一个界面,继承IMayHaveTenant
如果你想要它的其他内置功能:
public interface IMayHaveTenantSharedWithHost : IMayHaveTenant
{
}
实现该接口:
public class Work : Entity, IMayHaveTenantSharedWithHost
{
public int? TenantId { get; set; }
// ...
}
覆盖 AbpDbContext
子类中的 CreateFilterExpression
并处理该接口:
protected override Expression<Func<TEntity, bool>> CreateFilterExpression<TEntity>()
where TEntity : class
{
Expression<Func<TEntity, bool>> expression = null;
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> softDeleteFilter = e => !IsSoftDeleteFilterEnabled || !((ISoftDelete) e).IsDeleted;
expression = expression == null ? softDeleteFilter : CombineExpressions(expression, softDeleteFilter);
}
// if (typeof(IMayHaveTenant).IsAssignableFrom(typeof(TEntity)))
if (typeof(IMayHaveTenantSharedWithHost).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> mayHaveTenantFilter = e => !IsMayHaveTenantFilterEnabled || ((IMayHaveTenant)e).TenantId == CurrentTenantId || ((IMayHaveTenant)e).TenantId == null;
expression = expression == null ? mayHaveTenantFilter : CombineExpressions(expression, mayHaveTenantFilter);
}
else if (typeof(IMayHaveTenant).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> mayHaveTenantFilter = e => !IsMayHaveTenantFilterEnabled || ((IMayHaveTenant)e).TenantId == CurrentTenantId;
expression = expression == null ? mayHaveTenantFilter : CombineExpressions(expression, mayHaveTenantFilter);
}
if (typeof(IMustHaveTenant).IsAssignableFrom(typeof(TEntity)))
{
Expression<Func<TEntity, bool>> mustHaveTenantFilter = e => !IsMustHaveTenantFilterEnabled || ((IMustHaveTenant)e).TenantId == CurrentTenantId;
expression = expression == null ? mustHaveTenantFilter : CombineExpressions(expression, mustHaveTenantFilter);
}
return expression;
}