JacopoWolf / StackInjector

Easy-to-use and fast dependency injection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


Stack Injector

Modern, easy-to-use and fast dependency injection framework.

Documentation
Download · Report Bug · Request Feature






Table of Contents

About

There are a lot of dependency injection frameworks for .NET, but every single one of them has some complicated specifics you have to learn (like active registration of components) and sometimes you don't really have full control of what to inject into your instances.

If you want to use an extremely easy dependency injection framework, use the latest features the C# language has to offer, or work with assemblies, then StackInjector is made for you!

Installation

Visit the Nuget page

Usage

What follows is a brief introduction for versions above 4.0, in-depth tutorials and explanations can be found in the repository's Wiki


Plan your components as interfaces!

interface IFoo
{
    string SomeMethod( string element );
}

And then completely forget about constructors!

using StackInjector.Attributes;

[Service]
class SimpleFoo : IFoo
{
    // use the specific class
    [Served]
    FooOptions options;

    // or let the library find an implementation!
    [Served]
    ILogger Logger { get; set; }

    // serve an Enumerable of every implementation of IMyFilter!
    [Served]
    IEnumerable<IMyFilter> filters; 
    

    public string SomeMethod( string element ) 
    {
        // those are just random examples!
        foreach ( var f in this.filters )
            f.Pass( element );
        
        if (this.options.something)
            this.Logger.Log(element)
        
        return element;
    }
}

Everything must be explicit ([Service],[Served] and [Ignored] attributes) allowing for extremely readable code and for granular control over the injection process.


All instances are held inside a StackWrapper, which exposes useful methods to manage your classes.

To initialize your application in a wrapper, you have multiple options, and every single one of them type-safe!

synchronous

a simple call to a static method and you're done!

Injector.From<IMyAppEntryPoint>().Entry.DoWork();

asynchronous

Thead safe, IDisposable, and centralized!

// the "using" keyword here allows for safe disposing of the resources
//  and terminating pending tasks with a CancellationToken
using var app = Injector.AsyncFrom<IMyAsyncAppEntryPoint>( (e,i,t) => e.AsyncWork(i,t) );

// can be called from anywhere
app.Submit( "someInput" );

// waiting for completed tasks is this simple
await foreach ( var result in app.Elaborated() )
    Console.WriteLine( result );

Contributing

Any contribution is appreciated! Especially bug reports, which mean you've been using this library I've been hard working on!

If you want to contribute code first read contributing and code of conduct (I promise they're short)

suggested editor: visualStudio

License

Distributed under the General Public Licence v3.0.

See LICENSE.

Acknowledgements


initial project and logo by @JacopoWolf, 04/2020

About

Easy-to-use and fast dependency injection

License:GNU General Public License v3.0


Languages

Language:C# 100.0%