Permalink
Browse files

Support for value converter attribute (#408)

* Column value converter attribute support

* Revert "Column value converter attribute support"

This reverts commit 7a5b050.

* Column value converter attribute support
  • Loading branch information...
tkouba authored and pleb committed Jul 23, 2017
1 parent a563ab1 commit 14b79192e1259501e7b89aa210fe50e29b9e00b4
@@ -33,6 +33,20 @@ public void GetToDbConverter_GivenProperty_ShouldBeNull()
func.ShouldBeNull();
}
[Fact]
public void GetFromDbConverter_GivenPropertyWithValueConverterAttribute_ShouldNotBeNull()
{
var func = _mapper.GetFromDbConverter(typeof(Order).GetProperty(nameof(Order.PO)), typeof(string));
func.ShouldNotBeNull();
}
[Fact]
public void GetToDbConverter_GivenPropertyWithValueConverterAttribute_ShouldNotBeNull()
{
var func = _mapper.GetToDbConverter(typeof(Order).GetProperty(nameof(Order.PO)));
func.ShouldNotBeNull();
}
[Fact]
public void GetFromDbConverter_GivenPropertyTypeAndInterceptSet_ShouldCallback()
{
@@ -209,12 +223,27 @@ public class EntityWithAttributes
public string ColumnTwo { get; set; }
}
public class EmptyValueConverterAttribute : ValueConverterAttribute
{
public override object ConvertFromDb(object value)
{
return value;
}
public override object ConvertToDb(object value)
{
return value;
}
}
public class Order
{
public long OrderId { get; set; }
public DateTime CreatedOn { get; set; }
[EmptyValueConverter]
public string PO { get; set; }
public decimal? Discount { get; set; }
@@ -0,0 +1,30 @@
// <copyright company="PetaPoco - CollaboratingPlatypus">
// Apache License, Version 2.0 https://github.com/CollaboratingPlatypus/PetaPoco/blob/master/LICENSE.txt
// </copyright>
// <author>PetaPoco - CollaboratingPlatypus</author>
// <date>2017/07/20</date>
using System;
namespace PetaPoco
{
/// <summary>
/// Represents an attribute which can decorate a Poco property conver value from database type to property type and conversely.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public abstract class ValueConverterAttribute : Attribute
{
/// <summary>
/// Function to convert property value to database type value.
/// </summary>
/// <param name="value">Property value</param>
/// <returns>Converted database value</returns>
public abstract object ConvertToDb(object value);
/// <summary>
/// Function to convert database value to property type value.
/// </summary>
/// <param name="value">Database value</param>
/// <returns>Converted property type value</returns>
public abstract object ConvertFromDb(object value);
}
}
@@ -155,6 +155,20 @@ public ConventionMapper()
return true;
};
FromDbConverter = (pi, t) =>
{
var valueConverter = pi.GetCustomAttributes(typeof(ValueConverterAttribute), true).FirstOrDefault() as ValueConverterAttribute;
if (valueConverter != null)
return valueConverter.ConvertFromDb;
return null;
};
ToDbConverter = (pi) =>
{
var valueConverter = pi.GetCustomAttributes(typeof(ValueConverterAttribute), true).FirstOrDefault() as ValueConverterAttribute;
if (valueConverter != null)
return valueConverter.ConvertToDb;
return null;
};
}
/// <summary>
View
@@ -50,6 +50,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\ColumnAttribute.cs" />
<Compile Include="Attributes\ValueConverterAttribute.cs" />
<Compile Include="Core\AnsiString.cs" />
<Compile Include="Core\ColumnInfo.cs" />
<Compile Include="Core\ConventionMapper.cs" />

0 comments on commit 14b7919

Please sign in to comment.