This repository was archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial
Jacopo edited this page May 9, 2020
·
10 revisions
StackInjector treats dependency injection as an object itself.
A stack of automatically instantiated and injected dependencies is wrapped in a StackWrapper, of which you can call the EntryPoint() method that starts the whole logic of your application.
To start, include StackInjector in your dependencies.
Then you can start right away writing some basic code:
using StackInjector;
class MyApp
{
public static void Main (string[] args)
{
Injector.From<IMyAppBase>.Start();
}
}let's now define IMyAppBase and an implementation. Let's then add another class, IAwesomeFilter
using StackInjector;
interface IMyAppBase : IStackEntryPoint
{
int ListenForInput();
}
[Service]
class MySimpleAppBase : IMyAppBase
{
[Served]
IAwesomeFilter Filter { get; set; }
public int ListenForInput()
{
// some abstract listening logic
// listened for something! quick, filter it
Console.WriteLine( this.Filter.IsNice( input ) );
}
public object EntryPoint()
{
// your base LOGIC starts HERE
// a simple example, loop of listening for some input to manage for 100 times
for ( int i = 0; i < 100; i++ )
this.ListenForInput();
// since EntryPoint must return an object
// if you don't return anything simply return null
return null;
}
}Now, if you got it, the IAwesomeFilter implementation will look something like the following:
[Service]
class MyAwesomeFilter : IAwesomeFilter
{
public bool IsNice ( int input ) => input == 69;
}Now breaking down a couple of concepts:
- service classes must be explicitly marked with
[Service], otherwise an exception is thrown. This behaviour is possible to modify by changing theStackWrapperSettingsoptions. - injected dependencies must be explicitly marked with
[Served], otherwise they'll be left tonull. -
Injector.From<T>()accepts only a type of class/interface extendingIStackEntryPoint. -
IStackEntryPoint.EntryPoint()must return an object. If in your logic there's no return type,return null.