在哪里放置对象映射(在 RestKIt 中)

发布于 2024-12-04 04:58:37 字数 354 浏览 0 评论 0原文

因为我不想劫持另一个线程,所以我提出了关于映射的问题。

首先阅读: 放置对象映射的最佳位置在哪里RestKit

我确信 Blake Waters 给出的答案可能非常正确,因为他是一个比我更聪明、更有经验的人,但对我来说,逻辑告诉我将映射放入每个模型中:如果你更改模型中的某些内容,只需滚动即可更改映射。

然后,在我的 AppDelegate 中,我将在每个模型中调用 initMappings (或任何您想要的名称)。

As I don't want to hijack another thread here comes my question about mappings.

First read: Where's the best place to put object mappings in RestKit

I'm sure that the answer Blake Waters gave will probable be very correct as he is a much smarter and more experienced guy than I am, but to me logic tells me to put the mapping in each model: if you change something in your model, you're just a scroll away to change your mappings.

In my AppDelegate I would then just call the initMappings (or whatever you want to call it) in each of my models.

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

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

发布评论

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

评论(3

压抑⊿情绪 2024-12-11 04:58:37

我也喜欢将映射与我的模型放在一起。我通过向每个模型添加一个类方法来实现这一点,这样我就可以随时随地获取映射。

+ (RKObjectMapping *)objectMapping 
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];

    [mapping mapKeyPath:@"Id"       toAttribute:@"id"];
    [mapping mapKeyPath:@"Subject"      toAttribute:@"subject"];
    [mapping mapKeyPath:@"Message"      toAttribute:@"message"];
    [mapping mapKeyPath:@"PostDate"     toAttribute:@"postDateStr"];
    [mapping mapKeyPath:@"StatusId"     toAttribute:@"statusId"];
    [mapping mapKeyPath:@"StatusDate"   toAttribute:@"statusDateStr"];

    mapping.setNilForMissingRelationships = YES;

    return mapping;
}

I'm also a fan of placing the mappings with my models. I do it by adding a class method to each model so I can get the mapping whenever/wherever I need it.

+ (RKObjectMapping *)objectMapping 
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];

    [mapping mapKeyPath:@"Id"       toAttribute:@"id"];
    [mapping mapKeyPath:@"Subject"      toAttribute:@"subject"];
    [mapping mapKeyPath:@"Message"      toAttribute:@"message"];
    [mapping mapKeyPath:@"PostDate"     toAttribute:@"postDateStr"];
    [mapping mapKeyPath:@"StatusId"     toAttribute:@"statusId"];
    [mapping mapKeyPath:@"StatusDate"   toAttribute:@"statusDateStr"];

    mapping.setNilForMissingRelationships = YES;

    return mapping;
}
世俗缘 2024-12-11 04:58:37

我选择了创建类别的路线并将其放在那里。我已经在我的应用程序委托类而不是映射提供程序上创建了它。

我认为在模型中使用它的问题就像在另一个线程中描述的那样,如果你有关系,你可能最终会得到循环引用。

I've chosen the route of creating a category and put it in there. I've created it on my app delegate class instead than the mapping provider though.

I think the problem with having it in the models is just like described in the other thread, if you have relations you might end up with circular references.

婴鹅 2024-12-11 04:58:37

我相信以你的方式思考是很自然的,它是有意义的,因为你对代码有更多的控制并且它更干净,但你必须非常小心,正如许多人所说,循环引用的问题可能是一个大问题麻烦。

该问题的解决方案是,当 A 实体引用 B,B 引用 A 时,那么在这两个实体之一中,您将不得不选择不直接映射该实体,否则将以循环结束。

当所有内容都在同一范围内时,定义循环引用变得不可能,因为您需要 B 的初始定义来添加它对 A 的引用,因此我刚才提到的解决方案是采用这种方法的自然方法。

这取决于您和您的应用程序想要使用哪种方法,哪种方法更适合您和您的团队采用。您必须在可能的错误和更干净的代码之间做出选择。

I believe that is very natural to think the way you do, it has sense as you have more control over the code and its cleaner, but you have to be very careful, as many have said, the problem with circular references can be a big trouble.

A solution on that issue is that when you have an A entity referring to B, and B to A, then in one of both entities you will have to choose not to map directly the entity or you will end in a loop.

When you have everything on the same scope, defining circular references becomes imposible because you need the initial definition of B to add it's reference on A, so the solution I just mentioned is the natural way of doing it on this approach.

It depends on you and your app which approach do you want to use, which is better for you and your team to adopt. You have to choose between posible errors vs cleaner code.

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