Base solution for your next web application
Open Closed

NHibernate Multiple Session #146


User avatar
0
nobruds created

Hello,

First I want to say that your framework is very nice. Second, I am new to this whole structure, sorry for any wrong statement.

My question is, does the framework support multiple sessions ? If yes, how can I do that?

I mean, my WebSite have two or more connection string, how can I implement that using the NHibernate and DI ?

I did something like the sample "PhoneBook", creating two "xDependencyInstaller", where I created my "CreateNhSessionFactory" there, but when I use two connection string I got an error by castle ioc, saying that I can´t register same assembly (SessionFactory) more than once.

Now I am trying to use like the "SimpleTaskSystem" sample, but I don´t know how to achieve my goals .

PS: I am trying to change the framework to work with nhibernate map by code, so I dont need to use Fluent.

Thanks. Bruno


7 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    ABP does not natively supports more than one database for NHibernate. But I may help you based on your needs. Why do you need it? Do you need to second DB for a limited scope or usage of these 2 databases are balanced?

  • User Avatar
    0
    nobruds created

    Hello, thanks for the response.

    Actually its not 2 DB for the same NHibernate project. let me give you a sample.

    I have a financial market system with 3 separated projects: 1 responsible for user authentication and user Data (1 DB) 1 responsible for common searches shared on other systems (1 DB) 1 responsible for the main system operations, like BL, CRUD, etc (1 DB)

    Following your samples, I will have 3 different NHibernates class libraries right ? each of them containing my Dependency installers.

    So its 3 separated projects that I will use on my asp.net MVC website, How should I install the references there using DI, on the global.asax?

    EDIT: Maybe my question is more on the Castle Windsor side. The part that registers the assemblys.

    _windsorContainer.Install(FromAssembly.Containing<PineClientesDependencyInstaller>());
    

    i want to install 2 assembly DependencyInstaller, but got an error saying that ISession from nhibernate is already installed.

    Its something like this: [http://stackoverflow.com/questions/3351268/what-is-a-correct-approach-to-register-several-instances-for-the-same-type-in-wi])

    But if I use that, how do I know which NhRepositoryBase ISession is going to use, DB1 or DB2

    Thanks Bruno

  • User Avatar
    0
    nobruds created

    Hello hikalkan

    You was able to check if this is possible?

    I was looking to you SimpleTaskSystem sample, basically I want to have two or more "SimpleTaskSystemDataModule" so on theirs PreInitialize method I have my DB configs.

    How can I register that using DI ? The framework repositories will know which ISession to use ?

    Thanks

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I thought a bit, it's probably not possible without changing something in the framework (probably starting from NhUnitOfWork).

    It may be accomplished in a limited scope. Your main db remains same, you can do some special thing for additional dbs. If you check NhRepositoryBase class (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.NHibernate/NHibernate/Repositories/NhRepositoryBaseOfTEntityAndTPrimaryKey.cs">https://github.com/aspnetboilerplate/as ... maryKey.cs</a>), it gets a ISessionProvider in it's constructor which is used to perform database operations. So, you can create a class that implements ISessionProvider and create custom repositories (<a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/NHibernate-Integration">http://www.aspnetboilerplate.com/Pages/ ... ntegration</a>) that injects your session provider. Another way could be create and implement your own repositories that works with different session factories. I can not say exact solution without coding and trying.

  • User Avatar
    0
    nobruds created

    Hi, thanks for the feedback.

    Yes, I think I am going to make some changes on the framework, already did some because I don´t use the Fluent NHibernate. I will try those approach and let you know.

    Maybe I will create an SessionFactory dictionary, and inject the specific one on repositories, by name .

    I am really new to this that's why I have all those doubts =).

    Thanks again. Bruno

  • User Avatar
    0
    nobruds created

    Hello again.

    I did some changes on PineNHibernateModule and builded 2 SessionFactory, but I am back to that error :

    "Component Late bound NHibernate.ISessionFactory could not be registered. There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name."

    I changed NhRepositoryInstaller to receive a custom name:

    container.Register(
                    Component.For<ISessionFactory>()
                        .Named(_sessionFactoryName)
                        .UsingFactoryMethod(() => _sessionFactory)
                        .LifeStyle.Singleton
                    );
               container.Register(
                    Component.For(typeof(IRepository<>))
                        .ImplementedBy(typeof(NhRepositoryBase<>))
                        .LifestyleTransient(),
    
                    Component.For(typeof(IRepository<,>))
                        .ImplementedBy(typeof(NhRepositoryBase<,>))
                        .LifestyleTransient()
                    );
    

    So, i have 2 session now, but I am not understanding how can I inject my session to my repository, can you exemplify for me ?

    thanks

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You can not register same interface twice.. If so, how DI can know which should be injected when requested. You can create a different class that use your 2nd session factory, you can write a simple adapter and so on.