ioc container - Autofac: how do I pass a reference to the component being resolved to one of its dependents? -
with following:
public class aclass { public adependent dependent { get; set; } } public class adependent { public adependent(aclass ownervalue) {} }
with following registrations...
builder.registertype<aclass>().propertiesautowired().instanceperdependency(); builder.registertype<adependent>().propertiesautowired().instanceperdependency();
when resolve aclass, how make sure 'ownervalue' instance of aclass being resolved, , not instance? thx
follow on
the example above doesn't catch problem properly, how wire adependent when registering when scanning... example
public class aclass : iaclass { public iadependent dependent { get; set; } } public class adependent : iadependent { public adependent(iaclass ownervalue) {} } // registrations... builder.registerassemblytypes(assemblies) .assignableto<iaclass>() .as<iaclass>() .instanceperdependency() .propertiesautowired(); builder.registerassemblytypes(assemblies) .assignableto<iadependent>() .as<iadependent>() .instanceperdependency() .propertiesautowired();
the function looking relationship type like
public class adependent : iadependent { public adependent(ownedby<iaclass> ownervalue) {} }
the ownedby indicates ownervalue instance caused adependent created. make sense? make wiring ui components breeze.
to extend steven's approach, can resolve()
second class, passing first instance parameter:
builder.registertype<adependent>(); builder.register<aclass>(c => { var = new aclass(); a.dependent = c.resolve<adependent>(typedparameter.from(a)); return a; });
Comments
Post a Comment