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.
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.
On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote: > 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.
> Typos included to enhance the readers attention!
> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>> 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.
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.
>> Typos included to enhance the readers attention!
>> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>> 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.
On Sun, Feb 1, 2009 at 1:15 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
> 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.
>>> Typos included to enhance the readers attention!
>>> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>>> 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.
On Sun, Feb 1, 2009 at 6:43 PM, Germán Schuager <gschua...@gmail.com> wrote:
> Here it is.
> Let me know what you think.
> On Sun, Feb 1, 2009 at 1:15 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>> 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.
>>>> Typos included to enhance the readers attention!
>>>> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>>>> 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.
> Typos included to enhance the readers attention!
> On Sun, Feb 1, 2009 at 6:43 PM, Germán Schuager <gschua...@gmail.com>wrote:
>> Here it is.
>> Let me know what you think.
>> On Sun, Feb 1, 2009 at 1:15 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>> 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.
>>>>> Typos included to enhance the readers attention!
>>>>> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>>>>> 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.
On Mon, Feb 2, 2009 at 12:55 AM, Tuna Toksoz <tehl...@gmail.com> wrote:
> Does anybody know if there is a way to find if a component is initialized
> before?
>> Typos included to enhance the readers attention!
>> On Sun, Feb 1, 2009 at 6:43 PM, Germán Schuager <gschua...@gmail.com>wrote:
>>> Here it is.
>>> Let me know what you think.
>>> On Sun, Feb 1, 2009 at 1:15 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>>> 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.
>>>>>> Typos included to enhance the readers attention!
>>>>>> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com>wrote:
>>>>>>> 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.
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();
}
>> Typos included to enhance the readers attention!
>> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>> 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.
>>> Typos included to enhance the readers attention!
>>> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>>> 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.
On Mon, Feb 2, 2009 at 4:17 AM, Germán Schuager <gschua...@gmail.com> wrote:
> I like your solution. It is cleaner and it doesn't need a proxy.
> I'm already using this, it works ok.
> Thank you.
> On Sun, Feb 1, 2009 at 8:35 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>> 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;
>>>> Typos included to enhance the readers attention!
>>>> On Sun, Feb 1, 2009 at 3:46 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
>>>>> 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.