开发者

StructureMap and MVC 3.0

开发者 https://www.devze.com 2023-02-06 12:41 出处:网络
Has anyone used structuremap with the new DI features of asp.net m开发者_JS百科vc 3.0?If so, could you post some example code or some links to examples that have been helpful?In attempting to learn AS

Has anyone used structuremap with the new DI features of asp.net m开发者_JS百科vc 3.0? If so, could you post some example code or some links to examples that have been helpful? In attempting to learn ASP.NET MVC 3, I am trying to make sure I use all the "new" features and am struggling with integrating a IoC container.


As others mentioned you'd want to look at Common Service Locator (CSL) that is a simple service resolution facade around any container (any of your choice).

MVC3 internally heavily uses the new IDependencyResolver interface which is somewhat like CSL but in ASP.NET MVC context. The good thing is that if you are already using CSL then you can set it as Dependency Resolver for MVC.

This topic is equally essential for any container and not specific to StructureMap.

So what is going on there:

  • You provide all necessary container registrations (preferrably through Registry DSL in case of StructureMap);
  • You get the StructureMap adapter for Common Service Locator;
  • You (optionally - if you need CSL along with DependencyResolver) register your StructureMap adapter as current Service Locator: ServiceLocator.SetLocatorProvider(() => yourStructureMapAdapter)
  • You register your CSL (backed by StructureMap) as MVC DependencyResolver: DependencyResolver.SetResolver(yourStructureMapAdapter)
  • MVC3 automatically wires everything up through IDependencyResolver interface internally (using all StrucutreMap DI auto-wiring capabilities).

Along with MVC3 baked-in IoC capabilities, use the power of IoC tool at your disposal (e.g. use Assemblies scanning available in StructureMap) to max extent.


Ran into this the other day, may be helpful:

http://weblogs.asp.net/rashid/archive/2009/02/15/asp-net-mvc-unity-and-common-service-locator.aspx


I looked around a bit and this is the first Google result I got. It gives a good of what is new in MVC 3 service location: http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html

While it is dependent on a beta version of MVC 3, I am sure with some experimentation one can figure it out.

In a nutshell, it looks like they added some interfaces and extension methods that you can use to call StructureMap, or whatever your preferred IoC library is.

Update:

I just happened upon this link in the blogs I subscribe to. It has some good-looking sample code. http://stevesmithblog.com/blog/how-do-i-use-structuremap-with-asp-net-mvc-3/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号