对于 Rails 应用程序:是否存在用于命名映射到模型对象的空对象的命名约定?
例子:
#app/models/blog.rb
class Blog < ActiveRecord::Base
end
#app/models/null_blog.rb
class NullBlog # Null Objects are POROs, correct?
def title
"No title"
end
end
# so to implement it I can do this:
blogs = ids.map{|id| Blog.find_by(id: id) || NullBlog.new}
# which allows me to do this and it works, even if some of those ids did not find a blog
blogs.each{|blog| blog.title}
NullBlog
是否常规?
都不是this article by Avdi Grimm也不是 Nothing is Something Sandi Metz 的演讲提到了任何空对象命名约定。
最佳答案
目前还没有主流公约。可能是因为 Null Object Pattern本身还没有广泛使用。我遇到的对命名指南的唯一引用来自 ThoughBot Style Guide ,它使用它作为命名规则的示例:
Prefer naming classes after domain concepts rather than patterns they implement (e.g. Guest vs NullUser, CachedRequest vs RequestDecorator).
在您的情况下,这将是为“空博客”找到“域语言”的问题。我找到的查找这些术语的最佳方法是简单地与域用户讨论该主题,并记下他们使用的任何名词。 Object Thinking 一书中概述了这种方法.
关于ruby-on-rails - Rails 中空对象的命名约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33062862/