“约定优于配置”的优点范例
Web 开发中“约定优于配置”范式有哪些好处?是否存在坚持下去没有意义的情况?
谢谢
What are the benefits of the "Convention over Configuration" paradigm in web development? And are there cases where sticking with it don't make sense?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为好处很简单:无需配置。例如,您不需要为这种或那种类型的资源定义位置,以便应用程序/框架自行查找它们。
至于没有意义的情况:任何需要替代配置的情况相当频繁,或者开发人员/管理员需要明确“选择加入”某些行为(例如,以防止可能产生安全隐患的意外和意外副作用)。
I think the benefit is simple: No configuration necessary. You don't need to define locations for this-or-that type of resource, for example, for the app/framework to find them itself.
As for cases where it does not make sense: any situation where it will be fairly frequent that alternative configurations would be required, or where it makes sense that a developer/admin would need to 'opt-in' to some behavior explicitly (for example, to prevent unintended and unexpected side-effects that could have security implications).
Web 开发中约定优于配置范例的好处是提高生产力,因为您不需要配置来设置所有规则,并且程序员需要做出的决定更少。使用 .NET Framework 时这一点很明显。
The benefit of convention over configuration paradigm in web development the productivity since you won't be required to configured to set all the rules and there are less decision that a programmer has to make. This is evident when using the .NET Framework.
最明显的好处是您将需要编写更少的代码。让我们以 Java Persistence API 为例。当您定义具有属性和相应的 setter/getter 的 POJO 时,它是一个简单的类。但是当你用@javax.persistence.Entity注释它时,它就变成了一个可以持久保存在数据库中的实体对象(表)。现在,这只需一个简单的注释即可实现,无需其他配置文件。
另一个优点是,您的所有逻辑都在一个地方并采用一种语言(即您摆脱了单独的 xml)。
The most obvious benefit is that you will have to write lesser code. Let's take case of Java Persistence API. When you define a POJO having attributes and corresponding setters/getters, it's a simple class. But the moment you annotate it with @javax.persistence.Entity it becomes an entity object (table) which can get persisted in DB. Now this was achieved by just a simple annotation, no other config file.
Another plus point is, all your logic is at one place and in one language (i.e. you get rid of separate xml).
我认为这篇 wikipedia 文章已经解释得很好:
I think this wikipedia article has explained it very well:
又甜又短!
这意味着程序员不必花费大量时间配置文件即可进行设置。
sweet and short!
That means that the programmer does not have to spend a lot of time configuring files in order to get setup.
惯例规定,90% 的情况下都会以某种方式进行。当您偏离该约定时,您可以进行更改...而不是强迫每个用户了解每个配置参数。我们的想法是,如果您需要它有所不同,您会在那个时间点搜索它,而不是在它通常没有实际价值时试图将所有配置参数集中起来。
恕我直言,这总是有道理的。使约定优先于显式配置是理想的。同样,如果有人有顾虑,他们会强迫自己调查需求。
Convention states that 90% of the time it will be a certain way. When you deviate from that convention then you can make changes...versus forcing each and every user to understand each and every configuration parameter. The idea is that if you need it to differ you will search it out at that point in time versus trying to wrap your head around all the configuration parameters when it often times has no real value.
IMHO it always makes sense. Making convention the priority over explicit configuration is ideal. Again if someone has a concern, they will force themselves to investigate the need.