public static TenantBuilder WithTenantSession(this TenantBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.AddScoped <ITenantSession, TenantSession>();
            return(builder);
        }
        public static TenantBuilder WithTenantContainer(this TenantBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.AddSingleton <ITenantContainerFactory>(provider =>
                                                                    new TenantContainerFactory(provider, builder.Services));
            return(builder);
        }
        /// <summary>
        /// Add the services (application specific tenant class)
        /// </summary>
        /// <param name="services"></param>
        public static IServiceCollection AddMultitenancy <TTenant, TTenantStore, TResolutionStrategy>(this IServiceCollection services)
            where TTenant : class, ITenant
            where TTenantStore : ITenantStore <TTenant>
            where TResolutionStrategy : ITenantResolutionStrategy
        {
            services.AddScoped <ITenantAccessor <TTenant>, TenantAccessor <TTenant> >();

            var tenantBuilder = new TenantBuilder <TTenant>(services);

            tenantBuilder.WithStore <TTenantStore>();
            tenantBuilder.WithResolutionStrategy <TResolutionStrategy>();
            return(services);
        }
 /// <summary>
 /// Will search to find a The http request header in the current http request.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="builder"></param>
 /// <param name="headerName">The http request header name from where to extract the tenant id. ie: X-Tenant-Id</param>
 /// <param name="lifetime"></param>
 /// <returns></returns>
 /// <remarks>In order to use this strategy ensure that the UseRouting call precedes the UseMultiTenancy middleware</remarks>
 public static TenantBuilder <T> FromHeader <T>(this TenantBuilder <T> builder, string headerName = Constants.HttpRequestHeaderName, ServiceLifetime lifetime = ServiceLifetime.Transient) where T : Tenant =>
 builder.WithResolutionStrategy((sp) => {
     var accessor = sp.GetRequiredService <IHttpContextAccessor>();
     return(new HeaderResolutionStrategy(accessor, headerName));
 }, lifetime);
 /// <summary>
 /// Will search to find the current tenant identifier from the currently running application Host. For example www.indice.gr
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="builder"></param>
 /// <param name="lifetime"></param>
 /// <returns></returns>
 public static TenantBuilder <T> FromHost <T>(this TenantBuilder <T> builder, ServiceLifetime lifetime = ServiceLifetime.Transient) where T : Tenant =>
 builder.WithResolutionStrategy <HostResolutionStrategy>(lifetime);
 /// <summary>
 /// Register the tenant store implementation In memory
 /// </summary>
 /// <param name="builder">The builder</param>
 /// <param name="tenants">Available tenants</param>
 /// <param name="lifetime"></param>
 /// <returns></returns>
 public static TenantBuilder <T> WithInMemoryStore <T>(this TenantBuilder <T> builder, IEnumerable <T> tenants, ServiceLifetime lifetime = ServiceLifetime.Transient) where T : Tenant =>
 builder.WithStore((sp) => new InMemoryTenantStore <T>(tenants), lifetime);