在哪里放置对象映射(在 RestKIt 中)
因为我不想劫持另一个线程,所以我提出了关于映射的问题。
首先阅读: 放置对象映射的最佳位置在哪里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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也喜欢将映射与我的模型放在一起。我通过向每个模型添加一个类方法来实现这一点,这样我就可以随时随地获取映射。
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.
我选择了创建类别的路线并将其放在那里。我已经在我的应用程序委托类而不是映射提供程序上创建了它。
我认为在模型中使用它的问题就像在另一个线程中描述的那样,如果你有关系,你可能最终会得到循环引用。
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.
我相信以你的方式思考是很自然的,它是有意义的,因为你对代码有更多的控制并且它更干净,但你必须非常小心,正如许多人所说,循环引用的问题可能是一个大问题麻烦。
该问题的解决方案是,当 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.