ServiceCollectionBuilder 0.1.2-beta

This is a prerelease version of ServiceCollectionBuilder.
dotnet add package ServiceCollectionBuilder --version 0.1.2-beta
NuGet\Install-Package ServiceCollectionBuilder -Version 0.1.2-beta
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="ServiceCollectionBuilder" Version="0.1.2-beta" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ServiceCollectionBuilder --version 0.1.2-beta
#r "nuget: ServiceCollectionBuilder, 0.1.2-beta"
#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.
// Install ServiceCollectionBuilder as a Cake Addin
#addin nuget:?package=ServiceCollectionBuilder&version=0.1.2-beta&prerelease

// Install ServiceCollectionBuilder as a Cake Tool
#tool nuget:?package=ServiceCollectionBuilder&version=0.1.2-beta&prerelease

ServiceCollectionBuilder

Utility that helps to build the dependency graph used by interconnected services by using custom attributes, instead of maintaining a manual service configuration.

It integrates with Microsoft.Extensions.DependencyInjection by providing a IServiceCollection object pre configured with the dependant services.

Available on nuget

Usage

Lest assume that our main component is the IHiFiveService interface.

public interface IHiFiveService
{
    int HiFive();
}

and it implementation details need a ILogger instance.

public class ServiceImpl : IHiFiveService
{

    private readonly ILogger _logger;

    public ServiceImpl(ILogger logger) {
        this._logger = logger;
    }

    public int HiFive()
    {
        _logger.debug("Sending HiFive");
        return 5;
    }
}

We can wire them up like

[ServiceImplementation(typeof(IHiFiveService))]
public class ServiceImpl : IHiFiveService{
    ...
}

[ServiceImplementation(typeof(ILogger))]
public class ConsoleLogger : ILogger {
    ...
}

and get a fully constructed IHiFiveService

IHiFiveService service = SCBuilder
    .FromCurrentAssembly()
    .BuildServiceProvider()
    .GetService<IHiFiveService>();

Components

Is possible to assign a component ID to each ServiceImplementation in order to group services that should work together. This way the same assembly can be used to supply instances of IServiceCollection with diffent services.

Scoped Contexts

One of the limitations I found with the current IServiceCollection is that the options pattern registers a Singleton service for context data.

Inspired by the same pattern ContextSpiBuilder can create Scoped IServiceProvider instances passing a different context instance every time.

Services can access the scope context by requiring a depedency of type IScopeCtx<T> where T is the context type.

public class Context
{
    public int Value { get; }

    public Context(int value = 0) { Value = value; }
}

[ServiceImplementation(typeof(ScopedService), scope:Attributes.Scope.Scoped, component:"scoped")]
public class ScopedService
{
    private readonly Context _context;

    public ScopedService(IScopeCtx<Context> context) { _context = context.Context; }

    public int GetContextValue() { return _context.Value; }
}

public void TestScopeBuilder()
{
    ContextSpiBuilder<Context> sb = SCBuilder.Create()
        .AddAssembly(Assembly.GetExecutingAssembly(), "scoped")
        .Context<Context>();

    var value1 = sb.CreateScope(new Context(123)).GetRequiredService<ScopedService>().GetContextValue();
    Assert.Equal(123, value1);
    
    var value2 = sb.CreateScope(new Context(999)).GetRequiredService<ScopedService>().GetContextValue();
    Assert.Equal(999, value2);
}

Changelog

  • v0.1.2-beta Applied namespace conventions. Added more documentation.

  • v0.1.0-beta Initial beta release

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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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
0.1.2-beta 700 7/21/2018
0.1.1-beta 724 7/17/2018
0.1.0-beta 738 7/16/2018