我想就我处理 MultiTenancy 的方式询问您的意见。我正在使用 MVC3(切换到 MVC4)和 EF 作为我的后端。我正在使用单个应用程序,共享模式 MultiTenancy。下面是代码:
public abstract class Service<T> where T : Entity
{
private Repository<T> _repo;
public Service()
{
_repo = new Repository<T>();
}
public bool Authenticate(int id)
{
//var companyInfo = _authorizationRepository.GetApiKey(apiKey);
int tenantId = 0; // replaced by companyInfo using repository
var entity = _repo.GetQuery(tenantId).Where(e => e.Id == id).First();
if (tenantId != entity.TenantId)
throw new ArgumentException();
return true;
}
}
public class EmployeeService : Service<Employee>
{
private EmployeeRepository employeeRepository;
public EmployeeService()
{
employeeRepository = new EmployeeRepository();
}
public Employee GetEmployeeById(int employeeId)
{
this.Authenticate(employeeId);
return employeeRepository.GetById(employeeId);
}
}
public class Entity
{
public int Id { get; set; }
public int TenantId { get; set; }
}
当然 DI 也会在那里,但为了简单起见,我在这里(暂时)删除了它们。我在服务层上使用了泛型(感觉很脏),因为我无法将 TenantId 与将在类上传递的正确实体进行比较。我正在考虑使用 FilterAttributes 重新编码,但我不知道该怎么做。你们是如何处理多租户的?从长远来看,设计是否存在一些我可能会遇到的关键缺陷?如果您有一些使用 FilterAttributes 的示例,那将是一个很大的帮助。
谢谢!