基于 ASP.net MVC 教程的 GenericRepository 模式(在 ASP.NET MVC 应用程序中实现存储库和工作单元模式),反对实现多租户的内容如下:
public class GenericMultiTenantRepository<TEntity> where TEntity : MultitenantEntity
{
internal SchoolContext context;
internal DbSet<TEntity> dbSet;
...
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
query = query.Where(entity => entity.TenantId == <TenantId>); /* Like this? */
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
和 MultitenantEntity 如下:
public class MultitenantEntity {
public int TenantId {get;set;}
}
现在所有实体都派生自 MultitenantEntity,您仍然可以对整个应用程序进行编程,就好像它只适用于一个租户?
我在监督什么吗?或者是否有更广泛接受的做法来实现我想要做的事情?
同样的原则也应该添加到 insert 方法中,但为简洁起见,我省略了这些更改。