对于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
是传统的吗?
本文作者: Avdi Grimm和Sandi的什么都不是表示都没有提到任何空对象命名约定。
发布于 2015-10-11 08:21:19
到目前为止还没有主流公约。这很可能是因为空对象模式本身还没有被广泛使用。我遇到的唯一提到命名准则的是ThoughBot风格指南,它使用它作为命名规则的示例:
更喜欢在域概念之后命名类,而不是它们实现的模式(例如,Guest ,CachedRequest vs RequestDecorator)。
在您的例子中,这将是一个为“空博客”找到“域语言”的问题。找到这些术语的最佳方法是简单地与域用户谈论这个主题,并记下他们使用的任何名词。这种方法在“对象思维”一书中作了概述。
https://stackoverflow.com/questions/33062862
复制相似问题