继承和 Dapper.net

发布于 2024-12-19 13:37:04 字数 464 浏览 0 评论 0原文

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

我的问题:

假设我有这些类:

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

和这些表:

Foo 
- ID
- Name

Bar
- FooID
- FavoriteMoovie

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

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

  • 选择所有不是 Bars 的 Foos
  • ,然后选择所有 Bars

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

I decided to use Dapper.net because it seems to be doing only what I want : mapping, I don't need anything fancy , I'm just bored to handle the mapping between my datareader and my object.

My problem :

Let's say I have these class :

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

And these tables :

Foo 
- ID
- Name

Bar
- FooID
- FavoriteMoovie

So I'd like to know how I can select my Foo and Bars in the same query ?

My only idea so far is

  • SELECT all the Foos that are not Bars
  • then SELECT all the Bars

I can't use the overload of the method "Query" because there are just here for mapping relationships.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉南桥 2024-12-26 13:37:04

非常hacky,但这可能有效。

db.Query<Bar, object, Foo>("select * from Foo left join Bar on FooID = ID",
 (bar,ignore) => 
   bar.FavoriteMoovie == null ? bar : new Foo{ID = bar.ID, Name = bar.Name});

缺点是它创建了一些不需要的中间对象。

其他选项是简单地选择 Bar 对象,然后使用相同的技巧来过滤掉 Foos 或选择一个动态,然后转换为正确的类。

就我个人而言,我不会仅仅为了处理这个问题而查询两次,除非您允许 NULLFavoriteMoovie,在这种情况下您别无选择,只能使查询复杂化或选择两次。

Very hacky, but this probably works.

db.Query<Bar, object, Foo>("select * from Foo left join Bar on FooID = ID",
 (bar,ignore) => 
   bar.FavoriteMoovie == null ? bar : new Foo{ID = bar.ID, Name = bar.Name});

Disadvantage is that it creates a couple of intermediate objects that it could do without.

Other options are to simply select Bar objects and then use the same trick to filter out the Foos or select a dynamic and then convert to the correct class.

Personally I would not query twice just to handle this, unless you are allowing for NULL FavoriteMoovie, in which case you have no choice but to either complicate the query or select twice.

拥醉 2024-12-26 13:37:04

在每个层次结构 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;
    }
}

in the case when one table per hierarchy TPH, it is easy to get all concrete childs Class, especially if the BaseClass is abstract

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

and Repository select from TPH table:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文