AOP for Aspect-Oriented Programming, Attribute application in projects, aopattribute

Source: Internet
Author: User

AOP for Aspect-Oriented Programming, Attribute application in projects, aopattribute

Reposted from huide's blog

I. Introduction to AOP

During our usual development, we generally face object programming. Object-oriented features inheritance, polymorphism, and encapsulation. Our business logic code is mainly written in these classes, however, while implementing the business, we will inevitably have multiple repeated operations. AOP is to extract these repeated operations and use dynamic proxies to achieve unified maintenance of program functions, for example, login judgment, permission judgment, and logging are all implemented through AOP. I think there are a lot of theoretical knowledge about AOP on the Internet, I will not introduce them one by one, and I am not very familiar with it. Today I will explain AOP from the Attribute I used in development.

II. Introduction to Attribute

In this case, Attribute is a tag, which is equivalent to writing remarks to the marked class or method. What is the difference between Attribute and common remarks? The main difference is that common remarks cannot be called by a program, while values in Attribute can be called by a program.

Let's take a look at the code first.

First, we define an Attribute class, which inherits from the base class Attribute.

[AttributeUsage(AttributeTargets.Class,AllowMultiple=false)]  public class VersionAttribute : Attribute  {    public string Name { get; set; }    public string Date { get; set; }    public string Describtion { get; set; }  }

AttributeUsage is also a feature with two parameters. AttributeTargets is used to mark the scope of the current feature, which is usually used by All, Class, Method, and Assembly. You can also find out about other features, allowMultiple whether the property indicated by this value can be inherited by the derived class and the override Member
Next we will write a class using Attribute.

[Version(Name = "hyddd", Date = "2009-07-20", Describtion = "hyddd's class")]  public class MyCode  {    //...  }

The Attribute in this section is written above, which is to assign values to the VersionAttribute. At the same time, we can also use its attributes while using MyCode elsewhere.

Attribute Application

class Program{  static void Main(string[] args)  {    var info = typeof(MyCode);    var classAttribute = (VersionAttribute)Attribute.GetCustomAttribute(info, typeof(VersionAttribute));    Console.WriteLine(classAttribute.Name);    Console.WriteLine(classAttribute.Date);    Console.WriteLine(classAttribute.Describtion);  }}

  

Iii. Introduction to ActionFilterAttribute

ActionFilterAttribute inherits three base classes: FilterAttribute, IActionFilter, and IResultFilter. Its main function is to have the interceptor function. IActionFilter defines the connection methods, which are OnActionExecuting () with OnActionExecuted (), OnActionExecuting () is implemented by ASP. net mvc framework call; OnActionExecuted () is implemented by ASP. net mvc framework call. IResultFilter defines two methods: OnResultFiltering (), OnResultFiltered (), and OnResultFiltering (). ASP. net mvc framework call. OnResultFiltered () is called by the ASP. net mvc framework after the operation result is executed.

 

Iv. Attribute AOP Application

 

Public class usercheckattriattribute: ActionFilterAttribute, IActionFilter {public override void OnActionExecuting (ActionExecutingContext filterContext) {var controller = filterContext. controller as WebsiteController; if (controller = null) return; var loginInfo = controller. loginInfo; if (loginInfo = null | loginInfo. merchant. merchantId = 0) return; var result = SubscribeBusiness. checkMerchantMa XUser (loginInfo. Merchant. MerchantId); if (result! = Null & result. success = false) {if (filterContext. requestContext. httpContext. request. isAjaxRequest () {filterContext. result = new JsonResult () {Data = new {Success = false, Message = string. format ("the user has reached the upper limit. Please upgrade the service and try again. <a href = \" {0} \ "style = \" color: red; \ "target = \" _ blank \ "> click here to upgrade! </A> ", RouteHelper. action ("index", "purchase", new {area = ""})}, JsonRequestBehavior = JsonRequestBehavior. allowGet };} else {filterContext. result = new RedirectAction ("index", "purchase", new {area = ""}) ;}} base. onActionExecuting (filterContext );}}

This is an Attribute class that checks that the user has reached the upper limit and provides a corresponding prompt. The OnActionExecuting method is rewritten in the classes. If the user reaches the upper limit, the Result of ActionExecutingContext is changed.

 

Call Method

[UserCheck][HttpPost]public JsonResult AddParner(DealerSaveContext model){    ......    return Json(new { Success = result.Success, Message = result.Message });}

 

In this way, the permission check is completed using AOP.

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.