-
-
Save senjacob/8539127 to your computer and use it in GitHub Desktop.
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 static readonly string ColumnAttributeName = "ColumnAttribute"; | |
public ColumnAttributeTypeMapper() | |
: base(new SqlMapper.ITypeMap[] | |
{ | |
new CustomPropertyTypeMap(typeof (T), SelectProperty), | |
new DefaultTypeMap(typeof (T)) | |
}) | |
{ | |
} | |
private static PropertyInfo SelectProperty(Type type, string columnName) | |
{ | |
return | |
type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance). | |
FirstOrDefault( | |
prop => | |
prop.GetCustomAttributes(false) | |
// Search properties to find the one ColumnAttribute applied with Name property set as columnName to be Mapped | |
.Any(attr => attr.GetType().Name == ColumnAttributeName | |
&& | |
attr.GetType().GetProperties(BindingFlags.Public | | |
BindingFlags.NonPublic | | |
BindingFlags.Instance) | |
.Any( | |
f => | |
f.Name == "Name" && | |
f.GetValue(attr).ToString().ToLower() == columnName.ToLower())) | |
&& // Also ensure the property is not read-only | |
(prop.DeclaringType == type | |
? prop.GetSetMethod(true) | |
: prop.DeclaringType.GetProperty(prop.Name, | |
BindingFlags.Public | BindingFlags.NonPublic | | |
BindingFlags.Instance).GetSetMethod(true)) != null | |
); | |
} | |
} | |
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] | |
public class ColumnAttribute : Attribute | |
{ | |
public string Name { get; set; } | |
public ColumnAttribute() {} | |
public ColumnAttribute(string Name) { this.Name = Name; } | |
} | |
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; | |
} | |
} | |
} |
Hello, why don't write SelectProperties like this:
public static PropertyInfo SelectProperties(Type type, string colName)
{
return
type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault(
p =>
p.GetCustomAttributes(typeof(DbColumnAttribute), false)
.Any(attr => ((DbColumnAttribute)attr).ColumnName.ToLower() == colName.ToLower()));
}
Important.
Class FallbackTypeMapper will have an error => FindExplicitConstructor does not exist in this class.
You must implement this.
Follow the code
public ConstructorInfo FindExplicitConstructor()
{
foreach (var mapper in mappers)
{
try
{
ConstructorInfo result = mapper.FindExplicitConstructor();
if (result != null)
{
return result;
}
}
catch (NotImplementedException)
{
}
}
return null;
}
Perfect!!
Anyone want to tackle saving via Dapper.Contrib?
That would be awesome if the mapping worked there as well via Objects!
pls what is the minimum version of dapper supported?
Thanks for the mapper class!
Project builds but fails on runtime:
System.Data.SqlClient.SqlException : Invalid column name 'xyz...'
Please check the TestCustomTypeMap test samples for preferred way of doing custom typemapping with a DescriptionAttribute..
Anyone want to tackle saving via Dapper.Contrib?
That would be awesome if the mapping worked there as well via Objects!
Have you been able to?
Add database column name as ColumnAttribute's Name property and add the attribute to properties to be mapped to.
Then set the custom TypeMapper for your Model as:
For class libraries of .NET Framework >= v4.0, you can use
PreApplicationStartMethod
to register your classes for custom type mapping.Or you can find the classes to which ColumnAttribute is applied through reflection and set type mappings.