Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
MVC4RC and StructureMap
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public interface IHelloService
{
string Hello(string user);
}
public class HelloService : IHelloService
{
public string Hello(string user)
{
return "Hello " + user;
}
}
public class HomeController : Controller
{
private readonly IHelloService _helloService;
public HomeController(IHelloService helloService)
{
_helloService = helloService;
}
public ActionResult Index()
{
ViewBag.Message = _helloService.Hello("Steve");
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Dependencies;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using StructureMap;
using StructureMap.ServiceLocatorAdapter;
using System.Linq;
[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication1.App_Start.StructuremapMvc), "Start")]
namespace MvcApplication1.App_Start
{
public static class StructuremapMvc
{
public static void Start()
{
var container = (IContainer)IoC.Initialize();
DependencyResolver.SetResolver(new SmDependencyResolver(container));
// this override is needed because WebAPI is not using DependencyResolver to build controllers
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
}
}
/// <summary>
/// Wrapper for IDependencyScope, so that StructureMap plays nicely with built in mvc4 dependency resolution.
/// </summary>
public class StructureMapDependencyScope : ServiceLocatorImplBase, IDependencyScope
{
protected readonly IContainer Container;
public StructureMapDependencyScope(IContainer container)
{
if (container == null)
throw new ArgumentNullException("container");
Container = container;
}
public object GetService(Type serviceType)
{
if (serviceType == null)
return null;
try
{
return serviceType.IsAbstract || serviceType.IsInterface
? Container.TryGetInstance(serviceType)
: Container.GetInstance(serviceType);
}
catch
{
return null;
}
}
/// <summary>
/// When implemented by inheriting classes, this method will do the actual work of resolving
/// the requested service instance.
/// </summary>
/// <param name="serviceType">Type of instance requested.</param>
/// <param name="key">Name of registered service you want. May be null.</param>
/// <returns>
/// The requested service instance.
/// </returns>
protected override object DoGetInstance(Type serviceType, string key)
{
if (string.IsNullOrEmpty(key))
{
return Container.GetInstance(serviceType);
}
return Container.GetInstance(serviceType, key);
}
/// <summary>
/// When implemented by inheriting classes, this method will do the actual work of
/// resolving all the requested service instances.
/// </summary>
/// <param name="serviceType">Type of service requested.</param>
/// <returns>
/// Sequence of service instance objects.
/// </returns>
protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
return Container.GetAllInstances(serviceType).Cast<object>();
}
public IEnumerable<object> GetServices(Type serviceType)
{
return Container.GetAllInstances(serviceType).Cast<object>();
}
public void Dispose()
{
Container.Dispose();
}
}
/// <summary>
/// Wrapper for IDependencyResolver so that StructureMap plays nicely with built in mvc 4 dependency resolution.
/// </summary>
public class StructureMapDependencyResolver : StructureMapDependencyScope, System.Web.Http.Dependencies.IDependencyResolver
{
public StructureMapDependencyResolver(IContainer container)
: base(container)
{
}
public IDependencyScope BeginScope()
{
var child = Container.GetNestedContainer();
return new StructureMapDependencyResolver(child);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment