-
Notifications
You must be signed in to change notification settings - Fork 24
Home
StrongInject is a compile time IOC framework for C#, utilizing the new roslyn Source Generators feature.
If the type you're resolving isn't registered you get an error at compile time, not runtime.
There's no dictionary lookups, no runtime code generation. Just the fastest code it's possible to generate to resolve your type.
You can't use the container as a service locator. You can't forget to dispose the resolved types.
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/.
StrongInject fully supports async initialization and disposal, a feature sorely lacking in many IOC containers.
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.
Once an instance is no longer needed, StrongInject takes care of disposing it for you.
- Install the package from NuGet
- Create a container for the type you want to resolve, and register any dependencies:
To find out more about registration see the documentation.
using StrongInject; [Register(typeof(A))] [Register(typeof(B))] public class MyContainer : IContainer<A>() {} public class A { public A(B b){} } public class B {}
- Use the container:
To find out more about resolution see the documentation.
var myContainer = new MyContainer(); myContainer.Run(a => Console.WriteLine($"We've resolved an instance of A: {a.ToString()}!!"));