“约定优于配置”的优点范例

发布于 2024-10-05 16:18:28 字数 57 浏览 7 评论 0原文

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 技术交流群。

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

发布评论

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

评论(6

岁月染过的梦 2024-10-12 16:18:29

我认为好处很简单:无需配置。例如,您不需要为这种或那种类型的资源定义位置,以便应用程序/框架自行查找它们。

至于没有意义的情况:任何需要替代配置的情况相当频繁,或者开发人员/管理员需要明确“选择加入”某些行为(例如,以防止可能产生安全隐患的意外和意外副作用)。

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).

你对谁都笑 2024-10-12 16:18:29

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.

两人的回忆 2024-10-12 16:18:29

最明显的好处是您将需要编写更少的代码。让我们以 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).

甜`诱少女 2024-10-12 16:18:29

我认为这篇 wikipedia 文章已经解释得很好:

约定优于配置(也称为按约定编码)是
软件框架使用的软件设计范式尝试
减少开发人员使用的决策数量
框架需要在不失去灵活性的情况下建立。
这个概念是由 David Heinemeier Hansson 提出来描述
Ruby on Rails Web 框架的哲学,但与
早期的想法,例如“合理默认值”的概念和
用户界面设计中的最小惊讶原则。

该短语本质上意味着开发人员只需指定
应用程序的非常规方面。例如,如果有一个
模型中的类Sales,数据库中对应的表为
默认称为“销售”。只有当一个人偏离这一点时
惯例,比如“产品销售”表,需要写
有关这些名称的代码。

当工具实现的约定与所需的一致时
行为,它的行为符合预期,无需编写配置
文件。仅当期望的行为偏离实现的行为时
约定是需要显式配置。

I think this wikipedia article has explained it very well:

Convention over configuration (also known as coding by convention) is
a software design paradigm used by software frameworks that attempts
to decrease the number of decisions that a developer using the
framework is required to make without necessarily losing flexibility.
The concept was introduced by David Heinemeier Hansson to describe the
philosophy of the Ruby on Rails web framework, but is related to
earlier ideas like the concept of "sensible defaults" and the
principle of least astonishment in user interface design.

The phrase essentially means a developer only needs to specify
unconventional aspects of the application. For example, if there is a
class Sales in the model, the corresponding table in the database is
called "sales" by default. It is only if one deviates from this
convention, such as the table "product sales", that one needs to write
code regarding these names.

When the convention implemented by the tool matches the desired
behavior, it behaves as expected without having to write configuration
files. Only when the desired behavior deviates from the implemented
convention is explicit configuration required.

刘备忘录 2024-10-12 16:18:29

又甜又短!

这意味着程序员不必花费大量时间配置文件即可进行设置。

sweet and short!

That means that the programmer does not have to spend a lot of time configuring files in order to get setup.

一杯敬自由 2024-10-12 16:18:28

惯例规定,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.

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