The webform model is not like MVC. The controller of MVC is obtained in the factory mode and has the concept of controllerfactory. Therefore, webform cannot directly replace the Controller factory like MVC.
You don't need to think about constructor injection. aspx is directly initialized to an object by. net. You have no chance to intervene in this process and can only start with attribute injection later.
Therefore, there are two main implementation ideas:
1. In the Aspx. CS file, the property to be injected gets the object directly from springcontext.
ClassName object = (ClassName)ContextRegistry.GetContext().GetObject("objectId");
In this way, this method is suitable for a small number of pages, and occasionally under the "samples" (I embedded An ASPX. CS file in the MVC project, this method is used for the convenience of the figure .)
2. Traverse ihttphandler through httpmodule (the ASPX page is the page class, which also inherits the attributes in this interface), detect the attribute and implement Injection
Note that the castle container recommends the concept of automatic assembly, so the attribute is represented by type, and spring usually uses ID to describe an object. Therefore, it is best to include an objectid field on the attribute, the two methods are best supported at the same time.
The following is an example of a castle container. This can also be applied to spring. The method for obtaining the container context in spring is as follows:
ContextRegistry.GetContext();
The subsequent usage is similar to that of Castle.
(This code is copied from the Internet ~ )
public partial class IndexPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Logger.Write("page loading"); } [Inject] public ILogger Logger { get; set; } } // WindsorHttpModule.cs public class WindsorHttpModule : IHttpModule { private HttpApplication _application; private IoCProvider _iocProvider; public void Init(HttpApplication context) { _application = context; _iocProvider = context as IoCProvider; if(_iocProvider == null) { throw new InvalidOperationException("Application must implement IoCProvider"); } _application.PreRequestHandlerExecute += InitiateWindsor; } private void InitiateWindsor(object sender, System.EventArgs e) { Page currentPage = _application.Context.CurrentHandler as Page; if(currentPage != null) { InjectPropertiesOn(currentPage); currentPage.InitComplete += delegate { InjectUserControls(currentPage); }; } } private void InjectUserControls(Control parent) { if(parent.Controls != null) { foreach (Control control in parent.Controls) { if(control is UserControl) { InjectPropertiesOn(control); } InjectUserControls(control); } } } private void InjectPropertiesOn(object currentPage) { PropertyInfo[] properties = currentPage.GetType().GetProperties(); foreach(PropertyInfo property in properties) { object[] attributes = property.GetCustomAttributes(typeof (InjectAttribute), false); if(attributes != null && attributes.Length > 0) { object valueToInject = _iocProvider.Container.Resolve(property.PropertyType); property.SetValue(currentPage, valueToInject, null); } } } }