.net - How to tell StructureMap 3 to use specific constructor for specific type? -



.net - How to tell StructureMap 3 to use specific constructor for specific type? -

i using structuremap (version 3.1.4.143) general dependency resolution in web api project, , it's working fine far. want structuremap follow it's default behavior of selecting constructor parameters. however, specific type want utilize specific constructor used.

e.g. have service contract

public interface iservice { void dosomething(); }

and implementation

public class service : iservice { public service() { //something } public service(irepo repo, ilogger logger) { //something } //rest of logic }

for type only, want utilize parameter-less constructor. how do in structuremap 3 ? (i types creating instance of iconstructorselector , applying policy below)

x.policies.constructorselector<paramlessconstructorselector>();

answering own question:

this right way in structuremap 3. selectconstructor, structuremap infers constructor given expression.

x.forconcretetype<service>().configure.selectconstructor(() => new service());

or, can specified for-use-mapping.

x.for<iservice>().use<service>().selectconstructor(() => new service());

check documentation in github structuremap docs.

if rule needs applied throughout application, rule can applied policy creating instance of iconstructorselector

public class paramlessconstructorselector : iconstructorselector { public constructorinfo find(type pluggedtype) { homecoming pluggedtype.getconstructors().first(x => x.getparameters().count() == 0); } }

and configuring container.

x.policies.constructorselector<paramlessconstructorselector>();

.net inversion-of-control structuremap structuremap3

Comments

Popular posts from this blog

maven fortify plugin : Unable to load build session with ID XXXXX .. See log file for more details -

c# - Primavera WebServices does not return any data -

android - Display emoji panel with genymotion - keyboard/touch input? -