4
votes

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.

3
Please don't use Sanders' book for anything Windsor-related, it's very outdated by now.Mauricio Scheffer
@Mauricio, using the Web.config to store connection strings and database names needed for the repository class has to be useful! It allows you to swap your database by simply adding/changing the connection string in your Web.config...Kiril
of course, but not like that. That's an outdated mechanism.Mauricio Scheffer
@Mauricio, I'm all ears/eyes! I've been looking for a way to get the custom parameter from the windsor section all day long today... do you have any references or anything of the sort that I could look at? I'm about ready to start gnawing at my keyboard because I can't figure it out! I'm not sure what gnawing at my keyboard would do (perhaps produce some random sequence of characters that turns up to be the answer to the universe), but it makes for a convincing effect!Kiril

3 Answers

1
votes

You should be able to achieve this by reducing the XML to this:

<castle>
    <components>
      <component id="SqlUsersRepository">
        <parameters>
          <databaseName>MyDatabaseName</databaseName>
        </parameters>
      </component>
    </components>
</castle>

and then make sure your registration registers the repository under the right key/id - e.g. like this:

container.Register(AllTypes(...).Configure(c => c.Named(c.Implementation.Name)))

where all keys will be set to the short name of each concrete type.

Please note that this naming scheme might not be appropriate for you, so you should probably adapt the c.Named(...) to whatever you deem right for your use case. Just make sure that the registration key for your repository matches that if the id attribute in the XML.

1
votes

DependsOn the way you've done it = the parameter named "databaseName" has a component dependency with a key "MyDatabaseName".

You want to do a .Parameters(ForKey.Named("databasename").Value(ConfigurationManager.ConnectionStrings["MyDatabasename"].ConnectionString)).LifeStyle.Transient. ...

0
votes

I figured out the dependency exception... the issue was that my repositories are in a different assembly, so I had to rig the installation a bit:

public class RepositoriesInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        foreach (AssemblyName name in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
        {
            Assembly asm = Assembly.Load(name);
            container.Register(AllTypes.FromAssemblyNamed(asm.FullName)
                            .Where(Component.IsInSameNamespaceAs<SqlUsersRepository>())
                            .WithService.DefaultInterface()
                            .Configure(c => c.LifeStyle.Transient
                            .DependsOn(new { databaseName = "MyDatabaseName" })));
        }

        container.Register(AllTypes.FromThisAssembly()
                            .Where(Component.IsInSameNamespaceAs<SqlUsersRepository>())
                            .WithService.DefaultInterface()
                            .Configure(c => c.LifeStyle.Transient
                            .DependsOn(new { databaseName = "MyDatabaseName" })));
    }
}

Still trying to figure out how to get the database name from the castle section of the Web.config though.