大家又在聊asp.net mvc 最佳实践。
现在还找不到一个好的sample 。
我心目中的mvc最佳实践就是
易测试。
易扩展, 面向接口。
易理解。
高性能。( 开发量先不要考虑)
Understanding Controllers
MVC controllers are responsible for responding to requests made against an ASP.NET MVC website. Each browser request is mapped to a particular controller. For example, imagine that you enter the following URL into the address bar of your browser:
http://localhost/
In this case, a controller named ProductController is invoked. The ProductController is responsible for generating the response to the browser request. For example, the controller might return a particular view back to the browser or the controller might redirect the user to another controller.
Understanding Controller Actions
A controller exposes controller actions. An action is a method on a controller that gets called when you enter a particular URL in your browser address bar. For example, imagine that you make a request for the following URL:
http://localhost/
In this case, the Index() method is called on the ProductController class. The Index() method is an example of a controller action.
A controller action must be a public method of a controller class. C# methods, by default, are private methods. Realize that any public method that you add to a controller class is exposed as a controller action automatically (You must be careful about this since a controller action can be invoked by anyone in the universe simply by typing the right URL into a browser address bar).
There are some additional requirements that must be satisfied by a controller action. A method used as a controller action cannot be overloaded. Furthermore, a controller action cannot be a static method. Other than that, you can use just about any method as a controller action.
Understanding Action Results
A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.
ASP.NET MVC framework supports several types of action results including:
- ViewResult – Represents HTML and markup.
- EmptyResult – Represents no result.
- RedirectResult – Represents a redirection to a new URL.
- JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
- JavaScriptResult – Represents a JavaScript script.
- ContentResult – Represents a text result.
- FileContentResult – Represents a downloadable file (with the binary content).
- FilePathResult – Represents a downloadable file (with a path).
- FileStreamResult – Represents a downloadable file (with a file stream).
Understanding Views
ASP.NET or Active Server Pages, ASP.NET MVC does not include anything that directly corresponds to a page. In an ASP.NET MVC application, there is not a page on disk that corresponds to the path in the URL that you type into the address bar of your browser. The closest thing to a page in an ASP.NET MVC application is something called a view.
ASP.NET MVC application, incoming browser requests are mapped to controller actions. A controller action might return a view. However, a controller action might perform some other type of action such as redirecting you to another controller action.
Listing 1 contains a simple controller named the HomeController. The HomeController exposes two controller actions named Index() and Details().
The ASP.NET MVC framework includes the following set of standard HTML Helpers (this is not a complete list):
- Html.ActionLink()
- Html.BeginForm()
- Html.CheckBox()
- Html.DropDownList()
- Html.EndForm()
- Html.Hidden()
- Html.ListBox()
- Html.Password()
- Html.RadioButton()
- Html.TextArea()
- Html.TextBox()
The Different Types of Filters
The ASP.NET MVC framework supports four different types of filters:
- Authorization filters – Implements the
IAuthorizationFilterattribute. - Action filters – Implements the
IActionFilterattribute. - Result filters – Implements the
IResultFilterattribute. - Exception filters – Implements the
IExceptionFilterattribute.
Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter.
Authorization filters are used to implement authentication and authorization for controller actions. For example, the Authorize filter is an example of an Authorization filter.
Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.
Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.
Each different type of filter is executed in a particular order. If you want to control the order in which filters of the same type are executed then you can set a filter's Order property.
Improving Performance with Output Caching (C#)
The goal of this tutorial is to explain how you can dramatically improve the performance of an ASP.NET MVC application by taking advantage of the output cache. The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked.
Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index. Normally, each and every time that a user invokes the controller action that returns the Index view, the set of database records must be retrieved from the database by executing a database query.
If, on the other hand, you take advantage of the output cache then you can avoid executing a database query every time any user invokes the same controller action. The view can be retrieved from the cache instead of being regenerated from the controller action. Caching enables you to avoid performing redundant work on the server.
Where Content is Cached
By default, when you use the [OutputCache] attribute, content is cached in three locations: the web server, any proxy servers, and the web browser. You can control exactly where content is cached by modifying the Location property of the [OutputCache] attribute.
You can set the Location property to any one of the following values:
· Any
· Client
· Downstream
· Server
· None
· ServerAndClient
这周算是抽空把mvc 的基本内容都看了一遍, 基本了解asp.net mvc的架构和应用。
后面还是稍微实践一下。 抽空做一个小应用。
最佳实践呀 。
行版本控制框架在 Microsoft SQL Server 中始终处于启用状态,并被多个功能使用。它除了提供基于行版本控制的隔离级别之外,还用于支持对触发器和多个活动结果集 (MARS) 会话的修改,以及 ONLINE 索引操作的数据读取。
基于行版本控制的隔离级别是在数据库级别上启用的。访问已启用数据库的对象的任何应用程序可以使用以下隔离级别运行查询:已提交读隔离级别,通过将 READ_COMMITTED_SNAPSHOT 数据库选项设置为 ON 来使用行版本控制,如下面的代码示例所示:
ALTER DATABASE AdventureWorks
SET READ_COMMITTED_SNAPSHOT ON;
为 READ_COMMITTED_SNAPSHOT 启用数据库后,在已提交读隔离级别下运行的所有查询将使用行版本控制,这意味着读取操作不会阻止更新操作。
覆盖列概览(Introduction to Covering Columns )
对于堆或聚集索引的一张表,也叫"基表",包含(覆盖)了表的所有列,换句话说,非聚集索引只包含(覆盖)了表中的一小部分列,通过在非聚集索引来限制列的集合,SQL Server可以在每一页储存更多的行,这显然节约了空间,提高查找与扫描的效率,降低了I/O的操作数和页面数.然而,对于索引的扫描或查找来说,它只能返回该索引覆盖的那些列的记录行.
当创建覆盖列时,可以在非聚集索引上指定这些键列.如果基表是含有聚集索引的,位于该表上的每一个非聚集索引将覆盖聚集索引键,而不关心它们是否是非聚集索引键列的成员.在SQL Server 2005中,我们可以在非聚集索引上使用Create INDEX ...INCLUDE子语来添加额外的键列,注意:和索引键不同,INCLUDE中的列的次序并不重要.
下面我来通过一个示例说明,首先我们创建以下架构和对象:
Create TABLE T_heap (a int, b int, c int, d int, e int, f int)
Create INDEX T_heap_a ON T_heap (a)Create INDEX T_heap_bc ON T_heap (b, c)Create INDEX T_heap_d ON T_heap (d) INCLUDE (e)Create UNIQUE INDEX T_heap_f ON T_heap (f)Create TABLE T_clu (a int, b int, c int, d int, e int, f int)Create UNIQUE CLUSTERED INDEX T_clu_a ON T_clu (a)Create INDEX T_clu_b ON T_clu (b)Create INDEX T_clu_ac ON T_clu (a, c)Create INDEX T_clu_d ON T_clu (d) INCLUDE (e)Create UNIQUE INDEX T_clu_f ON T_clu (f)下面列举上面每一个索引的键列和覆盖列.
| 索引名称 | 键列 | 覆盖列 |
| T_heap_a | a | a |
| T_heap_bc | b,c | b,c |
| T_heap_d | d | d,e |
| T_heap_f | f | f |
| T_clu_a | a | a,b,c,d,e,f |
| T_clu_b | b,a | a,b |
| T_clu_ac | a,c | a,c |
| T_clu_d | d,a | a,d,e |
| T_clu_f | f | a,f |
对于创建的列在实际的索引查找和书签查询是有何不同呢?
下面我们来看一个例子:
Select e from t_clu where b = 2初看,这个查询看起来符合索引查找的候选,然而,该索引并不覆盖列e,因而索引的扫描或查找并不能返回e列的值,其解决方法是很简单的,对于从非聚集索引中获取的每一行,我们可以通过聚集索引来查询e列的值,这种方法称为"书签查询",书签查询是一个指向堆或聚集索引行的一个指针.在非聚集索引中存储每一行的书签,这样每次在进行非聚集索引查询时,总是由非聚集索引转向基表中相对应的行.
书签查询
在上面的例子中,我们了解了SQL Server如何使用索引查找来有效地获取满足谓词上的数据,然而,我们也知道非聚集索引并不覆盖表的所有列.试想一下,若我们有这样一个在非聚集索引键上的谓词查询:select查询的列并未被索引覆盖,当SQL Server在非聚集索引上查找时,将会丢失一些需要的列,与之相反,如果在聚集索引或唯上执行扫描时,将获取所有列,由于要扫描表的每一行,其操作显然不是很有效.以下的查询就是如此:
Select [OrderId], [CustomerId] FROM [Orders] Where [OrderDate] = '1998-02-26'上面的查询与先前我们进行索引查找的用到的查询一样,唯一不同的是,这里我们选择了两列:OrderID和CustomerID.非聚集索引OrderDate列仅覆盖OrderID列.
SQL Server具有处理这一问题的方法,对于从非聚集索引中获取的每一行,它可能查询包含在聚集索引中的剩余列(这里是CustomerID),我们称该操作为"书签查询".书签查询是一个指向堆或聚集索引行的指针.SQL Server在非聚集索引中存储每一行的书签,这样一来,可以从非聚集索引直接转向基表中相对应的记录行.
SQL Server 2000使用指定的迭代器来实现书签查询,通过文本计划可以看出索引查找与书签查询迭代器:
|--Bookmark Lookup(BOOKMARK
[Bmk1000]), OBJECT
[Orders])) |--Index Seek(OBJECT
[Orders].[OrderDate]), SEEK
[Orders].[OrderDate]=Convert([@1])) orDERED FORWARD)在SQL Server 2005中将使用嵌套循环联接和聚集索引查找来实现,仅当基表含有聚集索引或RID查询(基表是堆),SQL Server 2005中的查询计划与SQL Server 2000中的计划有些不同,但逻辑上是相同的.聚集索引查找就是通过LOOKUP关键来实现的一种书签查询或通过属性Lookup=”1”.以下是SQL Server 2005的图形查询计划和文本查询计划:|--Nested Loops(Inner Join, OUTER REFERENCES
[Orders].[OrderID])) |--Index Seek(OBJECT
[Orders].[OrderDate]), SEEK
[Orders].[OrderDate]='1998-02-26') orDERED FORWARD) |--Clustered Index Seek(OBJECT
[Orders].[PK_Orders]), SEEK
[Orders].[OrderID]=[Orders].[OrderID]) LOOKUP orDERED FORWARD)书签查询可以用在堆中,也可以用在聚集索引中,正如上所描述的,在SQL Server 2000中,堆上的书签查询与聚集索引上的书签查询是相同的,而在SQL Server 2005中,堆上的书签查询仍旧使用一个嵌套循环联接运算,代替了聚集索引查找,SQL Server使用一个叫做RID查询运算符.RID查询运算符包括在堆上进行书签查询的查找谓词,但是堆并不是一个索引,RID查询也不是一个索引查找.

