基於ASP.net MVC教程的GenericRepository模式(Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application),對實現多租戶的什麼東西在說話如下:單獨在GenericRepository中實現多租戶可以嗎?
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,您仍然可以編寫整個應用程序,就好像它只針對一個租戶?
我在監督什麼嗎?還是有更廣泛接受的做法來實現我想要做的事情?
插入方法也應該添加相同的原則,但爲了簡潔,我省略了這些更改。
我明白了,非常感謝! – senic
嗨CodeCaster - 我試圖實現類似的東西,但我遇到了虛擬屬性的困難。如果你有一分鐘,你會介意看看這個? http://stackoverflow.com/questions/19826316/virtual-navigation-properties-and-multi-tenancy – RobVious