property injection - How can StructureMap be used to create setter dependencies of objects that have primitive constructor argumeents? -
i have object "testproperty" implements "itestproperty". "testproperty" takes string constructor argument. configured in structuremap using along lines ctordependency or withctorarg.
i want inject instance of "itestproperty" (implemented "testproperty") class property. when try run code exception (structuremap error code 205, "missing requested instance property").
here's simplified version recreates problem:
test:
[test] public void can_resolve_the_correct_property() { objectfactory.initialize( x => x.addregistry( new testregistry() ) ); var instance = objectfactory.getinstance<testcontroller>(); }
registry setup:
public class testregistry : registry { public testregistry() { forrequestedtype<itestproperty>().addinstances( => i.ofconcretetype<testproperty>().withname( "test" ) .ctordependency<string>( "arg" ).is( "abc" ) ); //forconcretetype<testproperty>().configure .ctordependency<string>( "arg" ).is( "abc" ); forconcretetype<testcontroller>().configure .setterdependency( p => p.property ).is<testproperty>() .withname( "test" ); } }
test objects:
public interface itestproperty { } public class testproperty : itestproperty { private readonly string arg; public testproperty( string arg ) { this.arg = arg; } public string arg { { return arg; } } } public class testcontroller { public itestproperty property { get; set; } }
when go initialize "testcontroller" object above exception thrown. possible structuremap? assuming possible, need working?
thanks in advance.
there several ways this, josh mentioned if named instance important want in registry:
forrequestedtype<itestproperty>().addinstances(i => i.ofconcretetype<testproperty>().withname("test") .withctorarg("arg").equalto("abc")); forconcretetype<testcontroller>().configure .setterdependency(p => p.property).is(c => c .getinstance<itestproperty>("test"));
otherwise, can this:
forrequestedtype<itestproperty>().thedefault.is .ofconcretetype<testproperty>() .withctorarg("arg").equalto("abc"); forconcretetype<testcontroller>().configure .setterdependency(p => p.property).isthedefault();
also, old structuremap syntax, might want update latest version. here new syntax:
for<itestproperty>().add<testproperty>().named("test") .ctor<string>("arg").is("abc"); forconcretetype<testcontroller>().configure .setter(p => p.property).is(c => c .getinstance<itestproperty>("test"));
Comments
Post a Comment