Alfredo Fernández
Alfredo Fernández

Reputation: 200

ControllerFactory having controllers separated in a different project

I have my controllers separated in a different project, i'm using castle windsor and everything was fine, i had a tipical Controller factory in the same project that do the follow:

        public WindsorControllerFactory()
    {
        container = new WindsorContainer(
                        new XmlInterpreter(new ConfigResource("castle")
                        )
                    );

        var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                          where typeof(IController).IsAssignableFrom(t)
                          select t;

        foreach (Type t in controllerTypes)
        {
            container.AddComponentLifeStyle(t.FullName, t, Castle.Core.LifestyleType.Transient);
        }
    }

Then I wanted to recapsulate the factory to an own "Framework" to reuse it in future projects. But then the executing assembly doesn't have the controllers, Any ideas about how to make it decoupled?

I was thinking in something like a configuration file that indicates the assembly with the controllers...

Upvotes: 0

Views: 217

Answers (1)

maciejkow
maciejkow

Reputation: 6453

First of all, I would pass an Assembly as constructor parameter, then I would create a factory method:

public static IControllerFactory FromAssemblyContaining<T>()
{
    return new WindsorControllerFactory(typeof(T).Assembly);
}

And then create your controller factory using:

WindsorControllerFactory.FromAssemblyContaining<some class, Global perhaps>();

Upvotes: 1

Related Questions