InterfaceFactory.ContainerAdapter.DependencyInjection
1.0.0
See the version list below for details.
dotnet add package InterfaceFactory.ContainerAdapter.DependencyInjection --version 1.0.0
NuGet\Install-Package InterfaceFactory.ContainerAdapter.DependencyInjection -Version 1.0.0
<PackageReference Include="InterfaceFactory.ContainerAdapter.DependencyInjection" Version="1.0.0" />
<PackageVersion Include="InterfaceFactory.ContainerAdapter.DependencyInjection" Version="1.0.0" />
<PackageReference Include="InterfaceFactory.ContainerAdapter.DependencyInjection" />
paket add InterfaceFactory.ContainerAdapter.DependencyInjection --version 1.0.0
#r "nuget: InterfaceFactory.ContainerAdapter.DependencyInjection, 1.0.0"
#:package InterfaceFactory.ContainerAdapter.DependencyInjection@1.0.0
#addin nuget:?package=InterfaceFactory.ContainerAdapter.DependencyInjection&version=1.0.0
#tool nuget:?package=InterfaceFactory.ContainerAdapter.DependencyInjection&version=1.0.0
InterfaceFactory.ContainerAdapter.DependencyInjection
InterfaceFactory.ContainerAdapter.DependencyInjection is the Microsoft.Extensions.DependencyInjection adapter for InterfaceFactory. This package bridges the gap between InterfaceFactory's container-agnostic service registration and resolution, and the native dependency injection provided by Microsoft's DI container.
NOTE: This adapter is intended to be used with the core InterfaceFactory project. For details on the core concepts and usage, please see the InterfaceFactory README.
Table of Contents
- Overview
- Features
- Installation
- Getting Started
- Usage Examples
- Configuration & Customizations
- Contributing
- License
Overview
This adapter package integrates InterfaceFactory with Microsoft.Extensions.DependencyInjection, enabling you to:
- Automatically scan assemblies and register services with your DI container.
- Use extension methods to streamline the configuration of the interface factory using Microsoft’s ServiceCollection.
- Leverage keyed and non-keyed registrations with minimal boilerplate code.
Under the hood, the adapter implements the IContainerResolveAdapter and IContainerRegisterAdapter interfaces. It delegates all DI operations (such as singleton, scoped, and transient registrations) directly to the Microsoft DI container using extension methods like AddKeyedSingleton, AddKeyedScoped, and AddKeyedTransient.
Features
- Seamless Integration: Easily register all interface factories into your Microsoft DI container.
- Dynamic Assembly Scanning: Automatically detects and registers services from all loaded (or optionally unloaded) assemblies.
- Keyed Registrations Support: Register multiple implementations of the same interface under different keys.
- Centralized Resolution: Configure a single point of service resolution through the central container adapter.
Installation
You can install the adapter via NuGet. Use one of the following commands:
Using .NET CLI:
dotnet add package InterfaceFactory.ContainerAdapter.DependencyInjection
Using Package Manager:
Install-Package InterfaceFactory.ContainerAdapter.DependencyInjection
Getting Started
To set up the adapter in your application, follow these steps:
Add the NuGet package
Install the adapter package into your project.Register Interface Factories
In your startup or composition root, configure your IServiceCollection as follows:using Microsoft.Extensions.DependencyInjection; using InterfaceFactory.ContainerAdapter.DependencyInjection; // Create a new service collection var serviceCollection = new ServiceCollection(); // Register all interface factories // The optional 'includeUnloadedAssemblies' flag will scan for services in assemblies not yet loaded. serviceCollection.RegisterInterfaceFactories(includeUnloadedAssemblies: true); // Build the ServiceProvider var serviceProvider = serviceCollection.BuildServiceProvider();Configure the Container Adapter
Set the built ServiceProvider as the active container adapter:// Make the service provider available to InterfaceFactory serviceProvider.UseInterfaceFactory();
Once configured, you can resolve your services via your interfaces extending IFactory<T> (as defined in the core InterfaceFactory package).
Usage Examples
Example 1: Basic Service Registration & Resolution
// Define an interface that extends IFactory<T>
public interface IExample : IFactory<IExample> { }
// Implement the interface with a class decorated with ContainerRegistration attribute
[ContainerRegistration(ServiceLifetime.Scoped, "MyExample")]
public class MyExample : IExample { }
// In your application startup:
using Microsoft.Extensions.DependencyInjection;
var serviceCollection = new ServiceCollection();
serviceCollection.RegisterInterfaceFactories(includeUnloadedAssemblies: false);
var serviceProvider = serviceCollection.BuildServiceProvider();
serviceProvider.UseInterfaceFactory();
// Resolve service using the default method extended through IFactory<T>
IExample instance = IExample.GetRequiredKeyedInstance("MyExample");
Example 2: Keyed Registrations with Multiple Implementations
// Define the interface
public interface IReport : IFactory<IReport> { }
// Implement two separate versions of the interface
[ContainerRegistration(ServiceLifetime.Transient, "SimpleReport")]
public class SimpleReport : IReport { }
[ContainerRegistration(ServiceLifetime.Transient, "DetailedReport")]
public class DetailedReport : IReport { }
// In your configuration:
var serviceCollection = new ServiceCollection();
serviceCollection.RegisterInterfaceFactories(includeUnloadedAssemblies: true);
var serviceProvider = serviceCollection.BuildServiceProvider();
serviceProvider.UseInterfaceFactory();
// Resolve services by their key
IReport simpleReport = IReport.GetRequiredKeyedInstance("SimpleReport");
IReport detailedReport = IReport.GetRequiredKeyedInstance("DetailedReport");
Configuration & Customizations
Keyed Registrations
The adapter supports keyed registrations. When you decorate your implementation with the ContainerRegistration attribute and specify a key, the adapter uses extension methods like AddKeyedSingleton, AddKeyedScoped, or AddKeyedTransient to register the service in the Microsoft DI container.
Note: The actual implementation of these keyed extension methods must be available in your solution. If you’re using a third-party library that provides keyed registrations, ensure that it is referenced accordingly.
Adjusting Scanning Behavior
The extension method RegisterInterfaceFactories accepts a boolean parameter includeUnloadedAssemblies. Set this flag to true if you want the adapter to scan for additional assemblies in the current folder that have not yet been loaded. This allows for a more comprehensive registration but may impact startup performance if many assemblies are present.
Further Customizations
If your project has specific DI requirements or you want to hook into the registration or resolution process, consider creating your own adapter that extends IContainerResolveAdapter and IContainerRegisterAdapter. The Microsoft adapter provided in this package is designed to be simple and straightforward, but it is flexible enough to support customization if needed.
Contributing
Contributions are welcome! Please refer to the Contributing Guidelines in the main repository for more details on how to contribute, report issues, or propose improvements.
License
This project is licensed under the MIT License. See the LICENSE file for full license information.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- InterfaceFactory (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on InterfaceFactory.ContainerAdapter.DependencyInjection:
| Package | Downloads |
|---|---|
|
WK.OpenAiWrapper
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.