Hybrid-tenancy with MongoDB in .NET API — Part 02

  • Second: all the data will store in MongoDB, so we will manipulate more than one database on it.
  • Third: we will build an API using .NET Core
  • Fourth and last: we will implement the tenancy technique in our project, according to request.
  • Database: location where stay the repository classes.
  • Middleware: location where stay the code that intercepts the requests and makes something.
  • Models: location where stay the model entities

Model Entities

We need two specific entities, Customer and AppTenant. The customer entity will represent customer data:

Tenant Configuration

We need to install the package:

  • This class will receive the intercept the request, verify the headers, get the header that contains the register number of the company, and attribute it to AppTenant class.

Database configurations and operations

We need to install a package:

  • We will inherit the ICustomerRepository, to implement it and to do the Inversion of Control

The execution

We make the request below:


We can see that is very simple to make a solution for many clients, and we have more than one to do this. I would like to exalt this solution because I think this is more secure and simple to do. We can observe that we segregate the DBs but the business logic is the same.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store