The use of enum enum type in development--turn from the lookout

Source: Internet
Author: User

In the actual development, in the database table design, we tend to use an int type state field to represent the status of the data, this field is convenient to represent the state of the data, but do not want to build a foreign key table of the state field to explain the status. (There may be a lot of fields for this type of table state, just for example)

We typically use this state field as a convention to apply to the project (for example: 0: Enabled, 1: Disabled)

When the actual state of the int type is displayed in the background management or elsewhere, write a method in the public class, which uses a switch...case to return the corresponding Chinese explanation.


Http://www.dtan.so

But I am accustomed to using an enum enum to standardize the database to all the state fields, the use of enums, but also more conducive to development, can be enumerated comments, the Convention can be presented in front of the developer, rather than a direct contract. Share my use of the Enum class below.

1. First, we can create an entity class for the enumeration type: Readenum

public class readenum{Public      string Name {get; set;}      public int Value {get; set;}}

2. Second step, create the enumeration corresponding to the State field

#region # #状态枚举 (All state enumerations in the database)///<SUMMARY>///status enumeration (all States enumerated in database)//creator: porschev///created: 2011-7-19///</ summary>public enum ssnstate{    ///<summary>    //Enable///</summary> [Description ("Enabled") ]    Enabled = 0,    ///<summary>    ///disable///</summary> [Description ("Disabled")]    Disable = 1} #endregion

As with the enumeration created above, the developer generally does not use the red part of the Description property in the enumeration, which is under the System.ComponentModel namespace

With it, we can completely use the previous Switch...case method to paraphrase or display Chinese.

Step three: Write some application methods for all enums

       #region # #获得Enum类型description///<summary>//Get enum type description//created: 2011-7-19 </summary>///<param name= "enumtype" > Enum type </param>///<param name= "val" > enumeration value &L  t;/param>///<returns>string</returns> public static string Getenumdesc (Type enumtype, Object            val) {String enumvalue = System.Enum.GetName (Enumtype, Val); if (string.            IsNullOrEmpty (Enumvalue)) {return "";            } System.Reflection.FieldInfo finfo = Enumtype.getfield (enumvalue); object[] enumattr = Finfo.            GetCustomAttributes (typeof (System.ComponentModel.DescriptionAttribute), true); if (Enumattr.length > 0) {System.ComponentModel.DescriptionAttribute desc = enumattr[0] as S Ystem.                Componentmodel.descriptionattribute; if (desc! = null) {return desc. Description;        }} return enumvalue;         } #endregion #region # #获取某个枚举的全部信息///<summary>///Get all the information for an enumeration///created on: 2011-7-19 </summary>///<typeparam name= "T" > Enumeration </typeparam>///<returns> Enumeration of all information </  returns> public static list<readenum> getenumlist<t> () {list<readenum> List            = new List<readenum> ();            Readenum re = null;            Type type = typeof (T);                foreach (int enu in System.Enum.GetValues (typeof (T))) {re = new readenum (); Re.                Name = Getenumdesc (type, ENU); Re.                Value = ENU; List.            ADD (re);        } return list; } #endregion #region # #根据值返回枚举对应的内容///<summary>//Returns the contents of the enumeration based on value//creation time: 2011-7- ///</summary>///<typeparam name= "T" > Enumeration </typeparam>///<param name= "value" > Value (int) </param>///<returns></returns> PU Blic static T getmodel<t> (int value) {T MyEnum = (t) System.Enum.Parse (typeof (T), Valu            E.tostring (), true);        return myenum; } #endregion #region # #根据值返回枚举对应的内容///<summary>//Returns the contents of the enumeration based on value//creation time: 2011-7- ///</summary>///<typeparam name= "T" > Enum </typeparam>///<param name= "value"        ; Value (String) </param>///<returns></returns> public static T getmodel<t> (string value)            {T myenum = (t) System.Enum.Parse (typeof (T), value, true);        return myenum; } #endregion

These methods are perfectly sufficient for the use of enum enumerations in your project.

Fourth Step: Test code

            String str = Getenumdesc (typeof (Ssnstate), 0);            Result: Enable            list<readenum> List = getenumlist<ssnstate> ();            Result: List. count=2            //      First element: Name: Enabled; value:0            //      second element: Name: Disabled; Value:1            Ssnstate re = getmodel<ssnstate> (0);            Results: ssnstate.enabled            ssnstate Re1 = getmodel<ssnstate> ("0");            Results: ssnstate.enabled

The use of enum enum type in development--turn from the lookout

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.