locked
multi tenant, multi database, code first, migrations, on-demand tabel creation RRS feed

  • Question

  • User1088965359 posted

    hi.. 

    I'm trying to architecture an MVC5 application which will use Areas to separate each tenant. 

    The purpose is to have a single solution which will host multiple websites, connecting to multiple databases and have individual - yet shared use cases. 

    eg: SiteA might be a Webshop, while SiteB might be a Blog - they will however share some fundamentalt like a login system and billing / payment integration. 

    Ideally i would like to create sets of models eg: MechantModels, ContentModels, AuthenticationModels, PaymentModels, etc... 

    And then, when creating a new Area (Website), i'd create a Context + Configuration or add a reference to a Generic Repository - but somehow include the models needed for the specific website... 

    all sites would most likely include Authentication, but MerchantModels should be optional - which also means that the resulting Database doesn't get polluted with tables not needed for the specific site. 

    The point is that each customer (website) can select modules that they want to add - which results in the developers adding the modules with code ... 

    I imagine the Area Context could add each module using Interfaces ?? 

    the question is, how should i structure all this ?

    I've tried creating base models using abstract classes - but i seem to fall short in how to get it all right ... 

    any advice, links or solutions to my architecture out there ?? 

    Thanks :) 

    Tuesday, May 8, 2018 9:03 AM

Answers

  • User1088965359 posted

    hi ..

    I think i've figured it out.. 

    In the root MVC App i'm creating a base-DBContext class as well as ALL table definitions/models - the DBContext class is Abstract.

    Then i add Areas where each area implements its own DBcontext, inherited from the baseclass. If the Area then need extra properties on an entity, it simply create an inherited version, and then override the dbset<> on the inherited db-context. 

    I've tested all this and it works perfectly. 

    I've also managed to use URL Rewrite to map www.area1.com to /Area1 which makes it possible to host multiple websites in ONE application. 

    If anyone is interested i can upload a zip of the project somewhere - just ping me if you want it as a sample... 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 15, 2018 10:51 AM

All replies

  • User1120430333 posted

    I imagine the Area Context could add each module using Interfaces ??

    the question is, how should i structure all this ?
    I've tried creating base models using abstract classes - but i seem to fall short in how to get it all right ...
    any advice, links or solutions to my architecture out there ??

    You should learn how to use the factory pattern, an abstract factory pattern.

    https://www.tutorialspoint.com/design_pattern/factory_pattern.htm

    Maybe, you should look into Architectural Patterns and Styles.

    https://msdn.microsoft.com/en-us/library/ee658117.aspx

    Wednesday, May 9, 2018 9:41 PM
  • User1088965359 posted

    hi ..

    I think i've figured it out.. 

    In the root MVC App i'm creating a base-DBContext class as well as ALL table definitions/models - the DBContext class is Abstract.

    Then i add Areas where each area implements its own DBcontext, inherited from the baseclass. If the Area then need extra properties on an entity, it simply create an inherited version, and then override the dbset<> on the inherited db-context. 

    I've tested all this and it works perfectly. 

    I've also managed to use URL Rewrite to map www.area1.com to /Area1 which makes it possible to host multiple websites in ONE application. 

    If anyone is interested i can upload a zip of the project somewhere - just ping me if you want it as a sample... 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 15, 2018 10:51 AM