views:

27

answers:

1

Hi there Im using windsor as a DI container,

my code is below

public static class ContainerBuilder
    {
        public static IWindsorContainer Build()
        {
            var container = new WindsorContainer("Configuration\\Windsor.config");



            // automatically register controllers
            container.Register(AllTypes
                                   .Of<Controller>()
                                   .FromAssembly(Assembly.GetExecutingAssembly())
                                   .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));

            container.Register(


               Component.For<IServiceLocator>().Instance(new WindsorServiceLocator(container)),
              Component.For(typeof(IRepository<>)).ImplementedBy(typeof(NHibernateRepository<>)).LifeStyle.Transient

               );


            return container;
        }
    }

I need to call this from a test project , the problem is that when I do this the windsor.config is never found and the test seems to always fail, where is the best way to place this config file or is there a better approach to doing this? Thanks

A: 

Just make the config path configurable, e.g.

public static IWindsorContainer Build(string configPath) {
  var container = new WindsorContainer(configPath);
  ...
}

In your app the configPath is "Configuration\Windsor.config" while in your tests you'll have a path like "....\Configuration\Windsor.config".

Note that you shouldn't generally depend on the container in your tests, unless you're running some integration tests.

Also a static container builder doesn't seem like a good idea, take a look at Windsor Installers to register your components in a modular way.

Mauricio Scheffer
Hi Mauricio Thanks for the answer, yes I am running integration tests. All the examples I have seen regarding windsor uses a static container, what is the benefit of the windsor installers
Matthew
@Matthew: modularity. I recommend checking out http://www.castlecasts.com
Mauricio Scheffer