dependency injection - Ninject ASP.NET MVC without default constructor -
i've come across issue ninject not work unless there blank default constructor in controller. register bindings in ninjectwebcommon.cs in app_start follows:
private static void registerservices(ikernel kernel) { kernel.bind<iservices.iaccountprovider>().to<providers.account.service.accountprovider>(); kernel.bind<iservices.icontractprovider>().to<providers.contract.contractprovider>(); kernel.bind<iservices.iproductprovider>().to<providers.product.productprovider>(); kernel.bind<iservices.ichannelprovider>().to<providers.channel.channelprovider>(); kernel.bind<iservices.itaskprovider>().to<providers.task.taskprovider>(); kernel.bind<iservices.iexportprovider>().to<providers.export.exportprovider>(); kernel.bind<iservices.iimportprovider>().to<providers.import.importprovider>(); kernel.bind<iservices.iquoteprovider>().to<providers.quote.quoteprovider>(); kernel.bind<iservices.iemailprovider>().to<providers.email.emailprovider>(); kernel.bind<iservices.iquotemanagementprovider>().to<providers.quote.management.quotemanagementprovider>(); kernel.bind<iservices.iimportpricebookprovider>().to<providers.import.pricebook.pricebookprovider>(); kernel.bind<iservices.idashboardprovider>().to<providers.dashboard.dashboardprovider>(); kernel.bind<iservices.iauditprovider>().to<providers.audit.auditprovider>(); } }
in controller have properties declared like:
[inject] public iaccountprovider accountprovider { get; set; }
but work if have blank constructor so:
public accountcontroller() { }
otherwise following error:
error activating accountcontroller using implicit self-binding of accountcontroller no constructor available create instance of implementation type.
is there way me without default constructor or solution?
having thought this, suggest inject iaccountprovider parameter on constructor rather inject through property, believe preferred pattern dependency injection. may have reason being property, in case might better expose functionality through specific accessor methods. plus it's controller, can't think off hand why you'd need property.
Comments
Post a Comment