IoC.Container.Source 1.1.0-beta217

This is a prerelease version of IoC.Container.Source.
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.

Requires NuGet 3.3.0 or higher.

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

// Install IoC.Container.Source as a Cake Tool
#tool nuget:?package=IoC.Container.Source&version=1.1.0-beta217&prerelease

Simple, powerful and fast IoC container

License

IoC.Container provides the following benefits:

NuGet packages:

  • IoC.Container
    • NuGet
  • IoC.Container.Source (embedding in code)
    • NuGet
  • IoC.AspNetCore
    • NuGet
  • IoC.AspNetCore.Source (embedding in code)
    • NuGet

Supported platforms:

Schrödinger's cat shows how it works

The reality is that

Cat

Let's create an abstraction

interface IBox<out T> { T Content { get; } }

interface ICat { bool IsAlive { get; } }

Here is our implementation

class CardboardBox<T> : IBox<T>
{
    public CardboardBox(T content) => Content = content;

    public T Content { get; }

    public override string ToString() => Content.ToString();
}

class ShroedingersCat : ICat
{
    public bool IsAlive => new Random().Next(2) == 1;

    public override string ToString() => $"Is alive: {IsAlive}";
}

It is important to note that our abstraction and our implementation do not know anything about IoC containers

Add the package reference

IoC.Container ships entirely as NuGet packages. Using NuGet packages allows you to optimize your application to include only the necessary dependencies.

  • Package Manager

    Install-Package IoC.Container
    
  • .NET CLI

    dotnet add package IoC.Container
    

Let's glue all together

class Glue : IConfiguration
{
  public IEnumerable<IDisposable> Apply(IContainer container)
  {
    yield return container.Bind<IBox<TT>>().To<CardboardBox<TT>>();
    yield return container.Bind<ICat>().To<ShroedingersCat>();
  }
}

Just configure the container and check it works as expected

using (var container = Container.Create().Using<Glue>())
{
    var box = container.Resolve<IBox<ICat>>();
    Console.WriteLine(box);

    // Func
    var func = container.Resolve<Func<IBox<ICat>>>();
    Console.WriteLine(func());

    // Async
    box = await container.Resolve<Task<IBox<ICat>>>();
    Console.WriteLine(box);

    // Async value
    box = await container.Resolve<ValueTask<IBox<ICat>>>();
    Console.WriteLine(box);

    // Tuple<,>
    var tuple = container.Resolve<Tuple<IBox<ICat>, ICat>>();
    Console.WriteLine(tuple.Item1 + ", " + tuple.Item2);

    // ValueTuple(,,)
    var valueTuple = container.Resolve<(IBox<ICat> box, ICat cat, IBox<ICat> anotherBox)>();
    Console.WriteLine(valueTuple.box + ", " + valueTuple.cat + ", " + valueTuple.anotherBox);

    // Lazy
    var lazy = container.Resolve<Lazy<IBox<ICat>>>();
    Console.WriteLine(lazy.Value);

    // Enumerable
    var enumerable = container.Resolve<IEnumerable<IBox<ICat>>>();
    Console.WriteLine(enumerable.Single());

    // List
    var list = container.Resolve<IList<IBox<ICat>>>();
    Console.WriteLine(list[0]);
}

Under the hood

Actually these resolvers are represented just as a set of operators new which allow to create (or to get) required instances.

var box = new CardboardBox<ShroedingersCat>(new ShroedingersCat());

There is only one difference - these resolvers are wrapped to compiled lambda-functions and an each call of these lambda-functions spends some minimal time in the operator call, but in actual scenarios it is not required to make these calls each time to create instances. When some dependencies are injected to an instance they are injected without any lambdas at all but just as a minimal set of instruction to create these dependencies:

new ShroedingersCat()

Thus this IoC container makes the minimal impact in terms of perfomrance and of memory trafic on a creation of instances of classes and might be used everywhere and everytime in accordance with the SOLID principles.

ASP.NET Core

Add the package reference

  • Package Manager

    Install-Package IoC.AspNetCore
    
  • .NET CLI

    dotnet add package IoC.AspNetCore
    

Change IoC container and configure it at Startup

public IServiceProvider ConfigureServices(IServiceCollection services)
{
  services.AddMvc().AddControllersAsServices();

  // Create container
  var container = Container.Create().Using(new AspNetCoreFeature(services));

  // Configure container
  container.Using<Glue>();

  // Resolve IServiceProvider
  return container.Resolve<IServiceProvider>();
}

For more information see this sample.

Class References

Why this one?

The results of the comparison tests for some popular IoC containers like Castle Windsor, Autofac, Unity, Ninject ...

Cat

Product Compatible and additional computed target framework versions.
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on IoC.Container.Source:

Package Downloads
IoC.AspNetCore.Source

AspNetCore feature for expressions based Inversion of Control container for .NET as embedding-in-code package.

IoC.Interception.Source

Interception feature for expressions based Inversion of Control container for .NET as embedding-in-code package.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.8 606 1/14/2023
1.3.7 550 12/2/2021
1.3.6 745 7/12/2021
1.3.4 643 2/16/2021
1.3.3 769 11/11/2020
1.3.2 796 7/10/2020
1.3.0 926 4/29/2020
1.2.2 866 2/6/2020
1.2.0 978 12/22/2019
1.1.15 954 9/24/2019
1.1.14 1,017 3/21/2019
1.1.13 983 3/8/2019
1.1.12 1,102 2/5/2019
1.1.11 1,141 10/27/2018
1.1.10 1,141 10/8/2018
1.1.9 1,148 10/7/2018
1.1.8 1,119 9/26/2018
1.1.7 1,002 8/16/2018
1.1.6 985 7/19/2018