Friday, May 9, 2014

using HttpContext context with StructureMap

This commentary by Josh Flanagan suggests that both .HybridHttpOrThreadLocalScoped() and .HttpContextScoped() as suggested here...

ObjectFactory.Initialize(x =>
{
   x.For<IWhatever>().HybridHttpOrThreadLocalScoped().Singleton().Use<Whatever>();
   x.For<ISomethingMore>().HttpContextScoped().Singleton().Use<SomethingMore>();

 
 

...are going to try to use the HttpContext within their doings. The later has to have it or it will break and blow up. In an app I am working on we have some of this stuff going on for an implementation that uses Session and thus has to have HttpContext. I did some hand-ringing over whether or not HttpContext would be recognized if I just grabbed ahold of the IWhatever implementation in core logic far away from a code behind or controller like so:

DataProvider dataProvider = ObjectFactory.GetInstance<DataProvider>();

 
 

...but then it was pointed out to me that .Singleton() would prevent duplicate and possibly retarded StructureMap implementations of IWhatever from being spun up provided IWhatever was created with HttpContext available to begin with! In our app all code behinds in a DotNetNuke application inheirt from a base case that does the StructureMap wireups first. Clearly, HttpContext is available in that scenario.

No comments:

Post a Comment