c# - Referencing a common instance from a DI constructor -
using ninject di, have implemented 2 interfaces instantiate mvc controllers. example:
public class mycontroller : controller { private readonly iunitofwork _unitofwork; private readonly iassetservice _assetservice; public mycontroller(iunitofwork unitofwork, iassetservice assetservice) { this._unitofwork = unitofwork; this._assetservice = assetservice; } // controller actions etc. }
in ninject module have created next bindings:
public class domainmodule : ninjectmodule { public override void load() { bind<iunitofwork>() .to<sqlunitofwork>() .inrequestscope() .withconstructorargument("connectionstring", "mydb.database"); bind<iassetservice>() .to<filesystemassetservice>() .withconstructorargument("rootpath", "c:\\datastore"); } }
i want inject iunitofwork
instance iassetservice
have considered making property of iassetservice
, modifying controllers follows:
public class mycontroller : controller { private readonly iunitofwork _unitofwork; private readonly iassetservice _assetservice; public mycontroller(iunitofwork unitofwork, iassetservice assetservice) { this._unitofwork = unitofwork; this._assetservice = assetservice; this._assetservice.unitofwork = this._unitofwork; } // controller actions etc. }
but wondered if there better/cleaner way of doing using different di technique - ideally add together iunitofwork
assetservice
constructor?
then why not inject iunitofwork
assetservice
?
public class filesystemassetservice : iassetservice { private readonly iunitofwork unitofwork; private readonly string rootpath; public filesystemassetservice(iunitofwork unitofwork, string rootpath) { this.unitofwork = unitofwork; this.rootpath = rootpath; } }
c# asp.net-mvc ninject
No comments:
Post a Comment