[Entlib] Microsoft Enterprise Library 5.0 learning path-Step 9, using policyinjection module for AOP-PART2-custom matching rule

Source: Internet
Author: User

During this time, the company has new projects to be developed, so the road to learning the enterprise database is a little delayed. Today, we will continue with the previous article. In the previous article, I introduced the basic information and usage of the built-in matching rule (matching rule) of the Enterprise Library, but there are still two matching rules in the policyinjection module, which are not described:

1. custom attribute matching rule -- custom property matching rules

2. Custom matching rule-custom matching rules

Today, we will mainly introduce these two matching rules and some application examples on these two matching rules.

 

I. custom attribute matching rule -- custom feature matching rules

This matching rule is a bit similar to the tag attribute matching rule, except that the tag attribute matching rule has fixed the feature class-tagattribute, the tag string of the tagattribute class is used to identify the specific policy name for specific matching. It is not easy to use and can be identified by a string, which may cause an error, however, the Enterprise Library also reserves an Extended Interface for us, which is the custom feature matching rule-custom attribute matching rule.

This matching rule is easy to use. You only need to define a feature class and configure it in the enterprise database configuration tool.

This matching rule receives two parameters: attribute type name and search inheritance chain.

1. attribute type name, which clearly indicates the name of the feature class. You only need to click the select button next to it to select your own defined feature class.

2. Search inheritance chain, which indicates whether to query the inheritance relationship. The default value is false, indicating that the query is not performed. You can understand the following code:

I first defined a custom feature class:

[AttributeUsage(AttributeTargets.Method)]public class MyCustomAttribute:System.Attribute{}

And then apply it to the Code:

public abstract class CustomAttrTest : MarshalByRefObject{    [MyCustom]    public abstract void CwInfo(string aa);}public class ChildCustomerAttrTest :  CustomAttrTest{    public override void CwInfo(string aa)    {        Console.WriteLine(aa);    }}

As you can see, I applied mycustomattriance to the abstract method cwinfo in the abstract class customattrtest, which is then implemented by the subclass childcustomerattrtest. If I set the search inheritance chain to false, the method will not be blocked in the Process of calling the method, because mycustomattribute is applied in the abstract class rather than in the implementation class, so if you want to define the interception by using the abstract class, set search inheritance chain to true.

The specific configuration diagram is as follows:

 

Note: Due to a bug in the policyinjection module, when creating an object through policyinjection, if the created object needs to point to an abstract class, an error is reported. The Code is as follows:

CustomAttrTest customAttr = PolicyInjection.Create<ChildCustomerAttrTest, CustomAttrTest>();

For specific questions, refer to this article by artech: Is this a bug of enterlib piab ?, This section describes the causes and solutions of the problem.

 

Through custom attribute matching rule, we can set up feature classes as needed, such as exceptions, permissions, logs, and so on, and establish feature classes by ourselves, in this way, we can well separate various functions. In the specific process, we can freely combine the required functions, as shown in the following code:

First, I will define several feature classes to be used:

[AttributeUsage(AttributeTargets.Method)]public class ExceptionAttribute : System.Attribute{}[AttributeUsage(AttributeTargets.Method)]public class LoggingAttribute : System.Attribute{}[AttributeUsage(AttributeTargets.Method)]public class SecurityAttribute : System.Attribute{}public class MyCommonAttribute : System.Attribute{}

Then associate different processing operations for each feature class:

You only need to add features for the method as needed:

[MyCustom][Logging][Exception]public void Delete(int id){}

 

Ii. Custom matching rule-custom matching rules

If so many matching rules built in the enterprise database cannot meet your needs, you can try to write matching rules by yourself. Of course, the enterprise database also reserves this interface, that is, custom matching rule. If you want to implement a custom matching rule by yourself, there are two steps:

1. Create a class inherited from the imatchingrule interface (this interface belongs to Microsoft. practices. unity. interceptionextension, which must reference unity. DLL), and the implementation method bool matches (system. reflection. methodbase member ).

2. Add the feature [configurationelementtype (typeof (custommatchingruledata)] to the class (you need to reference the namespace Microsoft. practices. enterpriselibrary. common. configuration and Microsoft. practices. enterpriselibrary. policyinjection. configuration mmatchingruledata, which can be identified by the enterprise database configuration tool.

The Code is as follows:

using Microsoft.Practices.Unity.InterceptionExtension;using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;namespace AOPAssembly{    [ConfigurationElementType(typeof(CustomMatchingRuleData))]    public class MyCustomMatchRule : IMatchingRule    {        NameValueCollection attributes = null;        public MyCustomMatchRule(NameValueCollection attributes)        {            this.attributes = attributes;        }        public bool Matches(System.Reflection.MethodBase member)        {            bool result = false;            if (this.attributes["matchname"] != null)            {                if (this.attributes["matchname"] == member.Name)                {                    result = true;                }            }            return result;        }    }}

The configuration diagram is as follows:

In this way, you can write a matching method in the matches method. I have written a simple match to match the method name.

This matching is performed by the method name configured in the configuration tool. If the configured method name and the called method name are the same, the matching is successful.Note that if you want to obtain the configuration defined in the configuration tool from your own custom matching class, you need to define a constructor to receive parameters of the namevaluecollection type, here, we define an attributes parameter in the class to receive the parameters configured in the configuration tool, and then process them in the matches method..

 

The above is all the content in this article. This article mainly introduces how to create and use custom feature matching rules and custom matching rules. If there are any mistakes, please note :)

 

Index of a series of articles on the learning path of Microsoft enterprise database 5.0:

Step 1: getting started

Step 2: Use the vs2010 + data access module to create a multi-database project

Step 3: Add exception handling to the project (record to the database using custom extension)

Step 4: Use the cache to improve the website's performance (entlib caching)

Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 1

Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 1

Step 5: Introduce the entlib. validation module information, the implementation level of the validators, and the use of various built-in validators-Part 2

Step 6: Use the validation module for server-side data verification

Step 7: Simple Analysis of the cryptographer encryption module, custom encryption interfaces, and usage-Part 1

Step 7: Simple Analysis of the cryptographer encryption module, custom encryption interfaces, and usage-Part 2

Step 8. Use the configuration setting module and other methods to classify and manage enterprise database configuration information

Step 9: Use the policyinjection module for AOP-PART1-basic usage

Step 9: Use the policyinjection module for AOP-PART2-custom matching rule

Step 9: Use the policyinjection module for AOP-PART3 -- Introduction to built-in call Handler

Step 9: Use the policyinjection module for AOP-PART4 -- create a custom call handler to achieve user operation Logging

Step 10: Use unity to decouple your system-Part1-Why use unity?

Step 10: Use unity to decouple your system-Part2-learn how to use Unity (1)

Step 10. Use unity to decouple your system-Part2-learn how to use Unity (2)

Step 10: Use unity to decouple your system-Part2-learn how to use Unity (3)

Step 10: Use unity to decouple your system-Part3-dependency Injection

Step 10: Use unity to decouple your system-part4 -- unity & piab

Extended learning:

Extended learning and dependency injection in libraries (rebuilding Microsoft Enterprise Library) [go]

 

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.