ASP.NET MVC Using Castle Windsor IoC

I have an application modeled on one of the Apress Pro ASP.NET MVC that uses windsor IoC for virtualization to instantiate the controllers with their respective repositories and this works fine

eg.

public class ItemController : Controller
{
    private IItemsRepository itemsRepository;
    public ItemController(IItemsRepository windsorItemsRepository)
    {
        this.itemsRepository = windsorItemsRepository;
    }

      

from

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
using System.Reflection;
using Castle.Core;

namespace WebUI
{
    public class WindsorControllerFactory : DefaultControllerFactory
    {
        WindsorContainer container;

        // The constructor:
        // 1. Sets up a new IoC container
        // 2. Registers all components specified in web.config
        // 3. Registers all controller types as components
        public WindsorControllerFactory()
        {
            // Instantiate a container, taking configuration from web.config
            container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));

            // Also register all the controller types as transient
            var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                  where typeof(IController).IsAssignableFrom(t)
                                  select t;
            foreach (Type t in controllerTypes)
                container.AddComponentWithLifestyle(t.FullName, t, LifestyleType.Transient);
        }

        // Constructs the controller instance needed to service each request
        protected override IController GetControllerInstance(Type controllerType)
        {
            return (IController)container.Resolve(controllerType);
        }
    }
}

      

control of controller creation.

I sometimes need to create other repository instances in controllers to fetch data from other places, can I do this using CW IoC, if so how?

I am playing around with creating new controller classes, as they should automatically register with my existing code (if I can get this to work, I can register them correctly later), but when I try to create them there is an obvious objection as I cannot provide the repos class for the constructor (I was pretty sure this was the wrong way to get around this anyway).

Any help (especially examples) would be much appreciated. cheers mh

+2


source to share


2 answers


it no longer works with the latest version of the vibrator lock, in fact, the microkernel assembly melted inside castle.core



+1


source


Just declare the dependencies you need in the controller constructor, that is:

public class MyController: Controller {
  private readonly IItemsRepository itemsRepo;
  private readonly IPersonRepository personRepo;
  public MyController(IItemsRepository i, IPersonRepository p) {
    itemsRepo = i;
    personRepo = p;
  }
}

      

Windsor will automatically resolve dependencies when instantiating the controller.



There are many projects in google code that you can use for guidance like WineCellarManager .

By the way: you don't need to code your own WindsorControllerFactory, you can get this (and more) from MVCContrib

+5


source







All Articles