asp.net mvc 3 - Property injection into custom WebViewPage using Autofac -
asp.net mvc 3 - Property injection into custom WebViewPage using Autofac -
i'm creating internal application framework other dev teams in our organisation utilize build mvc applications from. part of that, i'm creating menu construction views, read configuration , modified based on current user's permissions. create menu part of framework, i've created custom webviewpage
implementation custom html helper class needs take dependency on applicationdatareader
build menu.
i've read various posts explain mvc needs webviewpage
have paramterless constructor, need utilize property injection. i've configured autofac mvc3 integration, including registering viewregistrationsource
. problem is, when dependent property accessed, it's null.
here's custom view page , helper phone call i'm trying make:
public abstract class oubaseviewpage<tmodel> : webviewpage<tmodel> tmodel : class { public ouhelper<tmodel> ou { get; set; } public override void inithelpers() { base.inithelpers(); ou = new ouhelper<tmodel>(viewcontext, this); } } public class ouhelper<tmodel> tmodel : class { public ouhelper(viewcontext viewcontext, iviewdatacontainer viewdatacontainer) : this(viewcontext, viewdatacontainer, routetable.routes) { } public ouhelper(viewcontext viewcontext, iviewdatacontainer viewdatacontainer, routecollection routecollection) { viewcontext = viewcontext; viewdata = new viewdatadictionary<tmodel>(viewdatacontainer.viewdata); } public viewdatadictionary<tmodel> viewdata { get; private set; } public viewcontext viewcontext { get; private set; } public ilist<businessfocusarea> readfocusareas() { // null - yes, service location, isolated instance of... homecoming dependencyresolver.current.getservice<idatareader>().read(); } }
the problem stems fact inithelpers
beingness called (via layout.execute()
) before application_start
called, none of dependencies have been registered. know it's not practice inject logic views , views should dumb, application framework , needs perform setup steps developers using framework mustn't have visibility of.
is there improve approach follow?
there's similar issue here: can autofac inject dependencies layout view files?
the problem stems fact inithelpers beingness called (via layout.execute()) before application_start called
i don't think called before application_start
. cannot reproduce problem.
here steps did , worked fine:
create new asp.net mvc 3 application using net template installautofac.mvc3
nuget define dummy interface:
public interface idatareader { }
and dummy implementation:
public class datareader : idatareader { }
define custom helper:
public class ouhelper<tmodel> tmodel : class { private readonly idatareader datareader; public ouhelper(viewcontext viewcontext, iviewdatacontainer viewdatacontainer, idatareader datareader) : this(viewcontext, viewdatacontainer, routetable.routes, datareader) { } public ouhelper(viewcontext viewcontext, iviewdatacontainer viewdatacontainer, routecollection routecollection, idatareader datareader) { viewcontext = viewcontext; viewdata = new viewdatadictionary<tmodel>(viewdatacontainer.viewdata); this.datareader = datareader; } public viewdatadictionary<tmodel> viewdata { get; private set; } public viewcontext viewcontext { get; private set; } public idatareader datareader { { homecoming this.datareader; } } }
define custom webviewpage using helper:
public abstract class oubaseviewpage<tmodel> : webviewpage<tmodel> tmodel : class { public ouhelper<tmodel> ou { get; set; } public override void inithelpers() { base.inithelpers(); var datareader = dependencyresolver.current.getservice<idatareader>(); ou = new ouhelper<tmodel>(viewcontext, this, datareader); } }
replace default view page custom 1 in ~/views/web.config
:
<pages pagebasetype="mvcapplication1.oubaseviewpage">
configure container in application_start
:
protected void application_start() { arearegistration.registerallareas(); registerglobalfilters(globalfilters.filters); registerroutes(routetable.routes); var builder = new containerbuilder(); builder.registercontrollers(typeof(mvcapplication).assembly); builder.registertype<datareader>().as<idatareader>(); var container = builder.build(); dependencyresolver.setresolver(new autofacdependencyresolver(container)); }
now happily utilize custom helper in views including _layout without problems:
@ou.datareader.gettype()
of course of study in illustration have exposed idatareader
dependency public property illustrate injected , never null. in particular code of course of study utilize private readonly field within helper accomplish task.
asp.net-mvc-3 autofac
Comments
Post a Comment