InterfaceFactory 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package InterfaceFactory --version 0.1.0
                    
NuGet\Install-Package InterfaceFactory -Version 0.1.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" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="InterfaceFactory" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="InterfaceFactory" />
                    
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 --version 0.1.0
                    
#r "nuget: InterfaceFactory, 0.1.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@0.1.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&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=InterfaceFactory&version=0.1.0
                    
Install as a Cake Tool

InterfaceFactory

InterfaceFactory is a lightweight, extensible factory solution for C# that integrates with Microsoft’s Dependency Injection container. It provides a factory abstraction over DI by:

  • Defining a Generic Factory Interface: IFactory<T> with static helper methods to resolve services.
  • Container Adapter Abstraction: An abstraction via IContainerAdapter to support registration and resolution from various IoC containers.
  • Attribute-Based Registration: Using ContainerRegistrationAttribute to annotate concrete classes for automatic registration with correct lifetimes and keys.
  • Automated Assembly Scanning: Automatically scans assemblies to register eligible services with minimal configuration.

Features

  • Generic Factory Interface – IFactory<T>
    Provides static helper methods (GetInstance, GetRequiredInstance, GetKeyedInstance, and GetRequiredKeyedInstance) to resolve services from the DI container.

  • Container Adapter Abstraction – IContainerAdapter
    Decouples application code from the specific DI container implementation. Currently, it supports Microsoft.Extensions.DependencyInjection.

  • Attribute-Based Registration – ContainerRegistrationAttribute
    Simplifies service registration by decorating concrete classes with the desired service lifetime (Singleton, Scoped, or Transient) and an optional registration key.

  • Automated Assembly Scanning
    Uses the ContainerRegistration.RegisterInterfaceFactories method to scan assemblies and automatically register services based on defined conventions.


Installation

Install the NuGet package via the Package Manager Console:

Install-Package InterfaceFactory

Or via the .NET CLI:

dotnet add package InterfaceFactory

Usage

1. Registering Services

In your application startup, create and configure your ServiceCollection as usual. Then automatically register your factories by scanning the loaded (or local folder) assemblies:

using InterfaceFactory;
using InterfaceFactory.ContainerAdapter.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

// Create the ServiceCollection
ServiceCollection serviceCollection = new ServiceCollection();

// Optionally, include additional assemblies from the current folder
serviceCollection.RegisterInterfaceFactories(includeUnloadedAssemblies: true);

// Build the ServiceProvider
ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

// Set the active container adapter (this wires up ContainerAdapterContainer.Instance)
serviceProvider.UseInterfaceFactory();

2. Defining a Factory Interface and Concrete Implementation

Define an interface that extends IFactory<T> and implement it. Optionally add the ContainerRegistrationAttribute to control lifetime and key registration:

public interface IExample : IFactory<IExample> { }

[ContainerRegistration(ServiceLifetime.Scoped, nameof(MyExample))]
public class MyExample : IExample { }

3. Resolving Services

Once registered, resolve your service using the static factory methods defined on IFactory<T>:

// Returns null if the service is not registered
IExample? example1 = IExample.GetInstance();

// Throws an exception if the keyed service is not available
IExample example2 = IExample.GetRequiredKeyedInstance(nameof(MyExample));

Configuration & Conventions

  • Automatic Service Registration
    The ContainerRegistration.RegisterInterfaceFactories method scans all loaded assemblies and locates concrete classes that implement an interface extending IFactory<T>. It then registers these services using the provided lifetime from the ContainerRegistrationAttribute.

  • Customizing Container Behavior
    The container adapter used by InterfaceFactory is managed via the static ContainerAdapterContainer.Instance. An implementation integrating with Microsoft.Extensions.DependencyInjection is provided by default. You can implement and set your own IContainerAdapter if needed.


Contributing

Contributions are welcome! If you find bugs or have feature requests, please open an issue or submit a pull request on GitHub.


License

This project is licensed under the MIT License. See the LICENSE file for more details.


Contact

For questions or additional information, please contact [Your Name or Support Email].


Notes

  • Ensure the container adapter is properly set (e.g., by calling serviceProvider.UseInterfaceFactory()) before resolving services via the static methods on IFactory<T>.
  • Including unloaded assemblies is optional. Note that scanning additional assemblies may affect startup performance, so use this feature based on your project's needs.

Happy coding with InterfaceFactory!

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.
  • net8.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on InterfaceFactory:

Package Downloads
WK.OpenAiWrapper

Package Description

InterfaceFactory.ContainerAdapter.DependencyInjection

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 205 4/10/2025
1.0.3 137 3/29/2025
1.0.2 126 2/19/2025
1.0.1 105 2/19/2025
1.0.0 110 2/19/2025
0.1.4 114 2/19/2025
0.1.3 105 2/13/2025
0.1.2 108 2/7/2025
0.1.1 111 2/6/2025
0.1.0 111 2/6/2025