Multi-Tenancy with EFDAL

Oct 30, 2011 at 11:46 PM

First, I love nHydrate, excellent work guys!

Second, I'm trying to figure out how to extend it for multi-tenant use ("shared database, shared schema").  I have figured out how to extend the NHDAL using the partial classes to inject my multi-tenant rules into the SQL before it gets executed.   Simply put, I'm grabbing the generated SQL right before it goes to the optimizer, and adding additional WHERE criteria.   

I'm trying to see if it's possible to do something similar with EFDAL.  The downside (for me) of EF is that it considers an entire table as an entity, when for me I need it to see only subsets of that table as a dynamic entity for each user based on their tenant rights.  I could create separate database views for each table, or I could create a layer of code above EFDAL to catch and rewrite each LINQ statement.   Both of these options would be labor intensive and not very elegant.  I stumbled upon a feature in nHibernate which allows for filtered views, which many recommend for multi-tenancy needs.  But EF doesn't support this, can anyone think of a way that I could extend nHydrate for the purpose of multi-tenancy?



Nov 4, 2011 at 7:09 PM

Update:  For anyone that may be interested in this topic, the route I've decided to pursue is to create custom SP's which I can pass a "PersonID" parameter to.  The SP inturn runs through custom logic to restrict the person's view of the data as required.  Sure, I could have done this within LINQ, but my tenancy rules are very complex so I wanted to bake it in as low as possible.

I'm able to do this successfully with nHydrate's custom stored procedure feature.  If I could add one thing to the nHydrate wish-list, would be to add the option to return an entity from an SP rather than just a complex type, that would be awesome!