Useful Types: Container, DefaultContainer, IContainer
License
—
Deps
2
Install Size
—
Vulns
✓ 0
Published
May 11, 2022
$ dotnet add package Rayson.DependencyInjectionWith absolutely zero setup, you can start writing code that uses dependency injection. There's no need to manually configure a container. I have always felt that having to write a configuration file for DI is so much hassle, and really isn't needed. The API is designed to be as thin and as easy as possible, while still providing some flexibility. Convention over Configuration!!
The static Container class contains static methods for instantiating objects and automatically resolving their dependencies. The resolution of the dependencies will store those dependencies in a cache ready for next time.
Getting a New Instance of an object
Interface obj = Container.ResolveInstance<Interface>();
// or
Class obj = Container.ResolveInstance<Class>();
Getting a Singleton
Interface obj = Container.ResolveSingleton<Interface>();
// or
Class obj = Container.ResolveSingleton<Class>();
Getting all possible components
Interface obj = Container.ResolveMany<Interface>();
// or
Class obj = Container.ResolveMany<Class>();
Getting a specific implementation
MySpecificClass obj = Container.ResolveSingleton<Interface>(nameof(MySpecificClass));
You may implement the IContainerAdapter interface in any way you like, and then set the static Container class's internal instance by calling...
Container.SetCurrent(yourCustomImplementation);
...All calls to Container methods will then use your custom implemenation. This gives you the freedomt to use any other DI framework under the hood.
Maybe... Just Try It! I promise you it'll be faster to start with than any of the major DI frameworks, I've tried most of them...
You will need to have your dependency assemblies in the same location as the class you're trying to resolve, this is where the DefaultContainer will search for assemblies and load them in. For example, if you resolve a class from an assembly which is in the "C:/MyLocation/Assemblies/" folder, all of the dependencies of that dll will need to also be in that folder.
YEP! But it's not thoroughly tested, so it may not work for everything. You can certainly get a DotNet but other classes have not been tested. I'm pretty sure would fail because it doesn't have a constructor, for example.
Dictionary<T,T2>StringOf course I do! There's a total of 24 sanity tests at the moment, and growing with every new feature I add. There shouldn't ever be something that works in one version that gets broken in a subsequent version, but bad things happen... If you ever find a bug, please report it to danrayson@hotmail.co.uk and I'll let you know when it's fixed.