Collie 2.2.2

dotnet add package Collie --version 2.2.2
NuGet\Install-Package Collie -Version 2.2.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Collie" Version="2.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Collie --version 2.2.2
#r "nuget: Collie, 2.2.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Collie as a Cake Addin
#addin nuget:?package=Collie&version=2.2.2

// Install Collie as a Cake Tool
#tool nuget:?package=Collie&version=2.2.2

Collie

Inversion of Control Container

Purpose

Collie is a multi-tenant inversion of control container for .NET (as of v1.2, .NET 6.0 & above are supported). Multi-tenancy is supported by introducing the concept of a tenant key, that defines the shared context which a given scope should use to resolve the TenantSingleton service lifetime (one shared instance per tenant).

Usage

Collie offers interoperability with the standard .NET dependency injection API. However, to support the enhanced tenancy features, a native API is also provided.

For the most basic use case, a ServiceCollection can be populated, and an IServiceProvider built as shown below.

int tenantId = 0;
var services = new ServiceCollection();
services.AddSingleton<IServiceA, DefaultServiceA>();
services.AddTenantSingleton<IServiceB, DefaultServiceB>();
services.AddScoped<Tuple<int>>(container => new Tuple<int>(tenantId++));

IServiceContainer rootContainer = services.BuildCollieProvider(catalog, container => container.GetService<Tuple<int>>(), typeof(Tuple<int>));

var scopedProvider = rootContainer.CreateScope().ServiceProvider;

If using from ASP.NET Core, Collie provides the MultitenantServiceProviderFactory as shown below.

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseServiceProviderFactory(new Collie.Compatibility.MultitenantServiceProviderFactory(k => k.GetRequiredService<KeyType>(), typeof(KeyType), new Collie.Abstractions.ServiceContainerOptions() { IgnoreUnresolvableEnumerables = true }))
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
}

For more advanced usage via the Collie API, see the unit test suite.

Scalability

One of the key challenges that arises when dealing with (potentially massive) multi-tenancy is determining exactly how long the TenantSingleton lifetime should persist. In the nominal case, where there are relatively few clients, TenantSingletons should live effectively as long as a Singleton, which is to say until the root IServiceProvider is disposed. However, as the number of tenants rises, so too does the memory overhead required to retain all TenantSingletons. Consequently, at some point, it becomes desireable to dispose of tenant state not currently in active use. Using Collie's native API, this can be achieved by specifying a TenantCacheSize. This controls the desired number of tenant contexts to retain in memory when no requests are active. When requests are active, the actual number retained may be higher if needed to service all active requests.

Background

While Collie derives its name from the breed known for their ability to fetch, it also pays tribute to a childhood friend who later tragically disappeared under suspicious circumstances. Still hoping you're out there and find your way home.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Collie:

Package Downloads
Collie.Compatibility

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.2 118 9/30/2023
2.2.1 118 8/29/2023
2.2.0 144 8/29/2023
2.1.0 150 7/15/2023
2.0.0 435 10/30/2022
1.2.0 502 7/17/2022
1.1.0 535 7/3/2022
1.0.11 515 6/30/2022
1.0.10 512 6/7/2022
1.0.9 502 6/4/2022
1.0.8 519 5/29/2022
1.0.7 501 5/28/2022
1.0.6 522 5/28/2022
1.0.4 518 5/12/2022
1.0.3 359 5/12/2022
1.0.2 363 5/12/2022
1.0.1 355 5/12/2022
1.0.0 363 5/12/2022