I was exploring the installer functionality of Castle Windsor based on the answers from a related question. I want to use the Web.config to specify the name of the database and I would rather not explicitly set the database name in my code. I tried Krzysztof Koźmic's example and when I debut the project my break-point at container.Register
gets hit, but the dependency is still not resolved:
public class RepositoriesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly()
.Where(Component.IsInSameNamespaceAs<SqlUsersRepository>())
.WithService.DefaultInterface()
.Configure(c => c.LifeStyle.Transient
.DependsOn(new { databaseName = "MyDatabaseName" })));
}
}
Sanders suggests that we can use the Web.config to resolve dependencies (note that he's doing this to get the connection string, but my connection string is encrypted so I'm doing in in a slightly different way):
<castle>
<components>
<component id="SqlUsersRepository"
service="MyDevArmyModel.Entities.IUsersRepository, MyDevArmyModel"
type="MyDevArmyModel.Entities.SqlUsersRepository, MyDevArmyModel">
<parameters>
<databaseName>MyDatabaseName</databaseName>
</parameters>
</component>
</components>
</castle>
My SqlUsersRepository
and IUsersRepository
are in the same namespace, but they're part of a class library which gets referenced in the current project. SqlUsersRepository
looks up the connection string from the Web.Config the database name:
public interface IUsersRepository
{
IQueryable<User> Users { get; }
// ...
// and some other things
// ...
}
public class SqlUsersRepository : IUsersRepository
{
private DataContext dataContext;
private Table<User> usersTable;
public IQueryable<User> Users { get { return usersTable; } }
public SqlUsersRepository(string databaseName)
{
HttpRequestWrapper request = new HttpRequestWrapper(System.Web.HttpContext.Current.Request);
Configuration config = WebConfigurationManager.OpenWebConfiguration(request.ApplicationPath);
dataContext = new DataContext(config.GetConnetionString(databaseName));
usersTable = dataContext.GetTable<User>();
}
// .... the rest of the repository goes here
}
Any help on this?
P.S.
I'm still getting an exception, even if I use the hard-coded database name (as seen in the RepositoriesInstaller
):
Server Error in '/' Application. Can't create component 'MyProjectName.Controllers.UserController' as it has dependencies to be satisfied. MyProjectName.Controllers.UserController is waiting for the following dependencies:
Services: - MyProjectName.Entities.IUsersRepository which was not registered. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Castle.MicroKernel.Handlers.HandlerException: Can't create component 'MyProjectName.Controllers.UserController' as it has dependencies to be satisfied. MyProjectName.Controllers.UserController is waiting for the following dependencies:
Services: - MyProjectName.Entities.IUsersRepository which was not registered.
Update I've posted an answer which addresses the exception problem, but I'm yet to figure out how to use the Web.config to store Castle Windsor specific sections.