. NET Framework-sample code that examines data uniqueness by applying attributes and reflection

Source: Internet
Author: User

Reflections and properties

The. NET Framework provides reflection and feature technology that can be used to check data repeatability to determine whether a piece of data is written to a database table.

Demand

When an entity writes a data to a database, many times, it is necessary to check that the data is not a duplicate data, such as the new person, assuming that the ID number can not be duplicated, the new person ID number and the person table of one or more duplicate, at this time need to give hints or take other means, such as update, or delete.

Method

In this scenario, you can use the features and reflection technologies provided by the. NET Framework to address such requirements. Specific process,
Second, in the entity class, reference the newly-written attribute class to construct its unique identity (one or more attribute combinations);
  Finally, when checking data repeatability, use the method provided by attribute to obtain a unique identity attribute (one or more) for each entity class.
  Attribute[] GetCustomAttributes(modeltype, inherit);

Keyfieldattribute Attribute Class

     public class Keyfieldattribute:attribute     {        private static list<string> keyfields = new list<string > ();        <summary>///Construction Key Properties///</summary>//        <param name= "Fields" ></param>        Public Keyfieldattribute (params string[] fields)        {            foreach (String kf, fields)            {                if (!keyfields. Contains (KF))                    Keyfields. ADD (KF);            }        }        public static list<string> keyfields        {            get {return keyfields;}        }    

Entity Class model

[Keyfield ("id")]public class person{public  int ID {get;set;}//Personnel ID public  string Name {get;set;} Person name Public  DateTime BirthDate {get;set;}//Date of birth}    [Keyfield ("Rolegroupid", "Rolecode")] public    class Role    {public        int rolegroupid {get; set}//Group ID public        string rolecode {get; set;}/Role number public        St Ring RoleName {get; set;} Role name    }

Note that the attribute extension class, here is the suffix attribute in Keyfieldattribute can be omitted, so Keyfield is shorthand, equivalent to Keyfieldattribute.

Use the attribute class:

            Keyfieldattribute.getcustomattributes (typeof (Person), true);            list<string> fields = Keyfieldattribute.keyfields; Gets the unique identity attribute ID of the person entity            Keyfieldattribute.getcustomattributes (typeof (Role), true);                        var fields = keyfieldattribute.keyfields;//role Entity unique attribute, 2 attribute combinations: Rolegroupid,rolecode

Using the key attribute combination returned by the attribute, query the data in the database, if can find at least one record, then follow certain logic processing; if not, you can write the new data directly. Description of the methods provided in the attribute class:

<summary>///retrieves an array of custom attributes that are applied to the members of the type. </summary>///<param name= "Modeltype" > type of custom attribute to search </param>///<param name= "Inherit" > Whether to search for the ancestors of the member </param>///<returns> the array of custom attributes </returns>attribute[] GetCustomAttributes (Modeltype, Inherit);
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.