I have been playing around with BoundTo and thinking that may be a better option....
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 ?