IoC.AspNetCore.Source 1.1.0-beta217

This is a prerelease version of IoC.AspNetCore.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.AspNetCore.Source --version 1.1.0-beta217
NuGet\Install-Package IoC.AspNetCore.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.AspNetCore.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.AspNetCore.Source --version 1.1.0-beta217
#r "nuget: IoC.AspNetCore.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.AspNetCore.Source as a Cake Addin
#addin nuget:?package=IoC.AspNetCore.Source&version=1.1.0-beta217&prerelease

// Install IoC.AspNetCore.Source as a Cake Tool
#tool nuget:?package=IoC.AspNetCore.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

There are no supported framework assets in this 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.3.8 357 1/14/2023
1.3.7 349 12/2/2021
1.3.6 489 7/12/2021
1.3.4 368 2/16/2021
1.3.3 436 11/11/2020
1.3.2 509 7/10/2020
1.3.0 541 4/29/2020
1.2.2 568 2/6/2020
1.2.0 596 12/22/2019
1.1.15 612 9/24/2019
1.1.14 611 3/21/2019
1.1.13 623 3/8/2019
1.1.12 722 2/5/2019
1.1.11 772 10/27/2018
1.1.10 784 10/8/2018
1.1.9 794 10/7/2018
1.1.8 771 9/26/2018
1.1.7 819 8/16/2018
1.1.6 852 7/19/2018