I have a Padrino project, that consists of multiple apps. For example:
- Website (Models: Site, Page)
- Blog (Models: Post, Comment)
- Shop (Models: Category, Product, Order)
- Tracking (Models: Visitor, Content)
Putting all models unmodified into one directory seems to me like a mess. So I thought of namespacing them like:
- Website (Models: Site, SitePage)
- Blog (Models: BlogPost, BlogComment)
- Shop (Models: ShopCategory, ShopProduct, ShopOrder)
- Tracking (Models: TrackingVisitor, TrackingContent)
But this looks very strange and produces a lot of extra typing.
What do you think? Is it good style to ignore namespacing and hope not to run into a naming conflict (e.g. "C开发者_JAVA技巧ategory" model for Blog app => Error) or should I prepend the apps name to each model?
Thanks in advance.
Cheers Marc
I use a module as namespace ie:
module BlogModels
class Category
end
end
and works quite well example with dm because I've namespaced table_name
, btw your way BlogCategory
is also fine to me.
I found a reasonable way to namespace the models in Mongoid and keep the overhead small.
I name the models like this: BlogPost, BlogComment, BlogCategory
And in the model I make use of class_name and inverse_of:
class BlogPost
include Mongoid::Document
# ... lots of stuff ommitted
has_many :comments, class_name: 'BlogComment', inverse_of: :post
end
class BlogComment
include Mongoid::Document
# ... lots of stuff ommitted
belongs_to :post, class_name: 'BlogPost', inverse_of: :comments
end
And access via:
post = BlogPost.first
post.comments.first # get comments
BlogComment.first.post # get related post
This keeps the access chain short and is better then:
post = BlogPost.first
post.blog_comments.first # get comments
BlogComment.first.blog_post # get related post
More details: http://mongoid.org/docs/relations.html
精彩评论