Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a bit of help here trying to solve a problem using Castle Windsor and NHibernate.

I have been reading different articles on this subject for a couple of days and I can't find a solution for my particular issue. What I want to do is have two database connections, a local and a remote. The remote connection will only be created on demand, so it could be lazily loaded. I can't have certain repositories using one session factory and others using the other factory, factories need to be available to all repositories so I can synchronize repositories.

I looked here: https://lookonmyworks.co.uk/2011/04/05/managing-multiple-session-factories-using-named-instances/ but as you can see it binds different repositories to different session factories.

I realize that when you are using IoC you have to give it some kind of resolution strategy in order to know which session factory to use under certain circumstances, I am just puzzled right now on how I am going to do that. Am I going to have to end up creating a second set of repositories for the remote repositories and explicitly tell Castle Windsor to use the remote context for those repositories?

C#
public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
    container.Register(
        Component.For<ISessionFactory>().UsingFactoryMethod(CreateNhSessionFactory).LifeStyle.Singleton,
        Component.For<NhUnitOfWorkInterceptor>().LifeStyle.Transient,
        Classes.FromAssembly(Assembly.GetAssembly(typeof(NhClaimRepository))).InSameNamespaceAs<NhClaimRepository>().WithService.DefaultInterfaces().LifestyleTransient(),
        Classes.FromAssembly(Assembly.GetAssembly(typeof(ClaimService))).InSameNamespaceAs<ClaimService>().WithService.DefaultInterfaces().LifestyleTransient()
        );
}

private static ISessionFactory CreateNhSessionFactory()
{
    var config = new Configuration().Configure();
    return Fluently.Configure(config)
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AttorneyMap>())
        .ProxyFactoryFactory<AddPropertyChangedProxyFactoryFactory>()
        .BuildSessionFactory();
}


Sorry for the jumbled stream of consciousness post, brain has been addled during this process. Any help is greatly appreciated.

What I have tried:

Tried this: https://lookonmyworks.co.uk/2011/04/05/managing-multiple-session-factories-using-named-instances/

but it requires separate repositories per session factory.

And this: http://fabiomaulo.blogspot.com/2009/09/configure-sessionfactory-providers.html same situation.

I also went through a number of posts on SO, to no avail. Right now I am at the point where I am just going to throw DI out the window and write a separate DLL using EF or something, but I will have to write all of my repositories again in this new project.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900