c# - StructureMap in multi-layer solution -
in solution have 3 projects: webapi, businesslayer , datalayer.
in webapi have structuremap configured use defaultregistry:
public class defaultregistry : registry { public defaultregistry() { scan( scan => { scan.thecallingassembly(); scan.withdefaultconventions(); }); for<business.interfaces.ibusinessclass>().use(ctx => new business.businessclass()); for<data.interfaces.idataclass>().use(ctx => new data.dataclass()); } }
a controller class in webapi looks this:
public class mycontroller : apicontroller { private readonly ibusinessclass _businessclass; public mycontroller(ibusinessclass businessclass) { _businessclass = businessclass; } public httpresponsemessage get(int id) { var success = _businessclass.dosomething(); return success ? request.createresponse(httpstatuscode.ok) : request.createerrorresponse(httpstatuscode.badrequest, "error"); } }
injecting dependency _businessclass in example works charm.
now let's take @ businessclass implementation:
public class businessclass : ibusinessclass { private readonly idataclass _dataclass; public businessclass(idataclass dataclass) { _dataclass = dataclass; } public void dosomething() { _dataclass.dosomethingwithdata(); } }
in example dependency injection _dataclass doesn't work. dataclass param in constructor null _dataclass field isn't initialized properly. doing wrong? i'll add businessclass defined in businesslayer project , dataclass defined in datalayer projects (both of projects being class libraries).
in you're default registry registering default conventions calling
scan.withdefaultconventions();
so these 2 calls redundant.
for<business.interfaces.ibusinessclass>().use(ctx => new business.businessclass()); for<data.interfaces.idataclass>().use(ctx => new data.dataclass());
like kenneth said aren't scanning assembly businessclass
located in.
public class defaultregistry : registry { public defaultregistry() { scan( scan => { scan.thecallingassembly(); scan.assemblycontainingtype<businessclass>(); scan.withdefaultconventions(); }); } }
Comments
Post a Comment