I am developing a medical application SaaS
And for cost reasons, I will create a multitenancy with shared database with Where column ClinicID
All my operations List / Save / Edit / Delete passes through the repository
and every repository has a Repository base
So too, all models inherit from ModelBase and my Repository base is as follows:
public class RepositoryBase <T> where T: ModelBase
My question is:
It is correct I intercept the actions of Repository Base thus:
public RepositoryBase ()
_context = _context ?? AppDbContext new ();
_dbSet = _dbSet ?? _context.Set <T> ();
var user = (ClaimsIdentity) System.Web.HttpContext.Current.User.Identity;
if (User! = null)
var = ClinicClaim user.Claims.FirstOrDefault (c => c.Type == "Clinic");
if (ClinicClaim! = null)
ClinicId = Convert.ToInt32 (EmpresaClaim.Value);
ClinicId = 0;
public virtual IQueryable <T> GetAll ()
_dbSet.Where return (x => == x.ClinicId ClinicId);
And ClinicId in Claims?
What more elegant way to do this?
Your approach works. But in many scenarios you don’t need ClinicId (tenant key). This depends on how designed your database.
For example you are fetching a patient based on the patientid, then you don’t need ClinicId. Sometimes you can design your database in a such a way that you can reduce the use of ClinicId. In those scenarios you can get rid of your base class.
Yes, I’m find my Patient Find(patientid)
But the get Edit by Id for the URL, a clinic can access the Patient of other Clinic
So I thought of something like this:
public virtual T GetById(int id)
return _dbSet.Where(x => x.Id == id && x.ClinicId== ClinicId).FirstOrDefault();
So I validate if Null
Your approach looks good.
Alternatively you can use some kind of Profile object and store clinic and user information so that it is globally accessible.
In User Claims