Xtra.Extensions.DependencyInjection 1.4.2

dotnet add package Xtra.Extensions.DependencyInjection --version 1.4.2
                    
NuGet\Install-Package Xtra.Extensions.DependencyInjection -Version 1.4.2
                    
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="Xtra.Extensions.DependencyInjection" Version="1.4.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Xtra.Extensions.DependencyInjection" Version="1.4.2" />
                    
Directory.Packages.props
<PackageReference Include="Xtra.Extensions.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 Xtra.Extensions.DependencyInjection --version 1.4.2
                    
#r "nuget: Xtra.Extensions.DependencyInjection, 1.4.2"
                    
#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 Xtra.Extensions.DependencyInjection@1.4.2
                    
#: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=Xtra.Extensions.DependencyInjection&version=1.4.2
                    
Install as a Cake Addin
#tool nuget:?package=Xtra.Extensions.DependencyInjection&version=1.4.2
                    
Install as a Cake Tool

Xtra.Extensions.DependencyInjection

A set of extension methods for IServiceCollection to simplify service registration patterns in .NET applications, featuring factory registration and service bundles.

Features

  • Factory Pattern Registration: Easily register Func<T> factories that resolve services from the DI container.
  • Service Bundles: Group related service registrations into reusable modules using the IServiceBundle interface.
  • Fluent API: Maintains the standard IServiceCollection fluent interface.
  • Modern .NET Support: Targets netstandard2.1 and tested against .NET 8, 9, and 10.

Installation

Install via NuGet:

dotnet add package Xtra.Extensions.DependencyInjection

Usage

Factory Registration

The AddFactory methods allow you to register a service along with a Func<T> that can be used to create instances of that service.

1. Simple Factory

Registers MyService as transient and a Func<MyService> that resolves it.

services.AddFactory<MyService>();

// Usage in a consumer
public class Consumer(Func<MyService> factory)
{
    public void DoWork()
    {
        var service = factory(); // Resolves a new instance
    }
}
2. Interface with Implementation Factory

Registers IMyService as transient using MyService implementation, and a Func<IMyService>.

services.AddFactory<IMyService, MyService>();
3. Custom Factory Delegate

Registers a singleton factory delegate.

services.AddFactory<IMyService>(() => new MyService("custom configuration"));
4. Factory with IServiceProvider Access

Registers a factory that uses IServiceProvider to resolve dependencies.

services.AddFactory<IMyService>(sp => 
{
    var config = sp.GetRequiredService<IConfiguration>();
    return new MyService(config["Setting"]);
});

Service Bundles

Service bundles allow you to encapsulate multiple related registrations into a single class. They're inspired by Autofac's modules, and provide a way to organize and reuse service registrations.

Define a Bundle
public class DataAccessBundle : IServiceBundle
{
    public void Load(IServiceCollection services)
    {
        services.AddScoped<IUserRepository, UserRepository>();
        services.AddScoped<IProductRepository, ProductRepository>();
        services.AddSingleton<IDbConnectionFactory, DbConnectionFactory>();
    }
}
Register a Bundle

You can register bundles by type, instance, or multiple instances at once.

// By type (requires parameterless constructor)
services.AddServiceBundle<DataAccessBundle>();

// By instance
services.AddServiceBundle(new MessagingBundle("ConnectionString"));

// Multiple bundles
services.AddServiceBundles(new FooBundle(), new BarBundle());

// From a collection
var bundles = new List<IServiceBundle> { new CoreBundle(), new WebBundle() };
services.AddServiceBundles(bundles);

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.2 39 1/12/2026
1.4.1 45 1/12/2026
1.4.1-pre.5 41 1/12/2026
1.4.1-ci0002 38 1/12/2026
1.4.1-ci0001 43 1/12/2026
1.4.0 41 1/12/2026
1.3.1-ci0002 37 1/12/2026
1.3.0 186 12/19/2024
1.2.0 180 6/24/2024
1.1.1-ci0002 118 6/24/2024
1.1.1-ci0001 155 6/24/2024
1.1.0 200 9/25/2023
1.0.1-ci0001 163 9/25/2023
1.0.0 248 9/25/2023
0.1.0-ci0001 170 9/25/2023