Rails 中空对象的命名约定?
Naming Convention for Null Objects in Rails?
对于 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
是常规的吗?
Sandi Metz 的 this article by Avdi Grimm nor the Nothing is Something 演示文稿均未提及任何空对象命名约定。
目前还没有主流公约。可能是因为 Null Object Pattern itself is not seeing widespread use yet. The only reference to a naming guideline that I have come across comes from the ThoughBot Style Guide,它使用它作为命名规则的示例:
Prefer naming classes after domain concepts rather than patterns they implement (e.g. Guest vs NullUser, CachedRequest vs RequestDecorator).
在您的情况下,找到 "null blog." 的 "domain language" 是我找到的查找这些术语的最佳方法是简单地与域用户讨论该主题,并记下他们使用的任何名词。 Object Thinking.
一书中概述了这种方法
对于 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
是常规的吗?
Sandi Metz 的 this article by Avdi Grimm nor the Nothing is Something 演示文稿均未提及任何空对象命名约定。
目前还没有主流公约。可能是因为 Null Object Pattern itself is not seeing widespread use yet. The only reference to a naming guideline that I have come across comes from the ThoughBot Style Guide,它使用它作为命名规则的示例:
Prefer naming classes after domain concepts rather than patterns they implement (e.g. Guest vs NullUser, CachedRequest vs RequestDecorator).
在您的情况下,找到 "null blog." 的 "domain language" 是我找到的查找这些术语的最佳方法是简单地与域用户讨论该主题,并记下他们使用的任何名词。 Object Thinking.
一书中概述了这种方法