c# - StructureMap MVC 3 IDependencyResolver -


interface idependencyresolver has 2 method getservice , getservices implement like:

public class smdependencyresolver : idependencyresolver {      private readonly icontainer _container;      public smdependencyresolver(icontainer container) {         _container = container;     }      public object getservice(type servicetype) {         if (servicetype == null) return null;         try {               return servicetype.isabstract || servicetype.isinterface                        ? _container.trygetinstance(servicetype)                        : _container.getinstance(servicetype);         }         catch (exception ex)         {              return null;         }     }      public ienumerable<object> getservices(type servicetype) {         return _container.getallinstances<object>().where(s => s.gettype() == servicetype);     } } 

but nead namedinstace dependencyresolver. objectfactory.getnamedinstace("instancename"); container how can that..

sorry bad english

create extension method did below. smdependencyresolver need expose _container variable read property.

public static class dependencyresolverextensions {         public static object getservice(this idependencyresolver resolver, type servicetype, string instancename)         {             var smresolver = resolver smdependencyresolver;              if (smresolver == null) throw new notsupportedexception();              return smresolver.container.getinstance(servicetype, instancename);         } } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -