Canister.IoC
5.3.15
See the version list below for details.
dotnet add package Canister.IoC --version 5.3.15
NuGet\Install-Package Canister.IoC -Version 5.3.15
<PackageReference Include="Canister.IoC" Version="5.3.15" />
paket add Canister.IoC --version 5.3.15
#r "nuget: Canister.IoC, 5.3.15"
// Install Canister.IoC as a Cake Addin #addin nuget:?package=Canister.IoC&version=5.3.15 // Install Canister.IoC as a Cake Tool #tool nuget:?package=Canister.IoC&version=5.3.15
<img src="https://jacraig.github.io/Canister/images/icon.png" style="height:25px" alt="Canister Icon" /> Canister
Canister is one of the easiest ways to get IoC configuration under control. No longer do you have to search for that one class that you forgot to register. Instead use Canister to handle discovery and registration for you using a simple interface.
Basic Usage
The system has a fairly simple interface and only a couple of functions that need explaining. The first is setup:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddCanisterModules();
}
AddCanisterModules will automatically scan assemblies for modules and load them accordingly. Or if you're doing a desktop app:
var Services = new ServiceCollection().AddCanisterModules();
Note that if you like, you can control which assemblies are searched:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddCanisterModules(configure => configure.AddAssembly(typeof(Startup).Assembly));
}
It's recommended that you do this for security reasons as the default will search all assemblies found in the entry assembly's top level directory.
Modules
Canister uses the concept of modules to wire things up. This allows you to place registration code in libraries that your system is using instead of worrying about it in every application. Simply add your library and Canister will automatically wire it up for you. In order to do this, under Canister.Interfaces there is the IModule interface. This interface, when implemented, has two items in it. The first is a property called Order. This determines the order that the modules are loaded in. The second is a function called Load:
public class TestModule : IModule
{
public int Order => 1;
public void Load(IServiceCollection bootstrapper)
{
bootstrapper.AddAllTransient<IMyInterface>();
bootstrapper.AddTransient<MyType>();
}
}
The module above is loaded automatically by the system and will have the Load function called at initialization time. At this point you should be able to resolve and register classes using the bootstrapper parameter. The service collection also has a couple of extra extension methods: AddAllTransient, AddAllScoped, AddAllSingleton:
bootstrapper.AddAllTransient<IMyInterface>();
The AddAllxxxx functions will find everything that implements a class or interface in the Assemblies that you tell it to look in and will register them with the service collection.
Attributes
Canister also allows for attributes to be used to control registration. There are two attributes that the system uses:
- RegisterAttribute - This attribute is used to control how a class is registered. It will register the class as all interfaces that it implements as well as the class itself. The attribute takes the life time of the registration as a parameter. If no parameter is given, the registration will be transient. It also can take a service key as well.
[Register(LifeTime.Singleton)]
public class MyType : IMyInterface
{
}
- RegisterAllAttribute - This attribute is used to control how an interface is registered. It will register all classes that implement the interface similar to the AddAllxxxx functions. The attribute takes the life time of the registration as a parameter. If no parameter is given, the registration will be transient.
[RegisterAll(LifeTime.Singleton)]
public interface IMyInterface
{
}
Canister Extension Methods
Canister provides a set of extension methods to streamline your IoC (Inversion of Control) container registration code. These methods offer convenient ways to conditionally register services based on certain criteria, enhancing the flexibility of your application's dependency injection setup.
1. AddTransientIf()
The AddTransientIf
method registers a service as transient only if a specified condition is met. This is useful when you want to dynamically determine whether a service should be transient or not.
services.AddTransientIf<IMyService, MyService>(services => condition);
2. AddScopedIf()
Similar to AddTransientIf
, AddScopedIf
registers a service as scoped based on a given condition.
services.AddScopedIf<IMyScopedService, MyScopedService>(services => condition);
3. AddSingletonIf()
The AddSingletonIf
method registers a service as a singleton if the specified condition holds true.
services.AddSingletonIf<IMySingletonService, MySingletonService>(services => condition);
4. AddKeyedTransientIf()
, AddKeyedScopedIf()
, AddKeyedSingletonIf()
These methods follow the same pattern as their non-keyed counterparts but additionally allow you to register services with a specified key.
services.AddKeyedTransientIf<IService>(key, implementationType, (services, key) => condition);
5. Exists()
The Exists
method checks whether a service with a specific type and, optionally, a key, has already been registered. This can be helpful in avoiding duplicate registrations or finding issues with your environment before starting the application.
if (!services.Exists<IMyService>())
{
services.AddTransient<IMyService, MyService>();
}
Usage Example
Here's an example of how you might use these methods:
IHostEnvironment? environment;
// Conditionally register a transient service if in development environment.
services.AddTransientIf<IMyService, MyDebugService>(_ => environment.IsDevelopment());
// However if you're in production, add a different implementation.
services.AddTransientIf<IMyService, MyProductionService>(_ => environment.IsProduction());
// Check if a keyed service is missing and log a warning if so.
if (!services.Exists<IService>(key))
{
logger.LogWarning("Service {Service} is missing", key);
}
These methods empower you to create more dynamic and adaptive dependency injection configurations tailored to your application's requirements.
Working With Other IoC Containers
While the library assumes you are using the built in ServiceCollection, it is possible to work with IoC containers. All that is required is that it implements the IServiceCollection interface.
Using Canister in Your library
If you wish to use Canister in your library it is recommended that you build an extension method off of the ICanisterConfiguration interface that will allow you to register your needed assemblies for the user to make the experience a bit simpler when they want to control configuration themselves.
Installation
The library is available via Nuget with the package name "Canister.IoC". To install it run the following command in the Package Manager Console:
Install-Package Canister.IoC
Build Process
In order to build the library you may require the following:
- Visual Studio 2022
Other than that, just clone the project and you should be able to load the solution and build without too much effort.
Product | Versions 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 is compatible. 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. |
-
net6.0
- Fast.Activator (>= 2.1.5)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
-
net8.0
- Fast.Activator (>= 2.1.5)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
NuGet packages (9)
Showing the top 5 NuGet packages that depend on Canister.IoC:
Package | Downloads |
---|---|
Aspectus
Aspectus is an advanced Aspect-Oriented Programming (AOP) library that simplifies the injection of cross-cutting concerns into your codebase. It empowers you to write clean and maintainable code by separating cross-cutting concerns from the core logic of your application. |
|
ObjectCartographer
ObjectCartographer is a high-performance, convention-based C# object-to-object mapping library designed to simplify the process of copying data between objects. It eliminates the tedious task of manually writing code for data copying by providing a developer-friendly approach. |
|
SerialBox
SerialBox is a simple serialization wrapper. It acts as a wrapper around various serialization libraries, thus giving them a common interface to deal with. |
|
SimpleHtmlToPdf
.Net wrapper of wkhtmltopdf library to convert HTML pages to PDF. |
|
FileCurator.Windows
FileCurator.Windows is a set of formats that are only available using the full version of the .Net framework. If an alternative is built in .Net Standard these will be replaced. So consider this a hold over. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
6.0.1 | 1,570 | 11/18/2024 |
6.0.0 | 80 | 11/18/2024 |
5.4.3 | 698 | 11/14/2024 |
5.4.2 | 83 | 11/14/2024 |
5.4.1 | 72 | 11/14/2024 |
5.4.0 | 69 | 11/14/2024 |
5.3.18 | 1,663 | 11/11/2024 |
5.3.17 | 2,562 | 11/4/2024 |
5.3.16 | 1,989 | 10/29/2024 |
5.3.15 | 2,608 | 10/14/2024 |
5.3.14 | 2,969 | 10/9/2024 |
5.3.13 | 10,933 | 8/22/2024 |
5.3.12 | 5,745 | 8/1/2024 |
5.3.11 | 6,556 | 6/24/2024 |
5.3.10 | 33,138 | 6/17/2024 |
5.3.9 | 9,977 | 5/6/2024 |
5.3.8 | 2,339 | 5/1/2024 |
5.3.7 | 1,867 | 4/30/2024 |
5.3.6 | 7,165 | 3/27/2024 |
5.3.5 | 3,505 | 3/14/2024 |
5.3.4 | 1,203 | 3/13/2024 |
5.3.3 | 2,948 | 3/4/2024 |
5.3.2 | 5,474 | 2/26/2024 |
5.3.1 | 3,408 | 2/21/2024 |
5.3.0 | 1,906 | 2/19/2024 |
5.2.4 | 6,084 | 2/2/2024 |
5.2.3 | 7,006 | 1/29/2024 |
5.2.2 | 4,752 | 1/19/2024 |
5.2.1 | 9,592 | 12/11/2023 |
5.2.0 | 439 | 12/10/2023 |
5.1.17 | 5,778 | 11/17/2023 |
5.1.16 | 2,134 | 11/16/2023 |
5.1.15 | 3,941 | 11/6/2023 |
5.1.14 | 3,249 | 10/30/2023 |
5.1.13 | 6,679 | 9/18/2023 |
5.1.12 | 4,029 | 9/11/2023 |
5.1.11 | 3,452 | 9/5/2023 |
5.1.10 | 1,933 | 9/4/2023 |
5.1.9 | 2,906 | 8/31/2023 |
5.1.8 | 2,262 | 8/30/2023 |
5.1.7 | 1,908 | 8/29/2023 |
5.1.6 | 8,145 | 8/8/2023 |
5.1.5 | 2,046 | 8/7/2023 |
5.1.4 | 6,654 | 7/13/2023 |
5.1.3 | 1,592 | 7/12/2023 |
5.1.2 | 1,872 | 7/10/2023 |
5.1.1 | 2,354 | 7/7/2023 |
5.1.0 | 568 | 7/7/2023 |
5.0.1 | 8,551 | 12/10/2022 |
5.0.0 | 701 | 12/10/2022 |
4.0.14 | 2,440 | 8/16/2022 |
4.0.13 | 828 | 8/16/2022 |
4.0.12 | 833 | 8/16/2022 |
4.0.11 | 13,201 | 3/22/2022 |
4.0.6 | 13,851 | 1/11/2022 |
4.0.5 | 726 | 1/11/2022 |
4.0.3 | 5,766 | 8/24/2021 |
4.0.2 | 11,448 | 6/15/2021 |
4.0.1 | 11,294 | 4/30/2021 |
4.0.0 | 14,099 | 1/6/2021 |
3.0.7 | 6,649 | 11/16/2020 |
3.0.4 | 8,582 | 9/13/2020 |
3.0.3 | 28,716 | 3/29/2020 |
3.0.2 | 34,194 | 2/16/2020 |
3.0.1 | 2,463 | 12/27/2019 |
3.0.0 | 10,716 | 11/23/2019 |
2.1.6 | 1,840 | 6/19/2019 |
2.1.5 | 8,879 | 4/16/2019 |
2.1.4 | 1,114 | 4/16/2019 |
2.1.3 | 9,306 | 2/21/2019 |
2.1.2 | 20,914 | 5/31/2018 |
2.1.1 | 6,933 | 5/31/2018 |
2.0.3 | 2,868 | 5/22/2018 |
2.0.2 | 5,024 | 5/21/2018 |
2.0.1 | 8,741 | 2/2/2018 |
2.0.0 | 10,609 | 1/2/2018 |
1.0.50 | 16,451 | 9/29/2017 |
1.0.49 | 28,383 | 8/24/2017 |
1.0.46 | 22,643 | 6/8/2017 |
1.0.45 | 1,435 | 6/8/2017 |
1.0.44 | 1,458 | 6/8/2017 |
1.0.43 | 1,403 | 6/8/2017 |
1.0.42 | 26,708 | 5/17/2017 |
1.0.41 | 9,747 | 3/22/2017 |
1.0.40 | 1,409 | 3/22/2017 |
1.0.35 | 7,496 | 1/23/2017 |
1.0.34 | 1,479 | 1/23/2017 |
1.0.33 | 1,734 | 1/18/2017 |
1.0.32 | 1,453 | 12/9/2016 |
1.0.31 | 6,015 | 12/9/2016 |
1.0.30 | 1,453 | 12/9/2016 |
1.0.26 | 5,694 | 11/18/2016 |
1.0.23 | 1,404 | 11/18/2016 |
1.0.21 | 1,474 | 11/18/2016 |
1.0.20 | 1,499 | 11/18/2016 |
1.0.19 | 2,210 | 10/28/2016 |
1.0.18 | 1,462 | 10/28/2016 |
1.0.17 | 1,465 | 10/28/2016 |
1.0.16 | 1,500 | 10/28/2016 |
1.0.15 | 1,447 | 10/28/2016 |
1.0.0 | 1,539 | 10/28/2016 |