Custom base page type for my razor views, how to use lock windsor for autwire properties?

My base page looks like this:

namespace ASDF.Mvc.ViewEngines.Razor { public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel> { public ISomeHelper SomeHelper { get; set; } } } 

My views /web.config

 <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="ASDF.Mvc.ViewEngines.Razor.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> </namespaces> </pages> </system.web.webPages.razor> 

How can I connect this wiring so that SomeHelper is connected using Castle.

This is currently returning null, I have already connected ISomeHelper and everything works fine for my controllers / repositories / classes of service .

I assume that this WebViewPage is called somewhere where my container does not have access (e.g. at the controller level).

How do I make this work?

+6
source share
2 answers

The first thing related to embedding dependencies in custom web pages is that you cannot use constructor injection. Sorry, and I hope that they will improve this in future versions. The reason for this is because the actual class that implements this is dynamically allocated at runtime by the ASP.NET engine.

Thus, we canonly use the injection properties at the moment.

Thus, one of the possibilities is to use a custom IDependencyResolver . Unfortunately, IDependencyResolver does not play well with Castle Windsor . For example, there would be a piece of cake with Ninject. All you have to do is decorate the SomeHelper property SomeHelper [Inject] attribute:

 [Inject] public ISomeHelper SomeHelper { get; set; } 

and it would be automatically associated with Ninject, as it uses a custom IDependencyResolver. I am afraid that with Windsor you will have to do this manually. Thus, you can make your container public in Global.asax, and then:

 public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel> { protected WebViewPage() { SomeHelper = MvcApplication.WindsorContainer.Resolve<ISomeHelper>(); } public ISomeHelper SomeHelper { get; set; } } 

I know this sucks, but I'm afraid it is a harsh reality. Or maybe switch to Ninject? It works great with ASP.NET MVC 3 and its IDependencyResolver .

+9
source

I think you will need to connect to the MVC3 Service Location to control how view pages are created. I have not used MVC3 yet, but I found an article describing the Location Location function , and which can give you the “hook” that you need to have access to WebViewPage before processing it.

Having accessed WebViewPage before processing it, you can use Windsor to enter the properties of your view page. By default, Windsor injects the constructor, but you can find an example of how to inject the properties through the extension method in the blog post. I did a couple of years ago on MVC1 ActionFilters.

0
source

Source: https://habr.com/ru/post/899093/


All Articles