zoukankan      html  css  js  c++  java
  • dapper 自定义数据库字段和代码中Model字段不一致时候的mapping方法

    namespace YourNamespace
    {
        /// <summary>
        /// Uses the Name value of the ColumnAttribute specified, otherwise maps as usual.
        /// </summary>
        /// <typeparam name="T">The type of the object that this mapper applies to.</typeparam>
        public class ColumnAttributeTypeMapper<T> : FallbackTypeMapper
        {
            public ColumnAttributeTypeMapper()
                : base(new SqlMapper.ITypeMap[]
                    {
                        new CustomPropertyTypeMap(
                           typeof(T),
                           (type, columnName) =>
                               type.GetProperties().FirstOrDefault(prop =>
                                   prop.GetCustomAttributes(false)
                                       .OfType<ColumnMappingAttribute>()
                                       .Any(attr => attr.Name == columnName)
                                   )
                           ),
                        new DefaultTypeMap(typeof(T))
                    })
            {
            }
        }
        
      [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
        public class ColumnMappingAttribute : Attribute
        {
            public string Name { get; set; }
        }
     
      public class FallbackTypeMapper : SqlMapper.ITypeMap
        {
            private readonly IEnumerable<SqlMapper.ITypeMap> _mappers;
     
            public FallbackTypeMapper(IEnumerable<SqlMapper.ITypeMap> mappers)
            {
                _mappers = mappers;
            }
     
     
            public ConstructorInfo FindConstructor(string[] names, Type[] types)
            {
                foreach (var mapper in _mappers)
                {
                    try
                    {
                        ConstructorInfo result = mapper.FindConstructor(names, types);
                        if (result != null)
                        {
                            return result;
                        }
                    }
                    catch (NotImplementedException)
                    {
                    }
                }
                return null;
            }
     
            public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
            {
                foreach (var mapper in _mappers)
                {
                    try
                    {
                        var result = mapper.GetConstructorParameter(constructor, columnName);
                        if (result != null)
                        {
                            return result;
                        }
                    }
                    catch (NotImplementedException)
                    {
                    }
                }
                return null;
            }
     
            public SqlMapper.IMemberMap GetMember(string columnName)
            {
                foreach (var mapper in _mappers)
                {
                    try
                    {
                        var result = mapper.GetMember(columnName);
                        if (result != null)
                        {
                            return result;
                        }
                    }
                    catch (NotImplementedException)
                    {
                    }
                }
                return null;
            }
        }
      
    }
    public static IEnumerable LoadWithCustomMapping()
    {
        using (var conn = OpenDBConnection())
        {
            Dapper.SqlMapper.SetTypeMap(typeof(MyArea), new ColumnAttributeTypeMapper());
            return conn.Query("SELECT TOP 10 CAID,CAName,En FROM CityArea");
        }
    }
  • 相关阅读:
    keras 与tensorflow 混合使用
    卷积神经网络入门
    Linux Shell
    使用Keras编写GAN的入门
    Wasserstein距离 和 Lipschitz连续
    keras与sklearn的结合使用
    keras与tensorboard结合使用
    【转】java 高并发 基础知识
    用python写map reduce程序
    【转】互联网时代的社会语言学:基于SNS的文本数据挖掘
  • 原文地址:https://www.cnblogs.com/starluck/p/4039183.html
Copyright © 2011-2022 走看看