I am new to MVC 3 and StructureMap, so please bear with me if I am asking a basic question.
In my project we have a Web project (MVC3), IRepository project (has repository interface) and a Repository project (has implementation classes for repository). In the Web project we are trying to use the repository classes through the interface. I want to
use structuremap for injecting the repository objects.
Here is my implementation
In the controller the interface is used like this
public class GroupController : Base
{
private IRepository.IUser IUser;
public GroupController()
{
this.IUser = ObjectFactory.GetInstance<IRepository.IUser>();
}
............
}
For the purpose of injecting the User object I have the following code in my Global.asax.cs
This works fine and I am able to run the application, but I am not sure if the implementation is correct. The questions I have are
1. In the web project I ended up adding both IRepository and Repository projects as reference. I had to add Repository project because it is used in the below statement. Is there a way to add just the IRepository as reference in the web tier and completely
hide the implementation project?
Your web layer (where the IoC container lives) needs to know about both the interface and the concrete class in order to do the mapping. So you do need a reference to both libraries.
With that said, the easiest way to set up structuremap to do constructor injection is to use the nuget package. In the nuget manager screen look for StructureMap.MVC3.
That will install the dll and do the intial setup. You should find a folder called DependencyResolution, in there you'll find a file called IoC.cs. Uncomment this line:
x.For<IExample>().Use<Example>();
That's where the mapping between interface and concrete class is done, in your case it would be:
IgnatiusNS
Member
5 Points
5 Posts
MVC3 and StructureMap Implementation
Oct 12, 2011 09:22 PM|LINK
Hi,
I am new to MVC 3 and StructureMap, so please bear with me if I am asking a basic question.
In my project we have a Web project (MVC3), IRepository project (has repository interface) and a Repository project (has implementation classes for repository). In the Web project we are trying to use the repository classes through the interface. I want to use structuremap for injecting the repository objects.
Here is my implementation
In the controller the interface is used like this
public class GroupController : Base { private IRepository.IUser IUser; public GroupController() { this.IUser = ObjectFactory.GetInstance<IRepository.IUser>(); } ............ }For the purpose of injecting the User object I have the following code in my Global.asax.cs
This works fine and I am able to run the application, but I am not sure if the implementation is correct. The questions I have are
1. In the web project I ended up adding both IRepository and Repository projects as reference. I had to add Repository project because it is used in the below statement. Is there a way to add just the IRepository as reference in the web tier and completely hide the implementation project?
ObjectFactory.Configure(x => x.For<IRepository.IUser>().HybridHttpOrThreadLocalScoped().
Use<Repository.User>());
2. I also would like to use Constructor Injection, but I am not sure how to do it. Is there a good article or walkthrough that shows that?
Thanks
CodeHobo
All-Star
18647 Points
2647 Posts
Re: MVC3 and StructureMap Implementation
Oct 13, 2011 03:30 AM|LINK
Your web layer (where the IoC container lives) needs to know about both the interface and the concrete class in order to do the mapping. So you do need a reference to both libraries.
Take alook at this post on using structuremap with mvc3.
http://stevesmithblog.com/blog/how-do-i-use-structuremap-with-asp-net-mvc-3/
With that said, the easiest way to set up structuremap to do constructor injection is to use the nuget package. In the nuget manager screen look for StructureMap.MVC3. That will install the dll and do the intial setup. You should find a folder called DependencyResolution, in there you'll find a file called IoC.cs. Uncomment this line:
That's where the mapping between interface and concrete class is done, in your case it would be:
No in your controller you can just do this:
public class GroupController : Base { private IRepository.IUser IUser; public GroupController(IUser IUser) { this.IUser = IUser } ............ }The controller now doesn't even know about structuremap, it just knows to that it will be given an IUser.
Blog | Twitter : @Hattan
IgnatiusNS
Member
5 Points
5 Posts
Re: MVC3 and StructureMap Implementation
Oct 13, 2011 04:09 PM|LINK
Thanks! It was very helpful.