Assuming you already have ASP.NET MVC3 framework installed, start VS 2010 and create new project "ASP.NET MVC 3 Web Application". I do recommend to select Razor View Engine as compared to ASPX it provides much cleaner interface and it's really easy to use, in fact once you have tried it you'll never go back to ASPX.
The easiest way to add StructureMap reference to you project is to use "Add Library Package Reference..." from context menu on project "References":
On the following window select "Online" packages, type "structuremap" into search box and after that locate "structuremap" package in result section, select it and click "Install". TADA! The package will be downloaded into "{YourSolutionFolder}\packages" folder and reference to "StructureMap.dll" is added to project. Neat.
To apply Dependency Injection into controller's constructor let's add some code.
1. Create custom ControllerFactory class:
2. Update controller to accept IRepository interface in constructor:
3. Create Application registry class which must be inherited from "Registry" class. In this class you need to map Interfaces to Concrete Implementations:
4. Create static Bootstrapper class to configure ObjectFactory:

5. And finally update Global.asax file to initialize StructureMap:
This is it!
Now if you run your application StructureMap will inject into controller's constructor concrete implementation of IRepository interface, which in this case will be MemberRepository!
No comments:
Post a Comment