當每一個分層TPH一個表,很容易讓所有混凝土孩子的類,特別是如果BaseClass的是抽象的
abstract class BaseValue
{
public BaseValue()
: this(0, 0, string.Empty)
{
}
public BaseValue(int id, double value, string dimension)
{
Id = id;
TypeName = GetType().Name;
Dimension = dimension;
_value = value;
}
protected double _value;
public double RawValue
{
get { return _value; }
}
public int Id { get; protected set; }
public string TypeName { get; protected set; }
public string Dimension { get; set; }
}
abstract class BaseValue<T> : BaseValue
where T : struct
{
public BaseValue()
: this(0, default(T), string.Empty)
{ }
public BaseValue(int id, T value, string dimension)
: base(id, 0, dimension)
{
Value = value;
}
public T Value
{
get
{
if (typeof(T) == typeof(float))
return (dynamic)(float)_value;
else if (typeof(T) == typeof(bool))
return (dynamic)(bool)(_value > 0 ? true : false);
else if (typeof(T) == typeof(int))
return (dynamic)(int)_value;
else if (typeof(T) == typeof(TimeSpan))
return (dynamic)TimeSpan.FromSeconds(_value);
else if (typeof(T) == typeof(DateTime))
return (dynamic)new DateTime((long)_value);
else
return (dynamic)_value;
}
set
{
if (typeof(T) == typeof(float))
_value = (float)(object)value;
else if (typeof(T) == typeof(bool))
_value = (bool)(object)value ? 1 : 0;
else if (typeof(T) == typeof(int))
_value = (int)(object)value;
else if (typeof(T) == typeof(TimeSpan))
_value = ((TimeSpan)(object)value).TotalSeconds;
else if (typeof(T) == typeof(DateTime))
_value = ((DateTime)(object)value).Ticks;
else
_value = (double)(object)value;
}
}
}
class IntValue : BaseValue<int>
{
public IntValue()
: this(0, 0, string.Empty)
{ }
public IntValue(int id, int value, string dimension)
: base(id, value, dimension)
{ }
}
class BoolValue : BaseValue<bool>
{
public BoolValue()
: this(0, false, string.Empty)
{ }
public BoolValue(int id, bool value, string dimension)
: base(id, value, dimension)
{ }
}
class FloatValue : BaseValue<float>
{
public FloatValue()
: this(0, 0, string.Empty)
{ }
public FloatValue(int id, float value, string dimension)
: base(id, value, dimension)
{ }
}
class TimeSpanValue : BaseValue<TimeSpan>
{
public TimeSpanValue()
: this(0, TimeSpan.MinValue, string.Empty)
{ }
public TimeSpanValue(int id, TimeSpan value, string dimension)
: base(id, value, dimension)
{ }
}
class DateTimeValue : BaseValue<DateTime>
{
public DateTimeValue()
: this(0, DateTime.Now, string.Empty)
{ }
public DateTimeValue(int id, DateTime value, string dimension)
: base(id, value, dimension)
{ }
}
和庫從TPH表中選擇:
class Repository
{
public IEnumerable<BaseValue> GetAll()
{
IEnumerable<BaseValue> values = null;
using (SqlConnection cn = new SqlConnection(""))
{
cn.Open();
const string query = "SELECT Id, TypeName, RawValue, Dimension FROM Values";
values = cn.Query<BaseValue, object, BaseValue>(query,
(value, ignore) =>
{
BaseValue child = null;
if (value.TypeName == "IntValue")
child = new IntValue(value.Id, (int)value.RawValue, value.Dimension);
else if (value.TypeName == "BoolValue")
child = new BoolValue(value.Id, value.RawValue > 0, value.Dimension);
else if (value.TypeName == "FloatValue")
child = new FloatValue(value.Id, (float)value.RawValue, value.Dimension);
else if (value.TypeName == "TimeSpanValue")
child = new TimeSpanValue(value.Id, TimeSpan.FromSeconds(value.RawValue), value.Dimension);
else if (value.TypeName == "DateTimeValue")
child = new DateTimeValue(value.Id, new DateTime((long)value.RawValue), value.Dimension);
return child;
});
cn.Close();
}
return values;
}
}
這不是像http://stackoverflow.com/q/8344591/75642一樣的問題,只是它是關於Dapper的? –
正如我所說在我的上一個問題,我不得不從petapoco搬出,因爲這是petapoco不可能的。正如我在這裏所說的,我有一個解決方案與Dapper,但我正在尋找這個工具更好的一個 –