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
Post a Comment