這個問題涉及到使用Ruby on Rails 3與MongoMapper和EmbeddedDocument。此外,ManyAssociation。在設計MongoMapper模型時有沒有嵌入類的(代碼設計)原因?
http://mongomapper.com/documentation/embedded-document.html
的MongoMapper的例子(在上面的鏈接)顯示出兩個獨立的類:
class Order
include MongoMapper::Document
many :line_items
timestamps!
end
class LineItem
include MongoMapper::EmbeddedDocument
key :name, String
key :quantity, Integer
end
但這種污染與LineItem全局命名空間。出於上下文的考慮,LineItem是什麼?如果我想要另一個模型,比如WishList,還有一個LineItem集合?
因此,有可能嵌入LineItem類的秩序,就像這樣:
class Order
include MongoMapper::Document
many :line_items, :class_name => "Order::LineItem"
timestamps!
class LineItem
include MongoMapper::EmbeddedDocument
key :name, String
key :quantity, Integer
end
end
雖然這可能是技術上的罰款(是嗎?),稍後我會遇到設計問題?它是否使代碼太醜陋?太複雜了?
大概,這種Ruby語言的存在意味着有人認爲這是一個好主意?
我一直很喜歡Django的一件事是它如何使用「應用程序」來分組相關模型類(並分隔名稱空間)。所以我上面的代碼也在Rails中實現。