added ConverterContext to marr Converters.

pull/4/head
kay.one 11 years ago committed by kayone
parent d1a4c7c942
commit 08e2d60f20

@ -20,14 +20,14 @@ namespace Marr.Data.Converters
{
public class BooleanIntConverter : IConverter
{
public object FromDB(ColumnMap map, object dbValue)
public object FromDB(ConverterContext context)
{
if (dbValue == DBNull.Value)
if (context.DbValue == DBNull.Value)
{
return DBNull.Value;
}
int val = (int)dbValue;
int val = (int)context.DbValue;
if (val == 1)
{
@ -40,7 +40,12 @@ namespace Marr.Data.Converters
throw new ConversionException(
string.Format(
"The BooleanCharConverter could not convert the value '{0}' to a boolean.",
dbValue));
context.DbValue));
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)

@ -20,14 +20,14 @@ namespace Marr.Data.Converters
{
public class BooleanYNConverter : IConverter
{
public object FromDB(ColumnMap map, object dbValue)
public object FromDB(ConverterContext context)
{
if (dbValue == DBNull.Value)
if (context.DbValue == DBNull.Value)
{
return DBNull.Value;
}
string val = dbValue.ToString();
string val = context.DbValue.ToString();
if (val == "Y")
{
@ -40,7 +40,12 @@ namespace Marr.Data.Converters
throw new ConversionException(
string.Format(
"The BooleanYNConverter could not convert the value '{0}' to a boolean.",
dbValue));
context.DbValue));
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext {ColumnMap = map, DbValue = dbValue});
}
public object ToDB(object clrValue)

@ -30,12 +30,17 @@ namespace Marr.Data.Converters
get { return typeof(TDb); }
}
public object FromDB(ColumnMap map, object dbValue)
public object FromDB(ConverterContext context)
{
TDb val = (TDb)dbValue;
TDb val = (TDb)context.DbValue;
return val.ToType(typeof(TClr), CultureInfo.InvariantCulture);
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)
{
TClr val = (TClr)clrValue;

@ -0,0 +1,13 @@
using System.Data;
using Marr.Data.Mapping;
namespace Marr.Data.Converters
{
public class ConverterContext
{
public ColumnMap ColumnMap { get; set; }
public object DbValue { get; set; }
public ColumnMapCollection MapCollection { get; set; }
public IDataRecord DataRecord { get; set; }
}
}

@ -20,11 +20,16 @@ namespace Marr.Data.Converters
{
public class EnumIntConverter : IConverter
{
public object FromDB(ColumnMap map, object dbValue)
public object FromDB(ConverterContext context)
{
if (dbValue == null || dbValue == DBNull.Value)
if (context.DbValue == null || context.DbValue == DBNull.Value)
return null;
return Enum.ToObject(map.FieldType, (int)dbValue);
return Enum.ToObject(context.ColumnMap.FieldType, (int)context.DbValue);
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)

@ -20,11 +20,16 @@ namespace Marr.Data.Converters
{
public class EnumStringConverter : IConverter
{
public object FromDB(ColumnMap map, object dbValue)
public object FromDB(ConverterContext context)
{
if (dbValue == null || dbValue == DBNull.Value)
if (context.DbValue == null || context.DbValue == DBNull.Value)
return null;
return Enum.Parse(map.FieldType, (string)dbValue);
return Enum.Parse(context.ColumnMap.FieldType, (string)context.DbValue);
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)

@ -20,6 +20,9 @@ namespace Marr.Data.Converters
{
public interface IConverter
{
object FromDB(ConverterContext context);
[Obsolete("use FromDB(ConverterContext context) instead")]
object FromDB(ColumnMap map, object dbValue);
object ToDB(object clrValue);
Type DbType { get; }

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Data.Common;
using Marr.Data.Converters;
namespace Marr.Data.Mapping
{
@ -53,7 +54,15 @@ namespace Marr.Data.Mapping
// Handle conversions
if (dataMap.Converter != null)
{
dbValue = dataMap.Converter.FromDB(dataMap, dbValue);
var convertContext = new ConverterContext
{
DbValue = dbValue,
ColumnMap = dataMap,
MapCollection = mappings,
DataRecord = reader
};
dbValue = dataMap.Converter.FromDB(convertContext);
}
if (dbValue != DBNull.Value && dbValue != null)

@ -52,6 +52,7 @@
<Compile Include="Converters\BooleanYNConverter.cs" />
<Compile Include="Converters\CastConverter.cs" />
<Compile Include="Converters\ConversionException.cs" />
<Compile Include="Converters\ConverterContext.cs" />
<Compile Include="Converters\EnumIntConverter.cs" />
<Compile Include="Converters\EnumStringConverter.cs" />
<Compile Include="Converters\IConverter.cs" />

@ -6,14 +6,14 @@ namespace NzbDrone.Core.Datastore.Converters
{
public class BooleanIntConverter : IConverter
{
public object FromDB(ColumnMap map, object dbValue)
public object FromDB(ConverterContext context)
{
if (dbValue == DBNull.Value)
if (context.DbValue == DBNull.Value)
{
return DBNull.Value;
}
var val = (Int64)dbValue;
var val = (Int64)context.DbValue;
switch (val)
{
@ -22,10 +22,15 @@ namespace NzbDrone.Core.Datastore.Converters
case 0:
return false;
default:
throw new ConversionException(string.Format("The BooleanCharConverter could not convert the value '{0}' to a Boolean.", dbValue));
throw new ConversionException(string.Format("The BooleanCharConverter could not convert the value '{0}' to a Boolean.", context.DbValue));
}
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)
{
var val = (Nullable<bool>)clrValue;

@ -5,24 +5,57 @@ using NzbDrone.Common.Serializer;
namespace NzbDrone.Core.Datastore.Converters
{
public class EmbeddedDocumentConverter : IConverter
public class ProviderSettingConverter : EmbeddedDocumentConverter
{
public override object FromDB(ConverterContext context)
{
if (context.DbValue == DBNull.Value)
{
return DBNull.Value;
}
public object FromDB(ColumnMap map, object dbValue)
var stringValue = (string)context.DbValue;
if (string.IsNullOrWhiteSpace(stringValue))
{
return null;
}
var ordinal = context.DataRecord.GetOrdinal("ConfigContract");
var implementation = context.DataRecord.GetString(ordinal);
var impType = Type.GetType(implementation, true, true);
return Json.Deserialize(stringValue, impType);
}
}
public class EmbeddedDocumentConverter : IConverter
{
public virtual object FromDB(ConverterContext context)
{
if (dbValue == DBNull.Value)
if (context.DbValue == DBNull.Value)
{
return DBNull.Value;
}
var stringValue = (string)dbValue;
var stringValue = (string)context.DbValue;
if (string.IsNullOrWhiteSpace(stringValue))
{
return null;
}
return Json.Deserialize(stringValue, map.FieldType);
return Json.Deserialize(stringValue, context.ColumnMap.FieldType);
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)

@ -14,16 +14,21 @@ namespace NzbDrone.Core.Datastore.Converters
}
}
public object FromDB(ColumnMap map, object dbValue)
public object FromDB(ConverterContext context)
{
if (dbValue != null && dbValue != DBNull.Value)
if (context.DbValue != null && context.DbValue != DBNull.Value)
{
return Enum.ToObject(map.FieldType, (Int64)dbValue);
return Enum.ToObject(context.ColumnMap.FieldType, (Int64)context.DbValue);
}
return null;
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)
{
if (clrValue != null)

@ -6,6 +6,21 @@ namespace NzbDrone.Core.Datastore.Converters
{
public class Int32Converter : IConverter
{
public object FromDB(ConverterContext context)
{
if (context.DbValue == DBNull.Value)
{
return DBNull.Value;
}
if (context.DbValue is Int32)
{
return context.DbValue;
}
return Convert.ToInt32(context.DbValue);
}
public object FromDB(ColumnMap map, object dbValue)
{
if (dbValue == DBNull.Value)

@ -7,18 +7,23 @@ namespace NzbDrone.Core.Datastore.Converters
{
public class QualityIntConverter : IConverter
{
public object FromDB(ColumnMap map, object dbValue)
public object FromDB(ConverterContext context)
{
if (dbValue == DBNull.Value)
if (context.DbValue == DBNull.Value)
{
return Quality.Unknown;
}
var val = Convert.ToInt32(dbValue);
var val = Convert.ToInt32(context.DbValue);
return (Quality)val;
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)
{
if(clrValue == null) return 0;

@ -6,9 +6,14 @@ namespace NzbDrone.Core.Datastore.Converters
{
public class UtcConverter : IConverter
{
public object FromDB(ConverterContext context)
{
return context.DbValue;
}
public object FromDB(ColumnMap map, object dbValue)
{
return dbValue;
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)

Loading…
Cancel
Save