zoukankan      html  css  js  c++  java
  • Smark.Data 值转换器

            组件支持属性转换描述,主要用于对象属性值和数据存储之前的转换,如常见的枚举和数值,枚举和字符,用户密码等信息转换存储。实际上也可以扩展更灵活的转换如对象关联字段转换,对象序列化转等等。

            在Smark.Data描述一个转换器是一件很简单的事情,只需要继承PropertyCastAttribute对象并重写两个方法即可,以下是一个简单的枚举和数值的数值转换

        public class EnumToInt : PropertyCastAttribute
        {
            public override object ToColumn(object value, Type ptype, object source)
            {
                return (int)value;
            }
            public override object ToProperty(object value, Type ptype, object source)
            {
                return (Status)value;
            }
        }
        public enum Status : int
        {
            Default,
            Yes,
            No
        }

            也可以针对需来实现一个枚举和字符的转换

        public class EnumToString : PropertyCastAttribute
        {
            public override object ToColumn(object value, Type ptype, object source)
            {
                return value.ToString();
            }
            public override object ToProperty(object value, Type ptype, object source)
            {
                return Enum.Parse(ptype, (string)value);
            }
        }

            PropertyCastAttribute的两个方法分别是描述到字段是值和到属性的值处理描述,这样的话就可以做对应的转换工作;以下是实现一个基于BlowFish加解密的密码存储转换。

        [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Struct)]
        public class StringCrypto : PropertyCastAttribute
        {
            public static string BlowfishKey="sdfsdfsd1213lkjl";
       
            private BlowFishCS.BlowFish mBlowfish;
            public StringCrypto()
            {
                mBlowfish = new BlowFishCS.BlowFish(BlowfishKey);
            }
            public StringCrypto(string key)
            {
                mBlowfish = new BlowFishCS.BlowFish(key);
            }
    
            public override object ToProperty(object value, Type ptype, object source)
            {
    
                return DecryptString((string)value);
            }
    
            public override object ToColumn(object value, Type ptype, object source)
            {
    
                return EncryptString((string)value);
            }
            public string EncryptString(string value)
            {
                if (string.IsNullOrEmpty(value))
                    return value;
                return mBlowfish.Encrypt_CTR(value);
            }
            public string DecryptString(string value)
            {
                if (string.IsNullOrEmpty(value))
                    return value;
                return mBlowfish.Decrypt_CTR(value);
            }
        }

            转换器实现后就可以通过属性的方式描述到实体成员中使用

        [Table]
        public interface ITest 
        {
            [UID]
            [ID]
            String ID { get; set; }
            [EnumToInt]
            [Column("ENUM_INT")]
            Status IntValue { get; set; }
            [Column("ENUM_STRING")]
            [EnumToString]
            Status StringValue { get; set; }
            [StringCrypto]
            [Column]
            string PassWord { get; set; }
        }

            直接创建对象设置值就能保存

        Test test = new Test();
        test.IntValue = Status.Yes;
        test.StringValue = Status.No;
        test.PassWord = "123456";
        DBContext.Save(test);

            数据保存数据的结果

        在制定转换器后也会自动应用到查询条件上

        (Test.intValue == new Status[] {Status.Default,Status.No }).Delete<Test>();

        Smark.Data 开源数据访问组件 Apache License 2.0 (Apache)

    访问Beetlex的Github
  • 相关阅读:
    POJ2976 Dropping tests 01分数规划
    POJ 2728 Desert King 01分数规划,最优比率生成树
    codeforces 675E Trains and Statistic 线段树+贪心统计
    codeforces 675D Tree Construction set
    UVA 11235Frequent values(RMQ)
    FZU 2105Digits Count(线段树 + 成段更新)
    HDU 4006The kth great number(K大数 +小顶堆)
    UVA 624CD(01背包输出 + 输出路径)
    HDU 1796How many integers can you find(容斥原理)
    HDU 1817Necklace of Beads(置换+Polya计数)
  • 原文地址:https://www.cnblogs.com/smark/p/2660523.html
Copyright © 2011-2022 走看看