StructureMap is an IoC container that we can use to inject implementations into the client. To do this in ASP.NET MVC we will use the constructor of the controller. Create a new ASP.NET MVC 4 project and install StructureMap via NuGet. When the package is installed a reference to StructureMap is automatically created.
Create a class named BootStrapper inside the root of the MVC project with the following code listing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| using StructureMap; using StructureMap.Configuration.DSL; using Mvc4ControllerFactoryAndStructureMap.Services; namespace Mvc4ControllerFactoryAndStructureMap { public class BootStrapper { public static void ConfigureDependencies() { ObjectFactory.Initialize(x => x.AddRegistry<ControllerRegistry>()); } } public class ControllerRegistry : Registry { public ControllerRegistry() { For<IProductService>().Use<ProductService>(); } } } |
Inside the ControllerRegistry class you specify the implementation(s) that belongs to a specific interface. The ConfigureDependencies method initializes the registry with the StructureMap framework.
Create another class named StructureMapControllerFactory inside the root of the MVC project with the following code listing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| using System; using System.Web.Mvc; using System.Web.Routing; using StructureMap; namespace Mvc4ControllerFactoryAndStructureMap { public class StructureMapControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { return ObjectFactory.GetInstance(controllerType) as IController; } } } |
The StructureMapControllerFactory let StructureMap inject implementations inside the constructor of our controllers. Let's see how that looks like.
IProductService (Interface)
1
2
3
4
5
6
7
8
9
| using Mvc4ControllerFactoryAndStructureMap.Models; namespace Mvc4ControllerFactoryAndStructureMap.Services { public interface IProductService { Product GetProduct(); } } |
ProductService (Implements IProductService)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| using Mvc4ControllerFactoryAndStructureMap.Models; namespace Mvc4ControllerFactoryAndStructureMap.Services { public class ProductService : IProductService { public Product GetProduct() { Product product = new Product(); product.Name = "Test" ; product.Description = "This product is awesome." ; return product; } } } |
ProductController (Implementations injected via constructor)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| using System.Web.Mvc; using Mvc4ControllerFactoryAndStructureMap.Services; using Mvc4ControllerFactoryAndStructureMap.Models; namespace Mvc4ControllerFactoryAndStructureMap.Controllers { public class ProductController : Controller { private readonly IProductService _productService; public ProductController(IProductService productService) { _productService = productService; } public ActionResult Index() { Product product = _productService.GetProduct(); return View(product); } } } |
As you can see the constructor of the ProductController takes an IProductService parameter and stores that in _productService. StructureMap has the ability to look at what the constructor expects and inject the implementation. To provide this implementation to the controllers constructor you created a custom controller factory: StructureMapControllerFactory. Every method inside the controller can now use the ProductService implementation by using _productService.
Now let's configure the dependencies at startup because we only need to do that once. Open global.asax and add the following lines to Application_Start:
1
2
| BootStrapper.ConfigureDependencies(); ControllerBuilder.Current.SetControllerFactory( new StructureMapControllerFactory()); |
The BootStrapper.ConfigureDependencies method registers all the dependencies with StructureMap. After that we change the current controller factory to our own StructureMapControllerFactory so that the dependencies are injected when needed.
That's it. You can download the source code and hit Ctrl-F5 to see it working: Download
Comments
Post a Comment