Skip to content
This repository was archived by the owner on Feb 14, 2022. It is now read-only.

Examples

JacopoWolf edited this page Dec 19, 2020 · 2 revisions

Examples

⚠️ this page is still a work in progress 🔧

Table of Contents

Simple Injection

Foos.cs

using StackInjector.Attributes;

interface IFoo {
    int Fight();
}

[Service]
class Foo : IFoo {

    [Served]
    WeaponService fightWith;

    int Fight ( string enemy ) {
        // some logic here
    }
}

[Service]
class WeaponService {
    // some stuff in here
}

Main.cs

using StackInjector;

class Program{
    public static void Main (string[] args) {

        // if you ever used ASP.NET you should be familiar with this patter for managing settings.

        // you could also use StackWrapperSettings.Empty, but you'd have to set everything up scratch. 
        //note: if no setting is passed to From<T>() Default will be used

        var settings = 
            StackWrapperSettings
            .Default
            .RemoveUnusedTypesAfterInjection();


        // this is a StackWrapper. 
        //From here you can work with your dependency-injected stack of classes.
        var wrapper = Injector.From<IFoo>(settings);

        // Entry is an injected instance of the type you passed to From<T>
        wrapper.Entry.Fight( "a bad guy" );

        // if you prefer, you could use delegates
        wrapper.Start( foo => foo.Fight("dummy") );

        // you are just calling the Fight() method on a dependency injected instance of Foo
        // the first implementation of IFoo found in the current assembly
    }
}

Clone this wiki locally