Injecting properties in the base controller using Ninject 2

I have the following code in my Global.aspx

protected override void OnApplicationStarted()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}

protected override IKernel CreateKernel()
{
    return new StandardKernel(new ServiceModule());
}

I also have the following Ninject module:

internal class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IProductService>().To<ProductService>().InRequestScope();
    }
}

I also have a base controller:

public class BaseController : Controller
{
    [Inject]
    public IProductService ProductService
    {
        get;
        set;
    }
}

This code works. The problem I am facing is that I would like to remove the add attribute from the base controller and instead specify this in the Ninject ServiceModule. I would like to say how can I write a binding rule in a ServiceModule that tells Ninject to inject a ProductService into a property in the base controller?

If I remove the attribute, I will get a NullReferenceException.

+5
source share
2 answers

, , http://github.com/ninject/ninject.extensions.conventions - IBindingGenerator. .

, - . , ASP.NET MVC, ( FubuMVC ..). , - .

, OnActivation Bind - , , , , .

, , . :

  • , . , ? , ? , , . .

  • . Ninject , (.. , [Inject], , , , ).

, OOTB. , IInjectionHeuristic .

, ,

  • , , .
  • ninject .
  • , .
+3

, IInjectionHeuristic.

public class ControllerInjectionHeuristic : NinjectComponent, IInjectionHeuristic
{
    private readonly IKernel kernel;

    public BaseControllerInjectionHeuristic(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public bool ShouldInject(MemberInfo member)
    {
        if (member.ReflectedType != typeof(BaseController))
        {
            return false;
        }

        var propertyInfo = member.ReflectedType.GetProperty(member.Name);
        object service = kernel.TryGet(propertyInfo.PropertyType);

        return service != null;
    }
}

ControllerInjectionHeuristic () BaseController, ​​ .

.

var kernel = new StandardKernel();
kernel.Components.Add<IInjectionHeuristic, ControllerInjectionHeuristic>();

OnActivation. ( , , ).

public class ControllerModule : NinjectModule
{
    public override void Load()
    {
        // Get all controller types. You could use
        // Ninject.Extensions.Conventions.
        IEnumerable<Type> controllerTypes = null;
        foreach (var controllerType in controllerTypes)
        {
            Bind(controllerType).ToSelf().InRequestScope()
                .OnActivation(ControllerActivation);
        }
    }

    private static void ControllerActivation(IContext context, object obj)
    {
        var controller = obj as BaseController;
        if (controller == null)
        {
            return;
        }

        controller.ProductService = context.Kernel.Get<IProductService>();
    }
}
+2

All Articles