StrangeIOC

JN
by JC Nuevas17 views·Updated Dec 11, 2014
Powered by AI

Need to present on this topic?

Create your own polished presentation in minutes. Just describe what you need.

Free to try, no credit card required

Transcript

Binding/Injecting Commands

commandBinder.Bind<StartSignal>().

To<StartCommand>();

class StartCommand : EventCommand {

override public void Execute() {

Debug.Log("GameStarted");

}

}

class GameStarter : Mediator {

[Inject]

public StartSignal startSignal { get; set; }

public overrride void OnRegister() {

startSignal.Dispatch();

}

}

Command

  • Controllers in the classic Model-View-Controller-Service structure.
  • CommandBinder listens to every dispatch from the dispatcher.

public class GameStarter : Mediator {

[Inject]

public IntSignal intSignal { get; set; }

public override void OnRegister() {

intSignal.Dispatch(20);

}

}

Signals

  • Dispatches mechanism / events that is type-safe.

Example:

public class IntSignal : Signal<int> {}

injectionBinder.Bind<IntSignal>().ToSingleton();

Injection

  • Use [Inject] tag to create an instance of the class you injected earlier. Without that [Inject] tag, 'weapon' is now just a regular getter/setter.

class Spaceship : ISpaceship

{

public void input(float angle, float velocity)

{

//do stuff here

}

[Inject] //<----- The magic word!

public IWeapon weapon{get;set;}

}

Extensions

  • Injection
  • Command
  • Signals

Examples:

Bind<Spaceship>().To<SampleSpaceship>();

Bind(“TotalDeaths”).To(42);

Bind<Spaceship>().To(“SampleSpaceship2”);

Bind<IComputer>().To<SuperComputer>().ToName(“EnthusiastBuild”);

Binding

  • Ties interfaces to a class that implements that interface.
  • Required parts are a key and a value.

injectionBinder.Bind<InterfaceHere>().To<ImpClassHere>();

StrangeIOC

  • Is a super-lightweight and highly extensible Inversion-of-Control (IoC) framework, written specifically for C# and Unity.

To make it simple:

  • Dependency injection is basically providing the objects that an object needs (its dependencies) instead of having it construct them itself.
  • Inversion-of-Control is being able to swap out an engine in a car right before you turn it on. If everything hooks up right (the interface), you are good.

public class TextEditor

{

private ISpellChecker checker;

public TextEditor(ISpellChecker checker)

{

this.checker = checker;

}

}

Example:

public class TextEditor

{

private SpellChecker checker;

public TextEditor()

{

checker = new SpellChecker();

}

}

Dependency Injection

  • Instead of having your objects creating a dependency or asking a factory object to make one for them, you pass the needed dependencies in to the constructor or via property setters, and you make it somebody else's problem

Inversion-of-Control

  • Is about getting freedom, more flexibility, and less dependency.
  • Main classes aggregating other classes should not depend on the direct implementation of the aggregated classes.
  • Abstraction should not depend on details, details should depend on abstraction.

What is:

  • Inversion-of-Control (IoC)
  • Dependency Injection (DI)
  • StrangeIOC

StrangeIOC