views:

221

answers:

2

CodeCampServer source code contains a generic StaticFactory.

I'm surmising that this is a key piece of the mechanism for how the framework plays well with Dependency Injection.

Subclasses of which use it's DefaultUnconfiguredState to provide static access to, well, a Default Unconfigured State for themselves which the dependency resolution mechanism can replace with working stuff.

I've not been able to find any documentation for this...

Is there a good explanation in the book? (I'm awaiting delivery from Amazon...)

...or can anyone else provide a good commentary on what this is and whether I'd be wise to adopt this pattern (if it is one...)?

Update

Since Jeffrey Palermo replied to this question I see that in the (work-in-progress) manuscript for MVC2 in Action this pattern/style is discussed and illustrated using a Factory that is used to locate a Repository in order to keep the domain layer ignorant of persistence concerns. (see chapter 23).

By default the use of this factory throws an exception:

"the knowledge of how to create the repository doesn’t reside with the factory. This factory merely represents the capability to return the repository"

The example could have used one of several mechanisms for initializing a concrete implementation of the repository interface. In the example in the book they elect not to use an IOC container for sake of simplicity and provide it explicitly in some start-up logic.

"The important thing is that neither the Core project nor the UI project should reference the Infrastructure project or libraries that are purely infrastructural in nature. We have kept NHibernate completely off to the side so that the rest of the application doesn’t care how the data access is happening"

A final point to note about the example code in this new chapter is that the Factory is no longer static (at least not as far as the externally facing interface is concerned).

Update 2

Mr Palermo blogged some more about this particular style of Abstract Factory (see the implementaion of OrderShipperFactory).

I could also just consider 'Manual Dependency Injection' (Uncle Bob).

+4  A: 

Anything static is an enemy of DI.

This StaticFactory looks like an implementation of the Service Locator (anti-)pattern.

I consider Service Locator to be an anti-pattern, since it is totally opaque to the user of the API which dependencies need to be in place; thus, one could easily invoke methods on objects in a context where the Service Locator would throw, and the API gives you absolutely no clue that this is the case.

Proper DI such as copious use of Constructor Injection is a much better alternative.

Mark Seemann
amen to that...
Krzysztof Koźmic
I agree that SL is most of the time an anti pattern
Krzysztof Koźmic
I did some analysis in my blog on injection or location of dependencies and would be glad to here your feedback (http://dzmitryhuba.blogspot.com/2009/08/inject-or-locate-dependencies.html).
Dzmitry Huba
tweeted @jeffreypalermo so hopefully he'll chime in...
rohancragg
I can't believe that actually worked.. hooray for twitter :-)
rohancragg
Why the downvote?
Mark Seemann
update: constructor injection can be over-used (http://jeffreypalermo.com/blog/constructor-over-injection-anti-pattern/) so there is still a place for an abstract factory...
rohancragg
@rohancragg: I disagree strongly with Jeffrey Palermo's post. Read my response here: http://blog.ploeh.dk/2010/01/20/RebuttalConstructorOverinjectionAntipattern.aspx
Mark Seemann
+4  A: 

Good question. I don't like it either. It should really be named "StartupFactoryConfiguration", but it is on the refactor list.

We played around with that idea as a way to set up DI for places that were not under constructor injection via the container.

It will go away. I don't know what the anti-pattern is (what name?), but StaticFactory will die.


Now it has been renamed as of this morning. It is now AbstractFactoryBase. It is an implementation of the Abstract Factory pattern: http://en.wikipedia.org/wiki/Abstract%5Ffactory%5Fpattern

The implementation of the factory is to end up calling an IoC contrainer, but it allows access from a place in code without an assembly reference to the IoC container assembly.

Regards, Jeffrey Palermo

Jeffrey Palermo
Thanks for the reply. The reason I asked is that I'm considering a gradual migration of legacy code towards (eventual) use of DI. For which this may prove to be a useful stepping-stone refactoring? Do you discuss it anywhere online or in your book?
rohancragg
Check out the abstract factory pattern:http://en.wikipedia.org/wiki/Abstract_factory_patternIt's not discussed in my ASP.NET MVC in Action book because that book is on MVC. It is demonstrated, however, in CodeCampServer in several places.
Jeffrey Palermo
@rohancragg: If you need guidance on moving a legacy application towards DI I can recommend Michael Feathers' book "Working Effectively with Legacy Code".
Mark Seemann
I do have that excellent book already and I'll make an effort to refer to it more. While there's some good advice on breaking dependencies Mr Feathers doesn't explicitly call out DI or IoC. The book was written back in 2005 so perhaps it pre-dates the current vogue for DI.
rohancragg
About the name, I call these static, targeted variants of ServiceLocator `Static Volcano` (or `Static Explosion` but I like the volcano analogy better)
Krzysztof Koźmic