NH facility: lazy session factory construction

6 views
Skip to first unread message

Germán Schuager

unread,
Feb 1, 2009, 8:43:54 AM2/1/09
to castle-pro...@googlegroups.com
Hi,
I have the following scenario in a Winforms application:

NH facility is configured from app.config with 2 session factories.
One of these configurations needs to get its connection string from data in the other database.

So, I need a way to initialize just the first session factory, retrieve some information and modify the configuration of the other factory before it is built.

I've been thinking that this could be interesting: to add a "lazy" attribute to the factory configuration and to register a proxy of ISessionFactory in the container instead of the real session factory. An SessionFactoryInitializerInterceptor will take care of doing cfg.BuildSessionFactory() the first time that it is needed. In the meanwhile, I can modify the conection string doing something like this:
   
    var cfg = kernel.Resolve<Configuration>("lazySessionFactory.cfg");
    cfg.SetProperty("connection.connection_string", newConnectionString);

What do you think about this? Is there a simple way to achieve this?
If this could be useful I'm willing to submit a patch.

Tuna Toksoz

unread,
Feb 1, 2009, 8:46:46 AM2/1/09
to castle-pro...@googlegroups.com
This is one of the things i had  in mind-lazy initialization- but for some reason, it went out of my mind.
I guess everybody could benefit from lazy initialization, and if you'd like to send a patch, I'd be glad to review it.

Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!

Tuna Toksoz

unread,
Feb 1, 2009, 11:02:46 AM2/1/09
to castle-pro...@googlegroups.com
I guess all you need is to inherit from ComponentActivator.


Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!



Germán Schuager

unread,
Feb 1, 2009, 11:09:37 AM2/1/09
to castle-pro...@googlegroups.com
I don't think I understand what you mean.

I was thinking in doing something like this:

            ISessionFactory sessionFactory;

            string dontBuild = config.Attributes["lazy"];
            if ("true".Equals(dontBuild))
            {
                ProxyGenerator generator = new ProxyGenerator();
                sessionFactory = (ISessionFactory) generator.CreateInterfaceProxyWithoutTarget(typeof (ISessionFactory),
                                                                                               new SessionFactoryInitializerInterceptor(cfg));
            }
            else
            {
                sessionFactory = cfg.BuildSessionFactory();
            }

in the ConfigureFactories method of NHibernateFacility.

Tuna Toksoz

unread,
Feb 1, 2009, 11:11:34 AM2/1/09
to castle-pro...@googlegroups.com
This would also work, but i guess ComponentActivator is a better way, what do the others think? I am not expert on microkernel.



Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!



Tuna Toksoz

unread,
Feb 1, 2009, 11:15:47 AM2/1/09
to castle-pro...@googlegroups.com
However, what I said requires some changes to Facility itself, the way SessionFactories stored requires them to be initialized(or lazy initialized-as-you-did), component activator thing needs some change.

IF you send me a patch, then I would review & apply then start the work with componentactivator thing.



Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!



Germán Schuager

unread,
Feb 1, 2009, 11:43:04 AM2/1/09
to castle-pro...@googlegroups.com
Here it is.
Let me know what you think.
lazysessionfactory.patch

Tuna Toksoz

unread,
Feb 1, 2009, 11:52:43 AM2/1/09
to castle-pro...@googlegroups.com
Looks good, I guess I will mark lazyness as default.

Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!



Tuna Toksoz

unread,
Feb 1, 2009, 5:55:30 PM2/1/09
to castle-pro...@googlegroups.com
Does anybody know if there is a way to find if a component is initialized before?


Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!



Tuna Toksoz

unread,
Feb 1, 2009, 6:29:50 PM2/1/09
to castle-pro...@googlegroups.com
I did some reflection to test it, it worked :)


Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!



Tuna Toksoz

unread,
Feb 1, 2009, 6:35:31 PM2/1/09
to castle-pro...@googlegroups.com
German,

Thanks for your efforts, I really appreciate it. I went with CustomComponentActivator approach and it is available in the trunk.

            var model = new ComponentModel(id, typeof(ISessionFactory), typeof(Empty));
            model.LifestyleType = LifestyleType.Singleton;
            model.ExtendedProperties[Constants.SessionFactoryConfiguration] = cfg;
            model.CustomComponentActivator = typeof (SessionFactoryActivator);
            Kernel.AddCustomComponent( model );
            sessionFactoryResolver.RegisterAliasComponentIdMapping(alias, id);

I ensured that SesisonFactory is singleton and activated viaa SessionFactoryActivator.
and in the activator, all I did is that

        public override object Create(CreationContext context)
        {
            var configuration = Model.ExtendedProperties[Constants.SessionFactoryConfiguration]
                                as Configuration;
            return configuration.BuildSessionFactory();
        }

I hope this helps.




Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!



Germán Schuager

unread,
Feb 1, 2009, 9:17:25 PM2/1/09
to castle-pro...@googlegroups.com
I like your solution. It is cleaner and it doesn't need a proxy.
I'm already using this, it works ok.
Thank you.

Tuna Toksoz

unread,
Feb 2, 2009, 9:48:03 AM2/2/09
to castle-pro...@googlegroups.com
I have written a blogpost about ComponentActivator's here for future reference. I hope you don't get this as an advertisement.

http://www.tunatoksoz.com/post/Castle-Custom-Component-Activators.aspx



Tuna Toksöz
http://tunatoksoz.com
http://twitter.com/tehlike

Typos included to enhance the readers attention!



Reply all
Reply to author
Forward
0 new messages