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

Using StructureMap to build a class that isn't registered with the container

I had a query this morning about this.  Say you're working in the new MVC framework and you write your own routing engine that has a syntax like this:

  Route("some string pattern").IsHandledBy<MyController>();
Route("some other string pattern").IsHandledBy<AnotherController>();

Each Controller has its own set of dependencies that you'd prefer that StructureMap build out for you, but it might be inconvenient to have to register all these types with StructureMap.  That's okay.  As long as you know the concrete type you want, StructureMap can create the concrete class and "fill" it with all of the dependencies even if StructureMap isn't configured with that concrete type in advance.  The syntax is:

MyController controller = ObjectFactory.FillDependencies<MyController>();

or

MyController controller = (MyController)ObjectFactory.FillDependencies(typeof(MyController));

I don't use this feature much, but it's handy in some spots



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

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