Skip to content
YairHalberstadt edited this page Aug 9, 2021 · 11 revisions

Table Of Contents

StrongInject

StrongInject is a compile time IOC framework for C#, utilizing the new roslyn Source Generators feature.

Aims

Compile time checked dependency injection

If the type you're resolving isn't registered you get an error at compile time, not runtime.

Fast

There's no dictionary lookups, no runtime code generation. Just the fastest code it's possible to generate to resolve your type.

Encourage best practices

You can't use the container as a service locator. You can't forget to dispose the resolved types.

No reflection or runtime code generation

Instead StrongInject uses roslyn Source Generators, meaning it's fast, and works well on UWP/IOS too. This also means it's linker friendly - see https://devblogs.microsoft.com/dotnet/app-trimming-in-net-5/.

Async support

StrongInject fully supports async initialization and disposal, a feature sorely lacking in many IOC containers.

Concepts

A container is esentially a factory that knows how to provide an instance of a type on demand, and then dispose of it once it's no longer needed.

Registration is how you let your container know what it can use, and how, to try and create that instance.

Resolution is how the container create/provides an instance of a type. This can be when you ask for the instance directly, or it may be needed as a dependency for another resolution.

Disposal

Once an instance is no longer needed, StrongInject takes care of disposing it for you.

Getting Started

  1. Install the package from NuGet
  2. Create a container for the type you want to resolve, and register any dependencies:
    using StrongInject;
    
    [Register(typeof(A))]
    [Register(typeof(B))]
    public class MyContainer : IContainer<A>() {}
    
    public class A { public A(B b){} }
    public class B {}
    To find out more about registration see the documentation.
  3. Use the container:
    var myContainer = new MyContainer();
    myContainer.Run(a => Console.WriteLine($"We've resolved an instance of A: {a.ToString()}!!"));
    To find out more about resolution see the documentation.