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
.
source share