继承和Dapper.net

时间:2011-12-05 09:48:17

标签: dapper

我决定使用Dapper.net,因为它似乎只做我想要的:映射,我不需要任何花哨的东西,我只是无聊处理我的datareader和我的对象之间的映射。

我的问题:

假设我有这些课程:

class Foo{
int ID;
string Name;
}
class Bar : Foo{
string FavoriteMoovie;
}

这些表格:

Foo 
- ID
- Name

Bar
- FooID
- FavoriteMoovie

所以我想知道如何在同一个查询中选择我的Foo和Bars?

到目前为止我唯一的想法是

  • 选择所有不是条形的Foo
  • 然后选择所有的条

我不能使用方法“Query”的重载,因为这里只有映射关系。

1 个答案:

答案 0 :(得分:1)

在每个层次结构TPH一个表的情况下,很容易得到所有具体的子类,特别是如果BaseClass是抽象的

 public DataAggragationResponse doAggregation(List<DataResponse> lst)
    {
        if (lst.Count == 0)
            return null;
        DataContainerResponse rd = new DataContainerResponse();

        //If I do it manually typing each prop by hand.
        rd.VIOL = lst.Sum(s => s.VIOL);


        //Automation!!!
        foreach (PropertyInfo propertyInfo in typeof(DataResponse).GetProperties())
        {

                rd.GetType().GetProperties().SetValue(lst.Sum(s => propertyInfo.Name[0]));

        }
    }

和Repository从TPH表中选择:

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)
    { }
}