IT:AD:Ninject

Activation Behavior

There are four built-in behaviors available in Ninject. Attribute is class level:

TransientBehavior [Transient]

A new instance of the type will be created each time one is requested.

SingletonBehavior [Singleton]

Only a single instance of the type will be created, and the same instance will be returned for each subsequent request.

OnePerThreadBehavior [OnePerThread]

One instance of the type will be created per thread.

OnePerRequestBehavior [OnePerRequest]

One instance of the type will be created per web request, and will be destroyed when the request ends.

Instead of relying on attributes, you can also define the behavior when you declare the binding:

Bind<Shogun>().ToSelf().Using<SingletonBehavior>();

Contextual Binding

Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Target.HasAttribute<RangeAttribute>()); // using attrib
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Tag == "range"); // using attrib:     [Inject, Tag("range")]
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Service.Name.StartsWith(“Foo”); // using convention
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Target.Name.BeginsWith(“Remote”); // using convention
// note: IWeapon is the “Service Type”(generally an interface of abstract type) 
// while Sword/Shuriken are “Impl/Target Type”
 

Example

using System;
using CommonServiceLocator.NinjectAdapter;
using Ninject;
using Ninject.Modules;

namespace XAct.Studies.NuGet01
{
    class Program
    {

        static void Main(string[] args)
        {
            IKernel kernel = new StandardKernel();


            CommonServiceLocator.NinjectAdapter.NinjectServiceLocator x = new NinjectServiceLocator(kernel);

            Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(kernel));
            

            //Register services one at a time:
            kernel
                .Bind<ISomeService>()
                .To<SomeService>()
                .InSingletonScope();
            //or in clumps:
            kernel.Load(new ALogicalGroupOfServicesModule());

            //Once Registered get it back out:
            ISomeService someService = kernel.Get<ISomeService>();

            ISomeService someService_B = 
                Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<ISomeService>();
            
            //Use it:
            Console.WriteLine(someService.BePolite("John"));

            Console.WriteLine(someService_B.BePolite("Betty"));
            
        }
    }
    //Define interfaces in another assembly (eg: App.Domain.Services.Contracts)
    interface ISomeService
    {
        string BePolite(string name);
    }

    //Define instances in another assembly (eg: App.Domain.Services)
    public class SomeService : ISomeService
    {
        public string BePolite(string name)
        {
            return string.Format("Hello, {0}.", name);
        }
    }

    //A logical group of services, easier for loading up services
    public class ALogicalGroupOfServicesModule : NinjectModule
    {
        public override void Load()
        {
            //Bind<ISomeOtherService>().To<SomeOtherService>().InRequestScope();
            //Bind<ISomeOtherService2>().To<SomeOtherService2>().InRequestScope();
        }
    }
}