Smark.Data实体默认值描述

来源:互联网 发布:c语言面向对象编程 pdf 编辑:程序博客网 时间:2024/05/17 04:20

在数据设计的时候会针对一些字段设置默认的值,Smark.Data同样支持这样的功能,组件通过在属性中描述一个Value属性来告诉组件这个值如果在没有设置的情况应该提供怎样的值。组件提供一个描述的基类,用户可以根据实际情况的需要扩展出具体的默认值描述,以下是值描述基础类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[AttributeUsage(AttributeTargets.Property)]
public abstract class ValueAttribute:Attribute
{
    public ValueAttribute(bool afterupdate)
    {
        AfterByUpdate = afterupdate;  
    }
    public bool AfterByUpdate
    {
        get;
        set;
    }
    public virtual void Executing(IConnectinContext cc,object data,PropertyMapper pm,string table)
    {
    }
    public virtual void Executed(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
    }
}

基础类有两个方法,一个是描述在数据操作前要做的工作,一个是描述数据操作后需要完成的工作。一般设置默认值都在数据操作之前的,为什么还要定义一个操作后的呢。其实有些情况的确要在数据操作后做,如果MSSQL自增ID就是添加后把相关ID值设置到对象中,这个时候就需要在数据操作完成后进行:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[AttributeUsage(AttributeTargets.Property)]
public class IDENTITY : ValueAttribute
{
    public IDENTITY()
        :base(true)
    {
    }
    public override void Executed(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        Command cmd = new Command("select @@IDENTITY ");
        object value = cc.ExecuteScalar(cmd);
        pm.Handler.Set(data,Convert.ChangeType( value,pm.Handler.Property.PropertyType));
    }
}

如果想给一个属性设置一个默认的GUID值,用于主键可以这样做

?
1
2
3
4
5
6
7
8
9
10
[AttributeUsage(AttributeTargets.Property)]
public class UID:ValueAttribute
{
    public UID() : base(false) { }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        string uid = Guid.NewGuid().ToString("N");
        pm.Handler.Set(data, uid);
    }
}

可以通过以下定义引用UID属性

?
1
2
3
[ID]
[UID]
string EmployeeID { get;set; }

在这基础上可以实现很多需要的默认值,以下是组件内部实现的默认值描述类

?
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
[AttributeUsage(AttributeTargets.Property)]
public class YearMonth : ValueAttribute
{
    public YearMonth() : base(false) { }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data, DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00"));
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class Year : ValueAttribute
{
    public Year() : base(false) { }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data, DateTime.Now.Year.ToString());
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class Month : ValueAttribute
{
    public Month() : base(false) { }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data, DateTime.Now.Month.ToString());
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class Day : ValueAttribute
{
    public Day() : base(false) { }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data, DateTime.Now.Day.ToString());
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class NowDate:ValueAttribute
{
    public NowDate() : base(false) { }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data, DateTime.Now);
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class DefaultInt : ValueAttribute
{
    private int mValue = 0;
    public DefaultInt(int value) : base(false) {
        mValue = value;
    }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data, mValue);
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class DefaultDecimal : ValueAttribute
{
    private decimal mValue = 0;
    public DefaultDecimal(string value)
        :base(false)
    {
        mValue = Convert.ToDecimal(value);
    }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data,mValue);
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class DefaultString : ValueAttribute
{
    private string mValue = "";
    public DefaultString(string value)
        :base(false)
    {
        mValue = value;
    }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data, mValue);
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class DefaultDate:ValueAttribute
{
    private DateTime mValue = DateTime.MinValue;
    public DefaultDate(string value)
        :base(false)
    {
        mValue =DateTime.Parse(value);
    }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data, mValue);
    }
}
[AttributeUsage(AttributeTargets.Property)]
public class Enabled : ValueAttribute
{
    public Enabled() : base(false) { }
    public override void Executing(IConnectinContext cc, object data, PropertyMapper pm, string table)
    {
        pm.Handler.Set(data,true);
    }
}
专注于可靠、高性能的Socket TCP通讯组件