c# - Custom resolve nested dependencies -


suppose following class structure:

classa (iclassb classb, iclassc classc) : iclassa classb (iclassc classc) : iclassb classc1 () : iclassc classc2 () : iclassc 

suppose want use classc1 whenever used resolve iclassa, i.e. container.getinstance<iclassa>() return

new classa(  new classb (new classc1()),  new classc1() ) 

can in structuremap? (ideally, for<classa>().nest(x => x.for<iclassc>().use<classc1>()), do.)

this got:

public interface iclassa { }  public interface iclassb { }  public interface iclassc { }  public class classa: iclassa {     public classa(iclassb classb, iclassc classc)     {         console.writeline("classa");         console.writeline("  classb: {0}", classb.gettype());         console.writeline("  classc: {0}", classc.gettype());     } }  public class classb : iclassb {     public classb(iclassc classc)     {         console.writeline("classb");         console.writeline("  classc: {0}", classc.gettype());     } }  public class classc1 : iclassc {     public classc1()     {         console.writeline("classc1");     } }  public class classc2 : iclassc {     public classc2()     {         console.writeline("classc2");     } }  class program {     static void main(string[] args)     {         icontainer container = configuredependencies();          container.getinstance<iclassa>();         container.getinstance<iclassb>();     }      private static icontainer configuredependencies()     {         return new container(x =>         {             x.for<iclassa>().use<classa>();             x.for<iclassb>().use<classb>();             x.for<iclassc>().use(z => z.roottype == typeof(classa) ? (iclassc) z.getinstance<classc1>() : z.getinstance<classc2>());         });     }  } 

concrete type of iclassc determined roottype.

this output:

classc1 classb   classc: mongodbenumerable.classc1 classa   classb: mongodbenumerable.classb   classc: mongodbenumerable.classc1 classc2 classb   classc: mongodbenumerable.classc2 

it shows classc1 used constructing classa , dependencies constructing separate iclassb used classc2.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -