Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(三)Convention over Configuration

            约定优于配置

            为了简化和使网站开发更有成效,ASP.Net MVC尽可能依靠约定优于配置原则。这就是说,不是依赖确定的配置,ASP.NET MVC 假设当开发者开发应用程序的时候会遵循确定的约定。

            如下图是ASP.net MVC 的文件结构:


           上图是我们建立InernetApplicate模板时自动生成的,给我们提供了一个很好的关于约定的例子。对应MCV模式有三个相应的文件夹:Models、Views、Controllers。我们可以很清楚地看到这些文件包含的内容。我们同样可以看到一些约定在起作用。比如:不仅Controlller文件夹包含所有的应用程序的controller classes,而且每一个controller class都是以controler为后缀的。framework就是使用这样的约定,当应用程序启动的时候,注册这些controller,而且把它们与对应的路由(route)联系。

           接下来,我们看一下View文件夹,除了一个很明显的约定,就是程序的所有views都应该在这个文件下,我们还看到,它有一些子文件夹:Shard文件夹,还有其它可选文件夹用来包含对应controller的Views。这些约定帮助开发者为用户提供明确view的位置。还有,开发者只需要提供view的名称,比如说“Index”,framework会首先在指定的View文件夹的子文件夹下查找,如果没有找到,会在Shard文件夹下查找。

           乍一看,“约定优于配置”看上去很繁琐,但是,这些看似小和没有意义的优化可以对减少开发时间,提高代码的可阅读性和提高开发效率产生很重要的影响。


          英文原文:

          Convention over Configuration
To  make  website  development  easier  and  help  developers  be  more  productive,
ASP.NET MVC relies on the concept of convention over configuration whenever pos-
sible. This means that, instead of relying on explicit configuration settings, ASP.NET
MVC simply assumes that developers will follow certain conventions as they build their
applications.
The ASP.NET MVC project folder structure (Figure 1-4) is a great example of the
framework’s use of convention over configuration. There are three special folders in
the project that correspond to the elements of the MVC pattern: the Controllers,
Models, and Views folders. It’s pretty clear at a glance what each of these folders
contains.


When you look at the contents of these folders, you’ll find even more conventions at
work. For example, not only does the Controllers folder contain all of the application’s
controller classes, but the controller classes all follow the convention of ending their
names with the Controller suffix. The framework uses this convention to register the
application’s controllers when it starts up and associate controllers with their corre-
sponding routes.
Next, take a look at the Views folder. Beyond the obvious convention dictating that the
application’s views should live under this folder, it is split into subfolders: a Shared
folder, and an optional folder to contain the views for each controller. This convention
helps save developers from providing explicit locations of the views they’d like to dis-
play to users. Instead, developers can just provide the name of a view—say, “Index”—
and the framework will try its best to find the view within the Views folder, first in the
controller-specific folder and then, failing that, in the Shared views folder.


At first glance, the concept of convention over configuration may seem trivial. However,
these seemingly small or meaningless optimizations can really add up to significant
time savings, improved code readability, and increased developer productivity.



  本文翻译:Programming ASP.NET MVC一书,如有侵权,请与本人联系:(nuliliushuai@gmail.com)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值