I would like to ask your opinions regarding the way I handle MultiTenancy. I"m using MVC3 (switching to MVC4) and EF as my backend. I'm using a single app, shared schema MultiTenancy. Below is the code:
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; }
}
Of course DI will be there as well but for simplicity I removed them here (temporarily). I used Generics (feeling dirty about it) on the Service Layer since I'm having trouble on comparing the TenantId with the correct Entity that will be passed on the class. I'm eyeing to recode this using FilterAttributes but I dont have any idea how to. How do you guys handle your multitenancy? Is the design have some crucial flaws that I may encounter on the long run? If you have some samples using FilterAttributes that would be a big help.
Thanks!