LfrlAnvil.Dependencies 0.1.1

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

// Install LfrlAnvil.Dependencies as a Cake Tool
#tool nuget:?package=LfrlAnvil.Dependencies&version=0.1.1                

(root) NuGet Badge

LfrlAnvil.Dependencies

This project contains an implementation of an IoC container.

Documentation

Technical documentation can be found here.

Examples

Following are a few examples of how to create a container and how to resolve dependencies:

public interface IFoo { }

public interface IBar { }

public interface IQux { }

public class FooBar : IFoo, IBar { }

public class Qux : IQux
{
    public Qux(IFoo foo) { }
}

// creates a new empty IoC container builder
var builder = new DependencyContainerBuilder();

// registers a shared implementor type
// shared implementors can be used to configure multiple dependency types
// that use the same dependency resolver, as long as those dependency types have the same lifetime
builder.AddSharedImplementor<FooBar>();

// registers an IFoo interface as a resolvable dependency with Scoped lifetime
// that should be resolved by using the previously registered shared implementor
builder.Add<IFoo>().SetLifetime( DependencyLifetime.Scoped ).FromSharedImplementor<FooBar>();

// registers an IBar interface as a resolvable dependency with Scoped lifetime
// that should be resolved by using the previously registered shared implementor
// this means that IFoo and IBar resolutions from the same scope will returns the same FooBar instance
builder.Add<IBar>().SetLifetime( DependencyLifetime.Scoped ).FromSharedImplementor<FooBar>();

// defines a keyed locator builder
var keyed = builder.GetKeyedLocator( 42 );

// registers a keyed (with key equal to 42) IQux interface as a resolvable dependency with Singleton lifetime
// that should be implemented by Qux type
var quxBuilder = keyed.Add<IQux>().SetLifetime( DependencyLifetime.Singleton ).FromType<Qux>();

// the container builder will automatically attempt to find the best-suited implementor constructor
// but it is also possible to specify constructors explicitly
quxBuilder.FromConstructor( typeof( Qux ).GetConstructors().First() );

// builds the IoC container
var container = builder.Build();

// begins a new scope
// it's generally not a good practice to resolve dependencies directly from the root scope
// since it may lead to memory-leak-like behavior
// also, make sure to dispose began scopes once you're done with them
using var scope = container.RootScope.BeginScope();

// resolves IFoo instance
var foo = scope.Locator.Resolve<IFoo>();

// resolves IBar instance, which should return the same object
var bar = scope.Locator.Resolve<IBar>();

// resolves keyed IQux instance
var qux = scope.GetKeyedLocator( 42 ).Resolve<IQux>();

// resolves a range of IBar instances
// result will contain only one element, since IBar dependency has only been registered once
var barRange = scope.Locator.Resolve<IEnumerable<IBar>>();

// it is also possible to resolve the container itself
var c = scope.Locator.Resolve<IDependencyContainer>();

// as well as the current scope
var s = scope.Locator.Resolve<IDependencyScope>();
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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.2.1 105 6/16/2024
0.2.0 92 6/16/2024
0.1.1 99 5/29/2024
0.1.0 101 5/26/2024