当前位置: 首页 > 工具软件 > Smark.Data > 使用案例 >

Smark.Data 实体属性值描述

袁亦
2023-12-01
 

       在实际应用中很多时候当属性没有值的情况下,在插入数据库时都希望设置一个默认值。Smark.Data提供属性默认值描述功能,开发人员可以拿Smark.Data内置的对象来描述属性默认值或者根据自己的需要实现默认值描述对象。

定义基础描述类

    [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)

        {

        }

    }

描述类有两个方法就是数据添加之前和之后;组件会根据AfterByUpdate来确定执行那个方法。基础功能描述出来那扩展描述器就方便了。

    [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);

        }

    }

    [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(decimal 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 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);

        }

    }

除了简单的描述外,还可以通过操作数据库来进行值设置;我们用MSSQL是不是有递增值吗,那可以写个值描述器来设置新添加成员的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));

        }

    }

对于使用也比较简单方便:

    [Table]

    interface IUser

    {

        [ID]

        [UID]

        string UserID { get; set; }

        [Column]

        [NotNull("帐号名称不能为空!")]

        [Unique("相同帐号名称已经存!")]

        string UserName { get; set; }

        [Column]

        [PWDCrypto]

        string UserPWD { get; set; }

        [Column]

        [NotNull("邮件地址不能为空!")]

        [Unique("邮件地址已经被其他帐号所使用!")]

        string EMail { get; set; }

        [Column]

        [NowDate]

        DateTime CreateDate { get; set; }

        [Column]

        [EnumToString]

        [DefaultEnum]

        UserType Type { get; set; }

 

    }

 

 类似资料: