ASP. NET webform uses IOC containers (spring. Net containers, Castle, etc)

Source: Internet
Author: User

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);                }            }        }    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.