Self-implemented simple AOP (5) allows the Demo to adapt to webApi, and can also complete automatic attribute injection, aopwebapi

Source: Internet
Author: User

Self-implemented simple AOP (5) allows the Demo to adapt to webApi, and can also complete automatic attribute injection, aopwebapi

 

In the previous Demo, the Controller of webApi cannot be automatically injected because IHttpController and IController are activated through two different channels.

IHttpController is activated through the IHttpControllerActivator interface.

// Summary: // defines the methods required by System. Web. Http. Dispatcher. IHttpControllerActivator. Public interface IHttpControllerActivator {// Abstract: // create a System. Web. Http. Controllers. IHttpController object. //// Parameter: // request: // message request. //// ControllerDescriptor: // HTTP controller descriptor. //// ControllerType: // type of the controller. //// Return result: // System. Web. Http. Controllers. IHttpController object. IHttpController Create (HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType );}

The default implementation class is DefaultHttpControllerActivator.

// Abstract: // indicates the default implementation of System. Web. Http. Dispatcher. IHttpControllerActivator. You can use System. Web. Http. Services. DependencyResolver // to register different implementations. For each System. web. http. controllers. the HttpControllerDescriptor instance has one // System. web. http. controllers. apiControllerActionInvoker instance is optimized, but a System is also supported. web. http. controllers. apiControllerActionInvoker // has multiple systems. web. http. controllers. the status of the HttpControllerDescriptor instance. In the latter case, the query will be slower, because the Query Needs to traverse the // HttpControllerDescriptor. Properties directory. Public class DefaultHttpControllerActivator: IHttpControllerActivator {// Summary: // initialize a new instance of the System. Web. Http. Dispatcher. DefaultHttpControllerActivator class. Public DefaultHttpControllerActivator (); // Abstract: // use the given request to create the System. Web. Http. Controllers. IHttpController specified by controllerType. //// Parameter: // request message. //// ControllerDescriptor: // controller descriptor. //// ControllerType: // type of the controller. //// Return result: // instance of the controllerType. Public IHttpController Create (HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType );}

 

The simplest way to get the above information is to inherit and override DefaultHttpControllerActivator. Unfortunately, its Create function is not a virtual function and cannot be rewritten. What should we do? Change the behavior of an object dynamically-the decoration mode. Of course, there is no need to strictly follow the decoration mode to apply it. It is completely unnecessary. You only need to decorate DefaultHttpControllerActivator according to the decoration principle, and then register the decoration object to the system.

For example:

/// <Summary> /// used for Web Api /// </summary> private class MyHttpControllerActivator: IHttpControllerActivator {private DefaultHttpControllerActivator defaultActivator; public MyHttpControllerActivator () {this. defaultActivator = new DefaultHttpControllerActivator ();} public IHttpController Create (HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) {IHt TpController httpController = this. defaultActivator. Create (request, controllerDescriptor, controllerType); if (httpController! = Null) {// automatic assembly attributes /// <para> enable proxy for the property object and delay the initialization of the object to be proxy </para> DelayProxyUtil. autowiredProperties (httpController);} return httpController ;}}

Then register when the application starts:

// Adapt AOP to WebApi GlobalConfiguration. Configuration. Services. Replace (typeof (IHttpControllerActivator), new MyHttpControllerActivator ());

 

In this way, the Controller attribute in WebApi will be automatically injected.

 

Source Code address: http://files.cnblogs.com/files/08shiyan/AOPDemo.zip

(Coming soon ...)

Related Article

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.