InterfaceFactory 0.1.0
See the version list below for details.
dotnet add package InterfaceFactory --version 0.1.0
NuGet\Install-Package InterfaceFactory -Version 0.1.0
<PackageReference Include="InterfaceFactory" Version="0.1.0" />
<PackageVersion Include="InterfaceFactory" Version="0.1.0" />
<PackageReference Include="InterfaceFactory" />
paket add InterfaceFactory --version 0.1.0
#r "nuget: InterfaceFactory, 0.1.0"
#:package InterfaceFactory@0.1.0
#addin nuget:?package=InterfaceFactory&version=0.1.0
#tool nuget:?package=InterfaceFactory&version=0.1.0
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
, andGetRequiredKeyedInstance
) 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
, orTransient
) and an optional registration key.Automated Assembly Scanning
Uses theContainerRegistration.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
TheContainerRegistration.RegisterInterfaceFactories
method scans all loaded assemblies and locates concrete classes that implement an interface extendingIFactory<T>
. It then registers these services using the provided lifetime from theContainerRegistrationAttribute
.Customizing Container Behavior
The container adapter used by InterfaceFactory is managed via the staticContainerAdapterContainer.Instance
. An implementation integrating withMicrosoft.Extensions.DependencyInjection
is provided by default. You can implement and set your ownIContainerAdapter
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 onIFactory<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 | 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
- 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.