I studied Castle Windsor installer functionality based on the answers of the question . I want to use Web.config to specify the database name, and I would prefer not to explicitly specify the database name in my code. I tried the Krzysztof KoΕΊmic example, and when I debut in the project, my break point in container.Register hits, 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 Web.config to resolve dependencies (note that he does this to get the connection string, but my connection string is encrypted, so I do it a little differently):
<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 are part of the class library referenced in the current project . SqlUsersRepository looks at the connection string from the Web.Config 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>(); }
Any help on this?
PS
I still get the exception, even if I use a hard-coded database name (as shown in RepositoriesInstaller ):
Server error in application "/". cannot create component 'MyProjectName.Controllers.UserController' because it has dependencies on satisfied. MyProjectName.Controllers.UserController waits for the following Dependencies:
Services: - MyProjectName.Entities.IUsersRepository which has not been registered. Description: An unhandled exception occurred during the execution of the current network request. View the stack trace for more information about the error and where it originated in the code.
Exception Details: Castle.MicroKernel.Handlers.HandlerException: Unable to create component 'MyProjectName.Controllers.UserController' because it has dependencies on satisfied. MyProjectName.Controllers.UserController waits for the following Dependencies:
Services: - MyProjectName.Entities.IUsersRepository which has not been registered.
Update I posted an answer regarding the exception problem, but I still didn't understand how to use Web.config to store individual sections of Castle Windsor.