Castle Windsor and Child containers reloaded

This is a follow up to my previous post. I deliberately didn’t discuss the issues that arise when using container hierarchies to get some feedback on usage first.

So what’s the problem?

Consider trivial scenario:

two

We have two components, where foo depends on bar. The dependency is optional, which means that foo can be constructed even when bar is not available.

Then we have the following container hierarchy:

one

We have parent container where foo is registered and two child containers. In one of them we register bar. Both foo and bar are singletons (for sake of simplicity – the example would work for any non-transient lifestyle).

Now we do this

var fooA = childA.Resolve<Foo>();
var fooB = childB.Resolve<Foo>();

What should happen?


Posted 06-03-2010 1:37 AM by Krzysztof Koźmic
Filed under: ,

[Advertisement]

Comments

Moutasem Al-awa wrote re: Castle Windsor and Child containers reloaded
on 06-03-2010 5:17 AM

Logically, fooA contains a bar, on the other hand fooB does not contain one because the bar is not registered. Does the child containers behave like this ?

John wrote re: Castle Windsor and Child containers reloaded
on 06-03-2010 7:15 AM

What he said.

zihotki wrote re: Castle Windsor and Child containers reloaded
on 06-03-2010 7:30 AM

I second Moutasem's opinion.

AndyZ wrote re: Castle Windsor and Child containers reloaded
on 06-03-2010 4:44 PM

Both should return a Foo without a Bar.  If a Foo with a Bar is returned and childA is disposed (and with it the singleton Bar), then Foo will be left referring to a disposed object.  Another problem with returning Foo with a Bar is that if childB is resolved first then the behavior will be different and having different behaviors based on the timing of calls to Resolve is not acceptable

One possible solution would be to add a lifetime of "singleton-per-container" in which case childA would hold its own Foo with a Bar and childB  would hold a different Foo without a Bar.

I've played with child containers and Unity and run into a number of subtle issues mostly having to do with singletons.

I agree with your assessment in the original post: "removing the child containers, and add some nicer support for contextual scoping of components would be the best solution"

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)