How to resolve idependency by key

Topics: Customizing Orchard, Writing modules
Mar 3, 2013 at 9:02 PM
hello
I have the following interface structure using inheritance :
public interface IFlowBase : IDependency 
    {
    }
    
    public interface IFlowMain : IFlowBase 
    {string Key { get;  }
     
    }
    
    and in my concreate classes i have 
    
    public class Flow1 :BaseFlow, IFlowMain
    {
        public override string Key {
            get { return "Flow1"; }
          
        }
    
    }
     public class Flow2 :BaseFlow, IFlowMain
    {
        public override string Key {
            get { return "Flow2"; }
          
        }
    
    }
So basically many concrete classes impelement the same interface (Iflowmain which derives from IFlowBase which derives from idependency).
How can i resolve this so that i get back a specific implementation based on the key passed in as a string to the resolver.
I can currently do this in a consuming class :
  myinstance = _workContextAccessor.GetContext().Resolve<IEnumerable<IFlowMain>>().FirstOrDefault(x => x.Key == "Flow1");
This works , but i think from a performance side it is terrible because it resolves all instances and then returns the one i need.
What is the most efficient way o do this ?
Ideally i would like to call something like this :
_workContextAccessor.GetContext().Resolve<IFlowMain>().BySomeKey("Flow1")
and will resolve only based on the key .
Thanks in advance
giannis
Coordinator
Mar 4, 2013 at 1:05 AM
Inject an IEnumerable<IFlowMain> and then you can do a .FirstOrDefault(x => x.Key == "Flow1").