Resolving Dependencies in ASP.NET MVC 5 using StructureMap

If you are using StructreMap to resolve the dependencies in the ASP.NET MVC 5 application then Steve Smith’s blog post Resolving Dependencies in ASP.NET MVC 5 with StructureMap can be a great help for you. I have followed Steve’s blog to use the StructureMap. I am summarizing the steps as follows:

Using the Nuget Package Manager add package of StructureMap.mvc5 in the project

image 

Refactor controller to use the constructor injection.

image

If you are following the default convention means IMovieRepository should be resolved to MovieRepository then you should not care about any other files. However you can override default behavior in the DefaultRegistry class which is inside DependencyResolution folder. As you notice below that StructureMap is scanning with default convention.

image

An Important Note

Example shown in Steve blog is using Repository from same MVC project. What if Repository is not part of the same MVC project, in this case you need to explicitly specify in the StructureMap DefaultRegistry class that scan the assembly containing the Repository. This can be specified as follows:

image

As you see that we are explicitly specifying that the scan assembly which contains MovieRepository. In this case MovieRepository is part of another project MvcMovie.Infrastructure.MovieRepository. Other important class is StructuremapMvc which is inside App_Start folder.

This is all you need to configure to resolve dependency using StructureMap in the ASP.NET MVC application.

Happy Coding.

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

5 thoughts on “Resolving Dependencies in ASP.NET MVC 5 using StructureMap”

  1. Very helpful article, I was stuck on DI with ASP.NET MVC5 with StructureMap.MVC5. Quick look at your article saved my sunday. Great work. Keep helping. God bless you!

  2. Hi,

    My application has 3 layer web,Business and service.Web is MVC 5 application.Controller has reference of business layer.My interface and its implementation lies in business layer.I have used the solution you have provided but I am still facing the same issue.Can you please help me with the error.

    “No parameterless constructor defined for this object”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s