0

I have many of the following types:

public class XDatabase : INHibernateDatabase
{
   public XDatabase(DatabaseConfiguration) { ... }
}
// etc... 

and I'm setting up StructureMap like so:

var container = new Container(x =>
    {
        x.Scan(scanner =>
                {
                    scanner.Assembly("Model.Persistence");
                    scanner.AddAllTypesOf<INHibernateDatabase>();
                });

        // The following will be generated from the app.config ConnectionStrings
        x.For<DatabaseConfiguration>()
               .Add(new DatabaseConfiguration("something"))
               .Named("XDatabase");
        // etc....
    });

container.GetAllInstances<INHibernateDatabase>();

Obviously this will not work because I have multiple instances (and no default) for DatabaseConfiguration and StructureMap doesn't know which one to choose for which INHibernateDatabase instance.

How can I tell StructureMap to use a convention so that it always picks the DatabaseConnnection instance based on a naming convention?

1 Answer 1

1

I ended up doing this using a custom IRegistrationConvention:

public class DatabaseRegistrationConvention : IRegistrationConvention
{
    public void Process(Type type, Registry registry)
    {
        if (type.CanBeCastTo<INHibernateDatabase>() && type.HasConstructors())
        {
            // In reality I get the database name via metadata
            var dbName = "databaseNameIDeterminedSomehow";

            // Outside this convention I will have procedurally add a bunch of named DatabaseConfigurations
            var configuredInstance = registry
                .For(typeof (ISchemaDeployer))
                .Use(type)
                .Ctor<DatabaseConfiguration>().IsNamedInstance(dbName);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.