Re: Scoped LifeStyles in winforms

59 views
Skip to first unread message

nst

unread,
Dec 5, 2012, 12:08:21 PM12/5/12
to castle-pro...@googlegroups.com
I have been playing around with BoundTo and thinking that may be a better option....

Here is an example

IWindsorContainer container = new WindsorContainer();
            container.Register(Component.For<Container>().ImplementedBy<Container>().LifestyleTransient().OnDestroy(x => x.Desroyed = true),
                               Component.For<A>().ImplementedBy<A>().LifeStyle.Transient.OnDestroy(x => x.Desroyed = true),
                               Component.For<B>().ImplementedBy<B>().LifeStyle.Transient.OnDestroy(x => x.Desroyed = true),
                               Component.For<C>().ImplementedBy<C>().LifestyleTransient().OnDestroy(x => x.Desroyed = true),
                               Component.For<AB>().ImplementedBy<AB>().LifeStyle.BoundTo<Container>().OnDestroy(x => x.Desroyed = true));
 
 
            ObjectResolver.Initialise(container);
 
            var c = ObjectResolver.Resolve<Container>();
 
            Assert.AreSame(c.A.AB, c.B.AB);
 
            var s = ObjectResolver.Resolve<Container>();
 
            Assert.AreSame(s.A.AB, s.B.AB);
            Assert.AreNotSame(s.A.AB, c.A.AB);
 
            var a = ObjectResolver.Resolve<A>();
            Assert.AreNotSame(a.AB, s.A.AB);    ---- This fails.... any way around this ? 

Where A and B are repositories and AB is the unitofwork and container is the Form... based on the test it looks like that works. The only problem is if I want to resolve a unit of work outside of a form's context. I suspect there isnt a way for a lifestyle to revert to a transient lifestyle if the boundto fails ?

Would appreciate any thoughts on this... is BoundTo a better approach ?

On Wednesday, 5 December 2012 13:18:32 UTC+2, nst wrote:
I want to use the scoped life styles in winforms in conjunction to the Repository & Unit Of Work implementations with Nhibernate.
i.e get windsor to new up the form and inject the defined repositories and have them all share the same unit of work.

My thinking was that I can call the begin scope prior to new'ing the form and store the IDisposable and release when the form is closed. Then when each form is opened a new Begin Scope is called.

Does this sound viable ? There is a problem though - if form 1 is opened and then form 2 is opened the latest begin scope would be owned by form 2. If the resolve method was called from form 1 (say button click) then the UoW would be associated with Form 2's scope ... this correct ?

Are there any suggestions how I can achieve what I am trying to do ?

nst

unread,
Dec 12, 2012, 7:51:21 AM12/12/12
to castle-pro...@googlegroups.com
No suggestions ?

nst

unread,
Dec 12, 2012, 11:42:10 AM12/12/12
to castle-pro...@googlegroups.com
Further playing around...

I was thinking maybe the option is similar to ninject per call scope

        private IHandler ScopeRootBinder(IHandler[] handlers)
        {
            return handlers.FirstOrDefault();
        }

Will the first one always be the parent component being resolved ?
Reply all
Reply to author
Forward
0 new messages