你应该知道的States字段使用规范

2025-05-25 0 19

前言

最近在工作中了遇到了一些内容,觉着有必要和大家分享下,我们为了统一数据库表的状态字段,统一数据库表设计,简化字段在程序开发中的使用方式,下面话不多说了,来一起看看详细的介绍吧。

解决方式

States对应位域枚举StatesFlags

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// <summary>
 /// 数据状态枚举
 /// </summary>
 [Flags]
 [DataContract]
 [EnumDescription(\"状态\")]
 public enum StatesFlags
 {
  /// <summary>
  /// 可用状态
  /// </summary>
  [XmlEnum(\"1\")]
  [EnumDescription(\"可用\")]
  [EnumMember]
  Enabled = 1,
  /// <summary>
  /// 停用状态
  /// </summary>
  [XmlEnum(\"2\")]
  [EnumDescription(\"停用\")]
  [EnumMember]
  Disabled = 1 << 1,
  /// <summary>
  /// 移除(相当于逻辑删除)
  /// </summary>
  [XmlEnum(\"4\")]
  [EnumDescription(\"移除\")]
  [EnumMember]
  Removed = 1 << 2,
  /// <summary>
  /// 已确认(已经审核通过)
  /// </summary>
  [XmlEnum(\"8\")]
  [EnumDescription(\"已确认\")]
  [EnumMember]
  Confirmed = 1 << 3,
  /// <summary>
  /// 锁定
  /// </summary>
  [XmlEnum(\"16\")]
  [EnumDescription(\"锁定\")]
  [EnumMember]
  Locked = 1 << 4,
  /// <summary>
  /// 锁定登录
  /// </summary>
  [XmlEnum(\"32\")]
  [EnumDescription(\"锁定登录\")]
  [EnumMember]
  LockLogin = 1 << 5
 }

业务模型使用方式

在业务模型中,需要关注模型的特定状态集,写入新的状态时使用模型中的States, 读取时每一个状态独立提供读取实现。如下图中IsRemoved状态 以后大家一看代码就知道这个模型到底有几个状态

?
1
2
3
4
5
6
7
8
///<sumary>
  /// 状态集,写
  ///</sumary>
  public StatesFlags States { get; set; }
  /// <summary>
  /// 只读
  /// </summary>
  public bool IsRemoved => States.HasFlag(StatesFlags.Removed);

StatesFlags的4个扩展方法

你应该知道的States字段使用规范

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// <summary>
 /// 数据状态枚举
 /// </summary>
 public static class StatesFlagsExtends
 {
  /// <summary>
  /// 设置可用
  /// </summary>
  /// <param name=\"states\">状态</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>
  /// 设置停用
  /// </summary>
  /// <param name=\"states\">状态</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>
  /// 移除状态
  /// </summary>
  /// <param name=\"states\">状态</param>
  /// <param name=\"state\">要移除的状态</param>
  public static StatesFlags RemoveState(this StatesFlags states, StatesFlags state)
  {
   //也可以通过如下计算去除一个状态states = states & ~StatesFlags.Disabled;
   return states ^ state;
  }
  /// <summary>
  /// 附加状态
  /// </summary>
  /// <param name=\"states\">状态</param>
  /// <param name=\"state\">要附加的状态</param>
  public static StatesFlags AttachState(this StatesFlags states, StatesFlags state)
  {
   return states | state;
  }
 
 }

由于Enable和Disable是互斥的,所以对应有SetDisable、SetEnable 。其它非互斥状态 提供 AttachState、RemoveState用于附加或移除状态。 如出现新的状态在StatesFlags中添加,状态为位域枚举,使用连续的数字移位操作,增加代码可读性。

附扩展方式测试代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[TestClass]
 public class StatesFlagsTest
 {
  [TestMethod]
  public void TestStatesExtends()
  {
   //赋初值 在用、锁定、移除
   var state = StatesFlags.Enabled | StatesFlags.Locked | StatesFlags.Removed;
   //调用SetDisable方法,设为停用
   state = state.SetDisable();
   Assert.IsTrue(!state.HasFlag(StatesFlags.Enabled));
   Assert.IsTrue(state.HasFlag(StatesFlags.Disabled));
   //调用SetEnable方法,设为在用
   state = state.SetEnable();
   Assert.IsTrue(state.HasFlag(StatesFlags.Enabled));
   Assert.IsTrue(!state.HasFlag(StatesFlags.Disabled));
   //调用RemoveState方法,移除状态
   state = state.RemoveState(StatesFlags.Locked);
   Assert.IsTrue(!state.HasFlag(StatesFlags.Locked));
   Assert.IsTrue(state.HasFlag(StatesFlags.Removed));
   //调用AttachState方法,附加状态
   state = state.AttachState(StatesFlags.Confirmed);
   Assert.IsTrue(state.HasFlag(StatesFlags.Confirmed));
   //直接调用方法,不赋值不能改变states的值
   state.AttachState(StatesFlags.Locked);
   Assert.IsTrue(!state.HasFlag(StatesFlags.Locked));
 
  }
 }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 你应该知道的States字段使用规范 https://www.kuaiidc.com/51888.html

相关文章

发表评论
暂无评论