PetaPOCO 和超过 4 个连接 [英] PetaPOCO and more than 4 joins

查看:49
本文介绍了PetaPOCO 和超过 4 个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

petapoco 是否能够实现以下目标:1.无限连接在一个查询2.在一个查询中无限的一对多关系

Is petapoco capable of achieving the following : 1.Unlimited joins in one query 2.Unlimited One to Many relations in one query

我看过 PetaPOCO,它似乎不能进行 4 个以上的连接,最长的签名看起来像:db.Query

I have looked at PetaPOCO and it seems like it is not capable of doing more than 4 joins, the longest signature looks like : db.Query<T1, T2, T3 , T4>

它似乎也支持一对多关系,但仅适用于一个复合对象,如下所示:db.FetchOneToMany其中 T2 是 T1 的外键

Also seems like it supports a one to many relation , but only for one composite object such as below : db.FetchOneToMany<T1, T2> where T2 is a foreign key of T1

我正在测试一些微型 ORM,以坚持使用最好的.你知道它们中的任何一个可以处理这些情况吗,如果没有一个微 ORM 支持这个功能,你如何处理如下所示的对象:

I'm testing some of the micro ORMs out there to stick to the best one. Do you know of any of them that can handle these situations and if none of the micro ORMs are supporting this feauture, how do you deal with an object that is like the following :

class A
{
    List<B> member1;
    List<C> member2; 
    Z  member3; //Composit object
    Z1 member4; //Composit object
    Z2 member5; //Composit object
    Z3 member6; //Composit object
    Z4 member7; //Composit object
}

然后更复杂的是,如果成员 1(类型 B)在其内部有一些复合对象怎么办?如果我们有:

And then even more complicated is , what if member one (type B) has some composite object within itself ? What if we have :

class B
{
    G member0;
}

请不要提出多次访问数据库的解决方案,因为当对象变得有点复杂时,调用会太多.

Please don't propose a solution to hit database multiple times, coz it's going to be way too many calls when the objects become just a little bit complex.

哦,我也知道解决无限连接情况的另一种方法是创建一个非常扁平的对象,将所有字段组合在一起.这根本不是一个优雅的解决方案.

Oh and i also know that one other way of tackling the case of unlimited joins is creating a very flat object that hols all fields combined. It's not an elegant solution at all.

推荐答案

T1..T$ Query() 重载都传递到主 Query(..Type[]..) 方法.您可以自己添加更多 Query() 重载以处理更多 T 参数,或者在 Type 数组中传递您需要的所有类型(这是 T1-T4 函数所做的):

The T1..T$ Query() overloads all pass through to the main Query(..Type[]..) method. You can either add more Query() overloads yourself to handle more T parameters, or pass in all the types you need in a Type array (which is what the T1-T4 functions do) :

Query<TRet>( new Type[]{typeof(Poco1), typeof(Poco2), typeof(Poco3), typeof(Poco4), typeof(Poco5)}, null, sql, args);

您可以有多个一对多的关系,但 Schotime 是对的,您需要非常小心结果集中返回的大量重复数据.写sql查询,看结果集,重复量你能接受吗?如果是这样,那么在 Petapoco 中有一个相关回调的概念,您可以在其中编写一个小类来处理单个结果行中的不同 poco,并将每个 poco 添加到父 poco 上的列表属性中.

You can have multiple one to many relationships but Schotime is right, you need to be very careful of swathes of duplicate data coming back in your result set. Write the sql query and look at the result set, is the amount of duplication acceptable to you? If so then in Petapoco there is a concept of relator callbacks where you write a small class that handles the different pocos in a single result row and add each poco to the list properties on the parent poco.

http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships

我从来没有用多个一对多来做这件事,但引用了上面的内容

I've never had to do this with multiple one to many but quoted from the above

如果你加入了两个以上的桌子,你需要更多的东西复杂,但实际上只是上述内容的扩展."

"If you're joining more than two tables you'll need something more complex but it's really just extensions of the above."

另一种选择是拥有一个存储过程,它可以在单个数据库请求中完成所有工作并返回多个结果集,我相信 Schotime 在他的 petapoco 分支中已经实现了,但我自己还没有使用过它,所以我无法真正评论它是否会在这里有所帮助:

Another option is to have a stored procedure that does all the work in a single database request and have it return multiple result sets which I believe Schotime has achieved in his branch of petapoco but I've not used it myself yet so I can't really comment on if it will help here :

http://schotime.net/blog/index.php/2011/11/20/petapoco-multiple-result-sets/

如果我绝对必须将所有数据一次性连接到像您建议的那样复杂和嵌套的对象,那么我将使用存储过程(单个数据库调用)并将其全部缝合连同代码.只有这样我才能弄清楚如何在 Petapoco 中做到这一点.但是,如果您的 UI 在用户单击扩展器按钮(或类似按钮)之前不显示所有嵌套数据,我会在此时使用 AJAX 调用,而不是最初获取所有数据.

If I absolutely had to wire up all the data in one go for objects as complex and nested as you are suggesting then I would use a stored procedure (a single db call) and stitch it all together with code. Only then would I figure out how to do this in Petapoco. However if your UI doesn't show all the nested data until the user clicks on an expander button (or similar) I'd use an AJAX call at that point rather than get all the data initially.

这篇关于PetaPOCO 和超过 4 个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆