Presolver 0.1.2

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

// Install Presolver as a Cake Tool
#tool nuget:?package=Presolver&version=0.1.2                

Presolver

Presolver is a simple dependency injection container for .NET to resolve dependencies at compile time, powered by Roslyn Source Generators. Inspired by stronginject.

NuGet

https://www.nuget.org/packages/Presolver/0.1.2

dotnet add package Presolver --version 0.1.2

How To Use

Define your services and interfaces.
Define your container class which derives from ContainerBase, and implement the interfaces with the services. You have to mark it partial and attach GenerateResolver attribute to it.

using Presolver;

interface IServiceA;

class ServiceA : IServiceA;

record ServiceB(IServiceA ServiceA);

[GenerateResolver]
public partial class Container : ContainerBase, ITransient<IServiceA, ServiceA>,
                                                ISingleton<ServiceB>;

Then you can resolve the services from the container.

using Presolver;
var container = new Container();
var a = container.Resolve<IServiceA>();
var b = container.Resolve<ServiceB>();
//var a2 = container.Resolve<ServiceA>();//Compile error

To create a child container, you can define a child container class which derives the ChildContainer<parent container class>. (By the way, I really like the primary constructor feature of C# 12.)

[GenerateResolver]
public partial class ChildContainer(Container parent) : ChildContainer<Container>(parent),
                                                        ITransient<IServiceA, ServiceA>;

If you don't have to register additional services, you can use CreateScope().

var container = new Container();
var scope = container.CreateScope();

Supported Scopes (Lifetimes)

  • Transient
    Create a new instance every time it is resolved.
  • Singleton
    Create a single instance and reuse it every time it is resolved.
    The instance will be disposed when the container is disposed.
  • Scoped
    Create a single instance per scope and reuse it within the same scope.
    The instance will be disposed when the container is disposed.

Registering Services

Constructors

If you want to use constructor injection, you can add the interfaces.

//Register class A as IService with scoped lifetime.
class Container :  IService<IServiceA,A,IScope.Scoped>;
//or
class Container :  IScoped<IServiceA,A>;

Factory methods

public partial class Container 
{
    [Factory] Transient<ServiceB> GetB(IServiceA a) => new (a);
//or [Factory] Transient<ServiceB,ServiceB> GetB(IService a) => new (a);
//or [Factory] Service<ServiceB,IScope.Transient> GetB(IService a) => new (a);
}

Instance (with method injection)

Instance registration is regard as a singleton, but Dispose method won't be called by default.

public class D(string name) : IDisposable
{
    B? b;
    [Inject]
    public void Ctor(IServiceA a)
    {
        this.b = b;
    }
}
public partial class Container(D d)
{
     //A flag to call injection method| A flag to call `Dispose` on container disposal
     [Instance(InstanceOptions.Inject | InstanceOptions.AddToContainer)]
     Singleton<D> D { get; }= d;
    //or [Instance(InstanceOptions.Inject | InstanceOptions.AddToContainer)]
    //D D => d;
}

Reusability feature

Interfaces are reusable.

interface IModuleAB<TScope> : ISingleton<IServiceA, A> ,IService<ServiceB,TScope>where TScope : struct, IScope;

Factories and Instances are also reusable.

public struct ModuleD<TScope>(D d)where TScope : struct, IScope
{
    [Instance(InstanceOptions.Inject | InstanceOptions.AddToContainer)]
    Singleton<D> D { get; }= d;
    [Factory]
    Service<int,TScope> GetInt ()=> 1;
}

public partial class Container(D d)
{
    [ModuleObject]
    ModuleD<IScope.Singleton> ModuleD { get; }= new (d);
}

LICENSE

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 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. 
.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.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

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.3.3 133 8/16/2024
0.3.2 119 8/12/2024
0.3.1 111 8/12/2024
0.3.0 109 8/12/2024
0.2.0 94 8/8/2024
0.1.2 95 8/8/2024
0.1.1 100 8/6/2024
0.1.0 80 8/5/2024