InterfaceFactory.ContainerAdapter.DependencyInjection 1.0.0

There is a newer version of this package available.
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
                    
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="InterfaceFactory.ContainerAdapter.DependencyInjection" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="InterfaceFactory.ContainerAdapter.DependencyInjection" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="InterfaceFactory.ContainerAdapter.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add InterfaceFactory.ContainerAdapter.DependencyInjection --version 1.0.0
                    
#r "nuget: InterfaceFactory.ContainerAdapter.DependencyInjection, 1.0.0"
                    
#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.
#:package InterfaceFactory.ContainerAdapter.DependencyInjection@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=InterfaceFactory.ContainerAdapter.DependencyInjection&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=InterfaceFactory.ContainerAdapter.DependencyInjection&version=1.0.0
                    
Install as a Cake Tool

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

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:

  1. Add the NuGet package
    Install the adapter package into your project.

  2. 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();
    
  3. 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 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. 
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 InterfaceFactory.ContainerAdapter.DependencyInjection:

Package Downloads
WK.OpenAiWrapper

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 343 4/10/2025
1.0.3 189 3/29/2025
1.0.2 170 2/19/2025
1.0.1 156 2/19/2025
1.0.0 152 2/19/2025
0.1.4 153 2/19/2025
0.1.3 163 2/13/2025
0.1.2 159 2/7/2025
0.1.1 147 2/6/2025