CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

If you need something in StructureMap, but you can't build it with new()...

I've had 3 separate questions in the past week about this, so I figured it's blogworthy.  You'll have to download the latest bits from svn at http://sourceforge.net/projects/structuremap.  Anyway, let's say you want the HttpContext in StructureMap so that it can be injected into other classes.  The current HttpContext doesn't even exist at configuration time, and you certainly can't just say new HttpContext(), so we needed a way to tell StructureMap a way to fetch this object and bypass the traditional object building pipeline.  About 4 months ago I added this syntax to the trunk:

StructureMapConfiguration.BuildInstancesOf<HttpContextBase>.TheDefaultIs(
	ConstructedBy<HttpContextBase>(() => HttpContext.Current))
);
Doing this just enables StructureMap to poke the correct HttpContext into something like "public Controller(HttpContext context)." 
This strategy should be useful for remoted objects, Provider classes, the HttpContext, and whatever else you can dream up. 
Comments, and suggestions for improving the syntax (please), would be welcome. 


Comments

FransBouma said:

(offtopic) you forgot to close the pre tag after the codesnippet (so the paragraph below it isn't readable)

# March 20, 2008 2:05 PM

Ben Scheirman said:

I should let you know that that sample doesn't actually work b/c you can't cast HttpContext to HttpContextBase, as they didn't alter HttpContext to inherit from it.

You _can_ do this:

return new HttpContextWrapper2( HttpContext.Current ) .... however given the name and some recent comments from PhilHa on twitter, I'm not sure I should be using this class directly.

Ah well, works for the time being!

# March 24, 2008 10:23 AM

Boris said:

Just got latest (26-MAR-2008)

I think I'm doing right but all I'm getting is:

The name 'ConstructedBy' does not exist in the current context ...

# March 26, 2008 4:08 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP