The States Field Usage specification you should know, and the States Field Usage Specification
Preface
I recently encountered some things in my work. I think it is necessary to share with you that we want to unify the status fields of database tables, design database tables, and simplify the use of fields in program development, I won't talk much about it below. Let's take a look at the detailed introduction.
Solution
The corresponding bitfield of States enumerates StatesFlags.
/// <Summary> // data status enumeration // </summary> [Flags] [DataContract] [EnumDescription ("status")] public enum StatesFlags {// <summary> // available status // </summary> [XmlEnum ("1")] [EnumDescription ("available")] [EnumMember] Enabled = 1, /// <summary> // disabled status /// </summary> [XmlEnum ("2")] [EnumDescription ("Disabled")] [EnumMember] Disabled = 1 <1, /// <summary> // remove (equivalent to logical deletion) /// </summary> [XmlEnum ("4")] [EnumDescription ("Remove")] [EnumMember] Removed = 1 <2, /// <summary> /// confirmed (approved) /// </summary> [XmlEnum ("8")] [EnumDescription ("Confirmed")] [EnumMember] Confirmed = 1 <3, /// <summary> // lock // </summary> [XmlEnum ("16")] [EnumDescription ("Locked")] [EnumMember] Locked = 1 <4, /// <summary> /// lock logon /// </summary> [XmlEnum ("32")] [EnumDescription ("lock Logon")] [EnumMember] LockLogin = 1 <5}
Business Model usage
In the business model, you need to pay attention to the specific state set of the model. When writing data to a new state, you can use the States in the model. Each state can be read independently during reading. If the IsRemoved status is in the "IsRemoved" state, you can check the code to see whether the model has several statuses.
/// <Sumary> /// status set, write /// </sumary> public StatesFlags States {get; set ;} /// <summary> /// read-only /// </summary> public bool IsRemoved => States. hasFlag (StatesFlags. removed );
Four extension methods of StatesFlags
/// <Summary> /// data status enumeration /// </summary> public static class StatesFlagsExtends {// <summary> /// set availability /// </ summary> // <param name = "states"> Status </param> public static StatesFlags SetEnable (this StatesFlags states) {if (states. hasFlag (StatesFlags. disabled) states = states ^ StatesFlags. disabled; if (! States. hasFlag (StatesFlags. enabled) states = states | StatesFlags. enabled; return states ;} /// <summary> /// set disabled /// </summary> /// <param name = "states"> Status </param> public static StatesFlags SetDisable (this statesFlags states) {if (states. hasFlag (StatesFlags. enabled) states = states ^ StatesFlags. enabled; if (! States. hasFlag (StatesFlags. disabled) states = states | StatesFlags. disabled; return states ;} /// <summary> /// removal status /// </summary> /// <param name = "states"> Status </param> /// <param name = "state"> state to be removed </param> public static StatesFlags RemoveState (this StatesFlags states, statesFlags state) {// You can also use the following calculation to remove a state states = states &~ StatesFlags. disabled; return states ^ state ;} /// <summary> /// additional status /// </summary> /// <param name = "states"> Status </param> /// <param name = "state"> the state to be appended </param> public static StatesFlags AttachState (this StatesFlags states, statesFlags state) {return states | state ;}}
Because Enable and Disable are mutually exclusive, SetDisable and SetEnable should be applied. Other non-mutex States provide AttachState and RemoveState for attaching or removing States. If a new status is added in StatesFlags, the status is bit-field enumeration, and continuous number shift operations are used to increase code readability.
Additional extended test code
[TestClass] public class StatesFlagsTest {[TestMethod] public void TestStatesExtends () {// assign the initial value in use, lock, and remove var state = StatesFlags. enabled | StatesFlags. locked | StatesFlags. removed; // call the SetDisable method and set it to disabled state = state. setDisable (); Assert. isTrue (! State. hasFlag (StatesFlags. enabled); Assert. isTrue (state. hasFlag (StatesFlags. disabled); // call the SetEnable method and set it to state = state. setEnable (); Assert. isTrue (state. hasFlag (StatesFlags. enabled); Assert. isTrue (! State. HasFlag (StatesFlags. Disabled); // call the RemoveState method to remove the state = state. RemoveState (StatesFlags. Locked); Assert. IsTrue (! State. hasFlag (StatesFlags. locked); Assert. isTrue (state. hasFlag (StatesFlags. removed); // call the AttachState method, and the additional state is state. attachState (StatesFlags. confirmed); Assert. isTrue (state. hasFlag (StatesFlags. confirmed); // call the method directly. If no value is assigned, the states value state cannot be changed. attachState (StatesFlags. locked); Assert. isTrue (! State. HasFlag (StatesFlags. Locked ));}}
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.