精巧的正确对象/聚合映射

发布于 2024-12-02 03:55:52 字数 656 浏览 0 评论 0原文

我最近开始评估 Dapper 作为 EF 的潜在替代品,因为我对生成的 SQL 不太满意并希望对其进行更多控制。我有一个关于在域模型中映射复杂对象的问题。假设我有一个名为 Provider 的对象,Provider 可以包含多个 IEnumerable 类型的属性,这些属性只能通过父提供程序对象(即聚合根)来访问。我看过类似的帖子,这些帖子使用 QueryMultiple 和 Map 扩展方法进行了解释,但我想知道如果 Dapper 能够一举完成此操作,我是否想编写一种方法来恢复急切加载的整个对象图或者是否需要零碎完成。举个例子,假设我的对象如下所示:

public AggregateRoot
      {
           public int Id {get;set;}
           ...//simple properties
           public IEnumerable<Foo> Foos
           public IEnumerable<Bar> Bars
           public IEnumerable<FooBar> FooBars
           public SomeOtherEntity Entity
           ...
      }

是否有一种使用 Dapper 填充整个对象图的直接方法?

I have recently started evaluating Dapper as a potential replacement for EF, since I was not too pleased with the SQL that was being generated and wanted more control over it. I have a question regarding mapping a complex object in my domain model. Let's say I have an object called Provider, Provider can contain several properties of type IEnumerable that should only be accessed by going through the parent provider object (i.e. aggregate root). I have seen similar posts that have explained using the QueryMultiple and a Map extension method but was wondering how if I wanted to write a method that would bring back the entire object graph eager loaded, if Dapper would be able to do this in one fell swoop or if it needed to be done piece-meal. As an example lets say that my object looked something like the following:

public AggregateRoot
      {
           public int Id {get;set;}
           ...//simple properties
           public IEnumerable<Foo> Foos
           public IEnumerable<Bar> Bars
           public IEnumerable<FooBar> FooBars
           public SomeOtherEntity Entity
           ...
      }

Is there a straightforward way of populating the entire object graph using Dapper?

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

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

发布评论

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

评论(1

我也有类似的情况。我使我的 sql 返回平坦,以便所有子对象都返回。然后我使用 Query<>绘制全套地图。我不确定你的套装有多大。

像这样:

var cnn =  sqlconnection();

var results = cnn.Query<AggregateRoot,Foo,Bars,FooBar,someOtherEntity,AggregateRoot>("sqlsomething"
                (ar,f,b,fb,soe)=>{
                    ar.Foo = f;
                    ar.Bars = b;
                    ar.FooBar = fb;
                    ar.someotherentity = soe;
                    return ar;

                },.....,spliton:"").FirstOrDefault();

所以 Query 标记中的最后一个对象是返回对象。对于 SplitOn,您必须将返回视为映射将运行的平面数组。您将为每个新对象选择第一个返回值,以便新映射从那里开始。

例如:

select ID,fooid, foo1,foo2,BarName,barsomething,foobarid foobaritem1,foobaritem2 from blah

spliton 将为“ID,fooid,BarName,foobarid”。当它运行返回集时,它将映射它可以在每个对象中找到的属性。

我希望这对您有所帮助,并且您的返回集不会太大而无法平坦返回。

I have a similar situation. I made my sql return flat, so that all the sub objects come back. Then I use the Query<> to map the full set. I'm not sure how big your sets are.

So something like this:

var cnn =  sqlconnection();

var results = cnn.Query<AggregateRoot,Foo,Bars,FooBar,someOtherEntity,AggregateRoot>("sqlsomething"
                (ar,f,b,fb,soe)=>{
                    ar.Foo = f;
                    ar.Bars = b;
                    ar.FooBar = fb;
                    ar.someotherentity = soe;
                    return ar;

                },.....,spliton:"").FirstOrDefault();

So the last object in the Query tag is the return object. For the SplitOn, you have to think of the return as a flat array that the mapping will run though. You would pick the first return value for each new object so that the new mapping would start there.

example:

select ID,fooid, foo1,foo2,BarName,barsomething,foobarid foobaritem1,foobaritem2 from blah

The spliton would be "ID,fooid,BarName,foobarid". As it ran over the return set, it will map the properties that it can find in each object.

I hope that this helps, and that your return set is not too big to return flat.

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