Autowire named instance to correct Repository...

3 views
Skip to first unread message

David Perfors

unread,
Dec 22, 2009, 5:34:44 AM12/22/09
to structuremap-users
Hi,
I started using structuremap for some example projects here at work
and I am very impressed.. So I though lets take it a step further and
use it in a real project. This project is a nice greenfield project,
but needs to use several extremely brownfield databases. To make it
even worse, I need to handle two types of databases at the same time
(access and mssql).
So I am using NHibernate and need to create a bunch of
sessionfactories based on the configuration. That's no problem when
doing it all by hand, but it takes a lot of time...

I was wondering whether it is possible to bootstrap this using
Structuremaps scanning features.
At the moment I have a simple version running. I have a several
Repositories which are correctly mapped using a convention:
public class DefaultRepositoryScanner :
StructureMap.Graph.DefaultConventionScanner
{
public override Type FindPluginType(Type concreteType)
{
if (!concreteType.Name.StartsWith("NHibernate")) return
base.FindPluginType(concreteType);
string interfaceName = "I" + concreteType.Name.Substring
("NHibernate".Length);
return Array.Find<Type>(concreteType.GetInterfaces(),
delegate(Type t)
{
return t.Name == interfaceName;
});
}
}
using this registry:
ForRequestedType<ISessionFactory>().AsSingletons()
.TheDefault.Is.ConstructedBy((context) =>
{
return ConfigurationBuilder
(context.GetInstance<INisDataConfig>()).BuildSessionFactory();
});

ForRequestedType<ISession>().CacheBy(InstanceScope.Hybrid)
.TheDefault.Is.ConstructedBy(ctx =>
ctx.GetInstance<ISessionFactory>().OpenSession());

Scan(x =>
{
x.TheCallingAssembly();
x.With<DefaultRepositoryScanner>();
});
The ConfigurationBuilder method returns a Configuration object based
on the configuration.

Now I only have one sessionfactory for all the databases. This would
be fine if we used either Access or MsSql, but some databases are
Access only.

I was thinking of creating multiple name instances of ISessionFactory
(name == database) based on some knowledge (probably a configuration
or hardcoded, that wouldn't be so difficult) and than Autowire them to
the correct repository. (I was thinking of giving the repositories a
marker interface to indicate which database to use.)

Would this be possible? Is this be done before?

Reply all
Reply to author
Forward
0 new messages