Before I begin I will say this: I have to extend DataContext
in my repository because I'm calling stored procedures and ExecuteMethodCall
is only available internally. Many people don't seem to know this, so please don't say "just don't extend DataContext".
I've just started using Windsor as my IoC container. My controller happily does the following:
public ContractsControlController(IContractsControlRepository contractsControlService)
{
_contractsControlRepository = contractsControlService;
}
But my repository must have this constructor:
public ContractsControlRepository()
: base(ConfigurationManager.ConnectionStrings["AccountsConnectionString"].ToString()) { }
But the IoC container is there to let you specify connection strings for your repository in the web.config. What must my constructor in the repository look like in order to do this? If I don't specify the one I've shown then it complains that there are no constructors that take zero arguments.
Cheers
EDIT
In global.asax.cs
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
WindsorControllerFactory.cs (in the root)
public class WindsorControllerFactory : DefaultControllerFactory
{
WindsorContainer container;
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, LifestyleType.Transient);
}
}
protected IController GetControllerInstance(Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
}
But the container
isn't needed if nothing is going in web.config?