Created
April 11, 2014 20:41
-
-
Save khalidabuhakmeh/10499930 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MvcApplication : System.Web.HttpApplication | |
{ | |
public static StructureMapDependencyResolver SmDepdencyResolver { get; set; } | |
protected void Application_Start() | |
{ | |
IContainer container = IoC.Initialize(); | |
SmDepdencyResolver = new StructureMapDependencyResolver(container); | |
DependencyResolver.SetResolver(SmDepdencyResolver); | |
AreaRegistration.RegisterAllAreas(); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
} | |
protected void Application_BeginRequest(object sender, EventArgs e) | |
{ | |
SmDepdencyResolver.CreateNestedContainer(); | |
} | |
protected void Application_EndRequest(object sender, EventArgs e) | |
{ | |
SmDepdencyResolver.DisposeNestedContainer(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StructureMapDependencyResolver : ServiceLocatorImplBase | |
{ | |
private const string StructuremapNestedContainerKey = "Structuremap.Nested.Container"; | |
public IContainer Container { get; set; } | |
private HttpContextBase HttpContext | |
{ | |
get | |
{ | |
// All the user to register where the context comes from | |
var ctx = Container.GetInstance<HttpContextBase>(); | |
return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current); | |
} | |
} | |
public IContainer CurrentNestedContainer | |
{ | |
get { return (IContainer)HttpContext.Items[StructuremapNestedContainerKey]; } | |
set { HttpContext.Items[StructuremapNestedContainerKey] = value; } | |
} | |
public StructureMapDependencyResolver(IContainer container) | |
{ | |
Container = container; | |
} | |
protected override IEnumerable<object> DoGetAllInstances(Type serviceType) | |
{ | |
return (CurrentNestedContainer ?? Container).GetAllInstances(serviceType).Cast<object>(); | |
} | |
protected override object DoGetInstance(Type serviceType, string key) | |
{ | |
var container = (CurrentNestedContainer ?? Container); | |
if (string.IsNullOrEmpty(key)) | |
{ | |
return serviceType.IsAbstract || serviceType.IsInterface | |
? container.TryGetInstance(serviceType) | |
: container.GetInstance(serviceType); | |
} | |
return container.GetInstance(serviceType, key); | |
} | |
public void Dispose() | |
{ | |
if (CurrentNestedContainer != null) | |
{ | |
CurrentNestedContainer.Dispose(); | |
} | |
Container.Dispose(); | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return (CurrentNestedContainer ?? Container).GetAllInstances(serviceType).Cast<object>(); | |
} | |
public void DisposeNestedContainer() | |
{ | |
if (CurrentNestedContainer != null) | |
CurrentNestedContainer.Dispose(); | |
} | |
public void CreateNestedContainer() | |
{ | |
if (CurrentNestedContainer != null) return; | |
CurrentNestedContainer = Container.GetNestedContainer(); | |
} | |
} |
hyrmn
commented
Apr 13, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment