Ruby Padrino 中的多个应用程序:如何命名模型?

发布于 2024-12-04 07:14:35 字数 647 浏览 1 评论 0原文

我有一个 Padrino 项目,由多个应用程序组成。例如:

  • 网站(模型:站点、页面
  • 博客(模型:帖子、评论
  • 商店(模型:类别、产品、订单
  • 跟踪(模型:访问者、内容

在我看来,将所有未修改的模型放入一个目录似乎是一团糟。所以我想到将它们命名为:

  • 网站(模型:Site、SitePage
  • 博客(模型:BlogPost、BlogComment
  • 商店(模型:ShopCategory、ShopProduct、ShopOrder
  • 跟踪(模型:TrackingVisitor、TrackingContent

但这看起来很奇怪,并且会产生大量额外的输入。

你怎么认为?忽略命名空间并希望不要遇到命名冲突(例如博客应用程序的“类别”模型 => 错误)是一种好的风格,还是应该在每个模型前面添加应用程序名称?

提前致谢。

干杯马克

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. "Category" model for Blog app => Error) or should I prepend the apps name to each model?

Thanks in advance.

Cheers Marc

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

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

发布评论

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

评论(2

转身泪倾城 2024-12-11 07:14:36

我使用模块作为命名空间,即:

module BlogModels
  class Category
  end
end

并且与 dm 一起工作得很好,因为我已经命名了 table_name,顺便说一句,你的方式 BlogCategory 对我来说也很好。

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.

桜花祭 2024-12-11 07:14:36

我找到了一种合理的方法来命名 Mongoid 中的模型并保持较小的开销。

我这样命名模型:BlogPost、BlogComment、BlogCategory

在模型中,我使用 class_name 和 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

访问方式:

post = BlogPost.first
post.comments.first # get comments

BlogComment.first.post # get related post

这可以保持访问链较短并且更好:

post = BlogPost.first
post.blog_comments.first # get comments

BlogComment.first.blog_post # get related post

更多详细信息: http://mongoid.org/docs/relations.html

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

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