问题描述
我正在与Scala,Salat,Casbah,Mongo,Play2,BackboneJS进行一个项目...但是同时要学习很多新东西...我对Scala没问题,但是我发现我的代码糟糕透顶,我真的不知道有什么解决方案可以改善它.
I'm working on a project with Scala, Salat, Casbah, Mongo, Play2, BackboneJS... But it's quite a lot of new things to learn in the same time... I'm ok with Scala but I find my code crappy and I don't really know what's the solution to improve it.
基本上我的用例是:
- Play2将MongoDB对象发送到浏览器的JS代码
- JS代码(通过Backbone模型)更新对象数据
- JS将更新后的JSON发送回服务器(通过Backbone保存方法发送,并由Play使用json bodyparser接收)
- Play收到的JSON应该更新MongoDB中的对象
- 出于安全原因(对象ID,creationDate ...),某些字段不可更新
我的问题是最后一部分.我使用带有Salat的案例类来表示存储在MongoDB中的对象.
My problem is the last part.I'm using case classes with Salat as a representation of the objects stored in MongoDB.
我真的不知道如何处理从JS代码收到的JSON.
I don't really know how to handle the JSON i receive from the JS code.
-
我应该将JSON绑定到Salat案例类中,然后要求Mongo用完整的新case类对象覆盖以前的对象数据吗?如果是这样,Play2或Salat是否可以根据接收到的JSON自动创建案例类?
Should I bind the JSON into the Salat case class and then ask Mongo to override the previous object data by the full new case class object?If so is there a way with Play2 or Salat to automatically create back the case class from the received JSON?
我应该用$ set分别处理我要更新的字段的JSON字段吗?
Should I handle my JSON fields individually with $set for the fields I want to update?
我应该使案例类的元素可变吗?例如,这实际上是我们在Java中使用Hibernate进行的操作:从DB中获取对象,更改其状态并保存.但这似乎不是使用Scala的合适方法...
Should i make the elements of my case class mutable? It's what we actually do in Java with Hibernate for exemple: get the object from DB, change its state, and save it. But it doesn't seem to be the appropriate way to do with Scala...
如果有人可以给我一些关于我的用例的建议,那会很好,因为我真的不知道该怎么办:(
If someone can give me some advices for my usecase it would be nice because I really don't know what to do :(
我在这里提出了一个相关问题:我应该用不可变或可变的数据结构表示数据库数据吗?
I asked a related question here: Should I represent database data with immutable or mutable data structures?
推荐答案
Salat使用lift-json处理JSON-请参见 https://github.com/novus/salat/wiki/SalatWithPlay2 .
Salat handles JSON using lift-json - see https://github.com/novus/salat/wiki/SalatWithPlay2.
Play本身使用Jerkson,这是解码模型对象的另一种方法-请参见 http://blog.xebia.com/2012/07/22/play-body-parsing-with-jerkson/.
Play itself uses Jerkson, which is another way to decode your model objects - see http://blog.xebia.com/2012/07/22/play-body-parsing-with-jerkson/ for an example.
可以免费制作一个示例Github项目示例,以演示您的问题并发布到Salat邮件列表中,网址为 https://groups.google.com/group/scala-salat 以获得帮助.
Feel free to make a small sample Github project that demonstrates your issue and post to the Salat mailing list at https://groups.google.com/group/scala-salat for help.
这篇关于如何使用不变的Salat案例类更新Mongo中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!