3

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).

Update 3 - March 2016

There's another example of it here, though Jeffrey is explicit about this being demo code, and the comment indicates that this would be configured in what Mark Seeman would call a Composition Root (i.e. at Application Start-up)

I discovered this in Jeffrey's article "Onion Architecture: Part 4 - After Four Years"

2 Answers 2

7

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_factory_pattern

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

6
  • 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
    Dec 17, 2009 at 14:45
  • Check out the abstract factory pattern: en.wikipedia.org/wiki/Abstract_factory_pattern It'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. Dec 17, 2009 at 15:31
  • @rohancragg: If you need guidance on moving a legacy application towards DI I can recommend Michael Feathers' book "Working Effectively with Legacy Code". Dec 17, 2009 at 20:24
  • 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
    Dec 18, 2009 at 8:37
  • About the name, I call these static, targeted variants of ServiceLocator Static Volcano (or Static Explosion but I like the volcano analogy better) Apr 14, 2010 at 6:19
5

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.

5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.