Skip to content

Cheatsheet

/// <summary>
/// This class showcase multiple ways to perform binding with the <see cref="IBinder"/>.
/// Please note that the class is just a collection of examples.
///
/// The following code does not make sense and would result in <see cref="BindingException"/> thrown.
/// </summary>
internal sealed class Cheatsheet
{
    public void Transient()
    {
        IBinder binder = new Container();

        //////// Binding to a type

        //Create a new instance of Foo every time when Resolve<IFoo>() is called.
        //Conclude() must be called every time we bind.
        binder.Bind<IFoo>().To<Foo>().AsTransient().Conclude();

        //Unbind removes the IBinding from the Binder as there can only be one IBinding per type and id.
        //Unbind should be called to reset the binding every time.
        //For simplicity reason, we will assume the Unbind is called after every usage in the example.
        binder.Unbind<IFoo>();

        //This is the non-generic equivalent as the above.
        //Please note, AsTransient() is called by default so it can be left out.
        binder.Bind(typeof(IFoo)).To<Foo>().Conclude();

        //This will not work as we cannot bind IFoo to an abstract class.
        binder.Bind<IFoo>().To<AbstractFoo>().Conclude();

        //Create a new instance of Foo every time only when Resolve<Foo>() is called (not Resolve<IFoo>).
        binder.Bind<Foo>().To<Foo>().Conclude();

        //Create a new instance of Foo every time only when Resolve<AbstractFoo>() is called.
        binder.Bind<AbstractFoo>().To<Foo>().Conclude();

        //Create new instance of Foo every time only when Resolve<IFoo>("foo") is called.
        binder.Bind<IFoo>().To<Foo>().ToId("foo").Conclude();

        //Create new instance of Bar every time only when Resolve<IFoo>("bar") is called.
        binder.Bind<IFoo>().To<Bar>().ToId("bar").Conclude();

        //////// Binding to an instance
        IFoo foo = new Foo();
        IFoo bar = new Bar();

        //Return the foo instance every time only when Resolve<IFoo>("foo") is called.
        binder.Bind<IFoo>().ToInstance(foo).Conclude();

        //Return the bar instance every time only when Resolve<Foo>("bar") is called.
        binder.Bind<Foo>().ToInstance(bar).Conclude();

        //////// Binding to a method

        //Create a new instance of Foo using the method provided every time when Resolve<IFoo>() is called.
        binder.Bind<IFoo>().FromMethod(() => new Foo()).Conclude();

        //////// Binding using information from an existing binding
        IBinding binding = binder.Bind<IFoo>().To<Foo>().ToId("foo").Conclude();
        binder.Bind<IFoo>().FromProvider(binding.provider).Conclude();
    }

    public void Cached()
    {
        IBinder binder = new Container();

        //Create a new instance of Foo every time when Resolve<IFoo>() is called the first time.
        //Subsequent call of Resolve<IFoo>() would return the same instance.
        binder.Bind<IFoo>().To<Foo>().AsCached().Conclude();

        //Create a new instance of Foo every time when Resolve<IFoo>() is called the first time.
        //Subsequent call of Resolve<IFoo>() would return the same instance.
        binder.Bind<IFoo>().FromMethod(() => new Foo()).AsCached().Conclude();

        //Return the same instance of Foo every time when Resolve<IFoo>() is called.
        //Only the first time Resolve<IFoo>() is called, the instance would be injected.
        IFoo foo = new Foo();
        binder.Bind<IFoo>().ToInstance(foo).AsCached().Conclude();
    }

    public void Singleton()
    {
        IBinder binder = new Container();

        //Create some bindings to IFoo.
        binder.Bind<IFoo>().To<Foo>().Conclude();
        binder.Bind<IFoo>().To<Foo>().ToId("foo").Conclude();

        //Create a new instance the first time when Resolve<IFoo>() is called.
        //This will wipe all previous bindings (2 bindings) of the type IFoo.
        //Subsequent call to Resolve<IFoo> would always return the same instance.
        binder.Bind<IFoo>().To<Foo>().AsSingleton().Conclude();

        //Binding any binding with the same contract type would result in BindingException.
        binder.Bind<IFoo>().To<Foo>().ToId("bar").Conclude();
        binder.Bind<IFoo>().To<Foo>().Conclude();
    }

    /// <summary>
    /// A dummy interface for the example.
    /// </summary>
    interface IFoo { }

    /// <summary>
    /// A dummy abstract class for the example.
    /// </summary>
    abstract class AbstractFoo : IFoo { }

    /// <summary>
    /// A dummy base class for the example.
    /// </summary>
    class Foo : AbstractFoo { }

    /// <summary>
    /// A dummy derived class for the example.
    /// </summary>
    class Bar : Foo { }
}