In actual development, in database table design, we are often used to using an int type state field to represent the data status. This field is very convenient to represent the status of this data, however, it is unwilling to create a foreign key table for this state field to explain the state. (There may be many fields in the status of this type of table. Here is an example)
We generally treat this state field as a convention to apply it in the project (for example, 0: enabled, 1: disabled)
When the actual status of the int type is displayed in the background management or other places, write a method to the public class, and use a switch... case to return the corresponding Chinese explanation.
However, I am used to using an Enum enumeration to standardize all the State fields in the database. The use of Enum is also more conducive to development. You can separately comment on enumeration and the conventions can be presented to developers, instead of simply making an appointment out of thin air. The following describes how to use the enum class.
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. Step 2: Create an enumeration corresponding to the state field
# Region # state enumeration (all State enumeration in the database)
/// <Summary>
/// State enumeration (all State enumeration in the database)
/// Created by porschev
/// Creation Time: 2011-7-19
/// </Summary>
Public Enum ssnstate
{
/// <Summary>
/// Enable
/// </Summary>
[Description ("enable")]
Enabled = 0,
/// <Summary>
/// Disable
/// </Summary>
[Description ("disabled")]
Disable = 1
}
# Endregion
As shown in the enumeration created above, developers generally do not use the description attribute in red when using enumeration. It is in the namespace of system. componentmodel.
With this, we do not need to use the switch... Case Method to interpret or display Chinese.
Step 3: write some application methods for all Enum
# Region # obtain the enum type description
/// <Summary>
/// Obtain the enum type description
/// Created by porschev
/// Creation Time: 2011-7-19
/// </Summary>
/// <Param name = "enumtype"> Enumeration type </param>
/// <Param name = "Val"> enumeration value </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 system. componentmodel. descriptionattribute;
If (DESC! = NULL)
{
Return DESC. description;
}
}
Return enumvalue;
}
# Endregion
# Region # obtain all information about an enumeration
/// <Summary>
/// Obtain all information about an enumeration
/// Created by porschev
/// Creation Time: 2011-7-19
/// </Summary>
/// <Typeparam name = "T"> enumeration </typeparam>
/// <Returns> All enumeration 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 # Return the enumerated content based on the Value
/// <Summary>
/// Return the enumerated content based on the Value
/// Created by porschev
/// Creation Time: 2011-7-19
/// </Summary>
/// <Typeparam name = "T"> enumeration </typeparam>
/// <Param name = "value"> value (INT) </param>
/// <Returns> </returns>
Public static t GetModel <t> (INT value)
{
T myenum = (t) system. enum. parse (typeof (t), value. tostring (), true );
Return myenum;
}
# Endregion
# Region # Return the enumerated content based on the Value
/// <Summary>
/// Return the enumerated content based on the Value
/// Created by porschev
/// Creation Time: 2011-7-19
/// </Summary>
/// <Typeparam name = "T"> enumeration </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 can fully meet the needs of Enum enumeration in the project.
Step 4: Test code
String STR = getenumdesc (typeof (ssnstate), 0 );
// Result: Enabled
List <readenum> List = getenumlist <ssnstate> ();
// Result: List. Count = 2
// The first element: Name: enabled; Value: 0
// Second element: Name: Disabled; Value: 1
Ssnstate Re = GetModel <ssnstate> (0 );
// Result: ssnstate. Enabled
Ssnstate RE1 = GetModel <ssnstate> ("0 ");
// Result: ssnstate. Enabled
PS: What kind of performance and maintainability tends to be? It depends on the specific circumstances of the project !!