Skip to content

Commit

Permalink
Support for value converter attribute (#408)
Browse files Browse the repository at this point in the history
* 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 14b7919
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
29 changes: 29 additions & 0 deletions PetaPoco.Tests.Unit/Core/ConventionMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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; }
Expand Down
30 changes: 30 additions & 0 deletions PetaPoco/Attributes/ValueConverterAttribute.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions PetaPoco/Core/ConventionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions PetaPoco/PetaPoco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down

0 comments on commit 14b7919

Please sign in to comment.