Created
May 20, 2016 08:00
-
-
Save whyleee/30ed384f54610b66ee52f356e6f8e03e to your computer and use it in GitHub Desktop.
StructureMap dependency resolver for Episerver with MVC/Web API
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
[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))] | |
public class IocConfig : IConfigurableModule | |
{ | |
public void ConfigureContainer(ServiceConfigurationContext context) | |
{ | |
context.Container.EjectAllInstancesOf<ICatalogContentBuilder>(); | |
context.Container.Configure(ConfigureContainer); | |
DependencyResolver.SetResolver(new StructureMap.StructureMapDependencyResolver(context.Container)); | |
GlobalConfiguration.Configuration.DependencyResolver = new StructureMap.StructureMapDependencyResolver(context.Container); | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Http.Dependencies; | |
using StructureMap; | |
namespace Website.App_Start.StructureMap | |
{ | |
// NOTE: slightly modified version of 'StructureMapDependencyResolver' from 'StructureMap.MVC4' project | |
// https://github.com/webadvanced/Structuremap.MVC4/blob/master/content/DependencyResolution/StructureMapDependencyResolver.cs.pp | |
public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver, System.Web.Mvc.IDependencyResolver | |
{ | |
public StructureMapDependencyResolver(IContainer container) : base(container) { } | |
public IDependencyScope BeginScope() | |
{ | |
var childContainer = Container.GetNestedContainer(); | |
return new StructureMapDependencyScope(childContainer); | |
} | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Http.Dependencies; | |
using StructureMap; | |
namespace Website.App_Start.StructureMap | |
{ | |
// NOTE: simplified version of 'StructureMapDependencyScope' from 'StructureMap.MVC4' project | |
// https://github.com/webadvanced/Structuremap.MVC4/blob/master/content/DependencyResolution/StructureMapDependencyScope.cs.pp | |
public class StructureMapDependencyScope : IDependencyScope | |
{ | |
protected readonly IContainer Container; | |
public StructureMapDependencyScope(IContainer container) | |
{ | |
Container = container; | |
} | |
public object GetService(Type serviceType) | |
{ | |
return serviceType.IsAbstract || serviceType.IsInterface | |
? Container.TryGetInstance(serviceType) | |
: Container.GetInstance(serviceType); | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return Container.GetAllInstances(serviceType).Cast<object>(); | |
} | |
public void Dispose() | |
{ | |
Container.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment