Last active
January 9, 2019 00:29
-
-
Save tkouba/1b7ff5a06c353b0bfdff3a17aee291a9 to your computer and use it in GitHub Desktop.
PetaPoco value converters using PetaPoco.ConventionMapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
namespace PetaPoco | |
{ | |
/// <summary> | |
/// Specifies a persistent property value converter. | |
/// </summary> | |
[AttributeUsage(AttributeTargets.Property)] | |
public abstract class ValueConverterAttribute : Attribute | |
{ | |
public abstract object ConvertToDb(object value); | |
public abstract object ConvertFromDb(object value); | |
} | |
/// <summary> | |
/// Converter for storing Color as INT32 in database | |
/// </summary> | |
public class ColorValueConverterAttribute : ValueConverterAttribute | |
{ | |
public override object ConvertToDb(object value) | |
{ | |
if (!(value is Color)) return null; | |
return ((Color)value).ToArgb() & 0x00FFFFFF; | |
} | |
public override object ConvertFromDb(object value) | |
{ | |
if (!(value is Int32)) return null; | |
return Color.FromArgb((Int32)value | /* 0xFF000000 */ -16777216); | |
} | |
} | |
/// <summary> | |
/// Converter for storing image as VARBINARY in database. | |
/// </summary> | |
public class ImageValueConverterAttribute : ValueConverterAttribute | |
{ | |
public override object ConvertToDb(object value) | |
{ | |
if (value == null || !(value is Image)) return null; | |
ImageConverter ic = new ImageConverter(); | |
return ic.ConvertTo(value, typeof(byte[])); | |
} | |
public override object ConvertFromDb(object value) | |
{ | |
if (value == null) return null; | |
ImageConverter ic = new ImageConverter(); | |
return ic.ConvertFrom(value); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of using ValueConverterAttributes | |
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
namespace PPTest | |
{ | |
[PetaPoco.ExplicitColumns] | |
class ItemReason | |
{ | |
[PetaPoco.Column] | |
public int ItemReasonID { get; set; } | |
[PetaPoco.Column] | |
public string Name { get; set; } | |
[PetaPoco.Column] | |
[PetaPoco.ColorValueConverter] | |
public Color Color { get; set; } | |
[PetaPoco.Column] | |
[PetaPoco.ImageValueConverter] | |
public Image Icon { get; set; } | |
public static List<ItemReason> Load() | |
{ | |
using (PetaPoco.Database db = new PetaPoco.Database()) | |
{ | |
// Modify PetaPoco.ConventionMapper for using ValueConverterAttribute (use IoC container in real project) | |
((PetaPoco.ConventionMapper)db.DefaultMapper).FromDbConverter = (pi, t) => | |
{ | |
var valueConverter = pi.GetCustomAttributes(typeof(ValueConverterAttribute), true).FirstOrDefault() as ValueConverterAttribute; | |
if (valueConverter != null) | |
return valueConverter.ConvertFromDb; | |
return null; | |
}; | |
((PetaPoco.ConventionMapper)db.DefaultMapper).ToDbConverter = (pi) => | |
{ | |
var valueConverter = pi.GetCustomAttributes(typeof(ValueConverterAttribute), true).FirstOrDefault() as ValueConverterAttribute; | |
if (valueConverter != null) | |
return valueConverter.ConvertToDb; | |
return null; | |
}; | |
return db.Fetch<ItemReason>(String.Empty); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment