Eodg.MultiTenancy.Middleware 0.1.0-aplha

Middleware to allow for multi-tenancy in .net core web api applications.

This is a prerelease version of Eodg.MultiTenancy.Middleware.
There is a newer prerelease version of this package available.
See the version list below for details.
Install-Package Eodg.MultiTenancy.Middleware -Version 0.1.0-aplha
dotnet add package Eodg.MultiTenancy.Middleware --version 0.1.0-aplha
<PackageReference Include="Eodg.MultiTenancy.Middleware" Version="0.1.0-aplha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Eodg.MultiTenancy.Middleware --version 0.1.0-aplha
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

MultiTenancyMiddleware

It's a little black box... kind of

This library allows you to incorporate multitenancy into a .net core 2.0 web api project. After setting up a handful of things (outlined below), you will be able to make a request, and by adding a header to said request containing the account id of the tenant for whose data you want to access you can get said data.

How do you implement it?

  • Write a concrete implementation of the provided MultiTenancyResolverService abstract class / IMultiTenancyResolverService interface.

    • There only has to be a ConnectionString property and a SetConnectionStringByAccountId(string accountId) method that sets the ConnectionString property.
  • In the Startup.cs file, we'll have to do a couple of things:

    • Add your implementation to the built-in DI container a la:

      services.AddScoped&lt;IMultiTenancyResolverService, ExampleMultiTenancyResolverService&gt;();

      where ExampleMultiTenancyResolverService is the name of your concrete implementation of the provided MultiTenancyResolverService abstract class / IMultiTenancyResolverService interface

    • Add the options for the middleware. This only contains a HeaderKey and this will be the header key used in the request that contains the account id of the tenant whose database you want to reference for the particular request.

      var multiTenancyMiddlewareOptions = new MultiTenancyMiddlewareOptions
      {
          HeaderKey = "Account-Id"
      };
      
      services.AddSingleton(multiTenancyMiddlewareOptions);
      
  • Database context class for the tenant template database:

    • Inject the IMultiTenancyResolver into the constructor, and store in private variable:

    private IMultiTenancyResolverService _multiTenancyResolverService;

    • Add the following method:
         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {
             optionsBuilder.UseSqlServer(_multiTenancyResolverService.ConnectionString);
             base.OnConfiguring(optionsBuilder);
         }
      

How do you use it?

  • For any controller/method utilizing multitenancy add the following attribute

    [MiddlewareFilter(typeof(MultiTenancyPipeline))]

  • Any time a request is made to said controllers/methods, a header will have to exist on the request that has the key specified in the Startup.cs class, and will have to have a value of the account id.

MultiTenancyMiddleware

It's a little black box... kind of

This library allows you to incorporate multitenancy into a .net core 2.0 web api project. After setting up a handful of things (outlined below), you will be able to make a request, and by adding a header to said request containing the account id of the tenant for whose data you want to access you can get said data.

How do you implement it?

  • Write a concrete implementation of the provided MultiTenancyResolverService abstract class / IMultiTenancyResolverService interface.

    • There only has to be a ConnectionString property and a SetConnectionStringByAccountId(string accountId) method that sets the ConnectionString property.
  • In the Startup.cs file, we'll have to do a couple of things:

    • Add your implementation to the built-in DI container a la:

      services.AddScoped&lt;IMultiTenancyResolverService, ExampleMultiTenancyResolverService&gt;();

      where ExampleMultiTenancyResolverService is the name of your concrete implementation of the provided MultiTenancyResolverService abstract class / IMultiTenancyResolverService interface

    • Add the options for the middleware. This only contains a HeaderKey and this will be the header key used in the request that contains the account id of the tenant whose database you want to reference for the particular request.

      var multiTenancyMiddlewareOptions = new MultiTenancyMiddlewareOptions
      {
          HeaderKey = "Account-Id"
      };
      
      services.AddSingleton(multiTenancyMiddlewareOptions);
      
  • Database context class for the tenant template database:

    • Inject the IMultiTenancyResolver into the constructor, and store in private variable:

    private IMultiTenancyResolverService _multiTenancyResolverService;

    • Add the following method:
         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {
             optionsBuilder.UseSqlServer(_multiTenancyResolverService.ConnectionString);
             base.OnConfiguring(optionsBuilder);
         }
      

How do you use it?

  • For any controller/method utilizing multitenancy add the following attribute

    [MiddlewareFilter(typeof(MultiTenancyPipeline))]

  • Any time a request is made to said controllers/methods, a header will have to exist on the request that has the key specified in the Startup.cs class, and will have to have a value of the account id.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.5.0-alpha 408 4/4/2018
0.4.0-alpha 185 4/4/2018
0.3.0-aplha 208 4/3/2018
0.2.0-aplha 339 3/31/2018
0.1.0-aplha 355 3/31/2018