在数据设计的时候会针对一些字段设置默认的值,Smark.Data同样支持这样的功能,组件通过在属性中描述一个Value属性来告诉组件这个值如果在没有设置的情况应该提供怎样的值。组件提供一个描述的基类,用户可以根据实际情况的需要扩展出具体的默认值描述,以下是值描述基础类:
[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值设置到对象中,这个时候就需要在数据操作完成后进行:
[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值,用于主键可以这样做
[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属性
[ID] [UID] string EmployeeID { get; set; }
在这基础上可以实现很多需要的默认值,以下是组件内部实现的默认值描述类
[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); } }