Windsor 3, BoundTo scoping and lazy resolving

414 views
Skip to first unread message

Asger Hallas

unread,
Dec 22, 2011, 3:29:07 PM12/22/11
to Castle Project Users
Hi

I might have misunderstood the purpose or overlooked some limitations
of the new BoundTo lifestyle, but isn't this supposed to work?

public class windsor_boundto_scoping
{
[Fact]
public void can_scope_late_bound()
{
var c = new WindsorContainer()
.AddFacility<TypedFactoryFacility>()
.Register(Component.For<Parent>().LifeStyle.Transient,

Component.For<Child>().LifeStyle.BoundTo<Parent>());

c.Resolve<Parent>().AndThen();
}

public class Parent
{
readonly Func<Child> late;

public Parent(Func<Child> late)
{
this.late = late;
}

public void AndThen()
{
late();
}
}

public class Child
{
}
}

The docs says: "Bound lifestyle - allows objects to be reused within
dependency subtree"

Is a typed factory not part of the dependency subtree of its owner?

//Asger

Krzysztof Koźmic

unread,
Dec 22, 2011, 3:56:04 PM12/22/11
to castle-pro...@googlegroups.com
Hi Asger,

It doesn't work for typed factories. Since a factory can be passed
around and used in different contexts there's ambiguity as to the scope
of components you'd be resolving via the factory.

K

Asger Hallas

unread,
Dec 23, 2011, 3:15:56 AM12/23/11
to Castle Project Users
Hi Krzysztof

I see. I would have thought that the original owner of the factory
would always be the scope of the resolved components, no matter in
what context the factory was used. It seems like a nice default to me,
but I guess I overlook some subtleties :)

For what it's worth, I can say that it would be very useful for my
current scenario of a batch processor of messages. I get a bunch of
messages, use the factory to resolve message handlers for each one,
and I want to have injected the same database session into each in the
scope of the batch processor.

Any advice on how this can be done otherwise?

Thanks,
Asger

On Dec 22, 9:56 pm, Krzysztof Koźmic <krzysztof.koz...@gmail.com>
wrote:
> Hi Asger,
>
> It doesn't work for typed factories. Since a factory can be passed
> around and used in different contexts there's ambiguity as to the scope
> of components you'd be resolving via the factory.
>
> K
>
> On 23/12/2011 6:29 AM, Asger Hallas wrote:
>
> 112113
>
> 114

Krzysztof Koźmic

unread,
Jan 3, 2012, 8:16:19 PM1/3/12
to castle-pro...@googlegroups.com
Hi,

sorry for the delay.

I think Scoped lifestyle with an explicit scope might work for you
(that's what NServiceBus 3 is using for example)

Asger Hallas

unread,
Jan 4, 2012, 9:08:34 AM1/4/12
to Castle Project Users
Hi,

I am on my way down the Scoped lifestyle route. NServiceBus 3 is a
good one, I'll look into that one.

BTW I've been having some trouble disposing a scope on another thread
than the one it was created on (are there any downsides to that?). It
seems to be an issue only with xUnit, but I thought you might like to
know: http://xunit.codeplex.com/workitem/9760

Thanks for returning, and happy new year :)

On Jan 4, 2:16 am, Krzysztof Koźmic <krzysztof.koz...@gmail.com>
wrote:
> Hi,
>
> sorry for the delay.
>
> I think Scoped lifestyle with an explicit scope might work for you
> (that's what NServiceBus 3 is using for example)
>
> On 23/12/2011 6:15 PM, Asger Hallas wrote:
>
> 162163
>
> 164
>
>
>
> > Hi Krzysztof
>
> > I see. I would have thought that the original owner of the factory
> > would always be the scope of the resolved components, no matter in
> > what context the factory was used. It seems like a nice default to me,
> > but I guess I overlook some subtleties :)
>
> > For what it's worth, I can say that it would be very useful for my
> > current scenario of a batch processor of messages. I get a bunch of
> > messages, use the factory to resolve message handlers for each one,
> > and I want to have injected the same database session into each in the
> > scope of the batch processor.
>
> > Any advice on how this can be done otherwise?
>
> > Thanks,
> > Asger
>
> > On Dec 22, 9:56 pm, Krzysztof Koźmic<krzysztof.koz165...@gmail.com>
Reply all
Reply to author
Forward
0 new messages