ASP.NET MVC以ValueProvider為核心的值提供系統 三

來源:互聯網
上載者:User

ValueProviderFactory

在ASP.NET Model繫結系統中,用於提供資料值的ValueProvider對象通過ValueProviderFactory來建立。在ASP.NET MVC應用編程介面中,ValueProviderFactory繼承自ValueProviderFactory類。本篇文章只要介紹基於ValueProviderFactory的ValueProvider的提供機制,以及如何通過自訂ValueProviderFactory實現我們需要的資料值的綁定方式。

一、ValueProviderFactory

如下面的代碼片斷所示,ValueProviderFactory是一個抽象類別,唯一的抽象方法GetValueProvider用於實現基於指定Controller內容相關的ValueProvider建立。

   1: public abstract class ValueProviderFactory
2: {
3: public abstract IValueProvider GetValueProvider(ControllerContext controllerContext);
4: }

下面的列表列出了定義在Model繫結系統中的6個原生的ValueProviderFactory:

ChildActionValueProviderFactory:根據給定的Controller上下文建立一個ChildActionValueProvider對象。

FormValueProviderFactory:根據給定的Controller上下文建立一個FormValueProvider對象。

JsonValueProviderFactory:將以JSON形式表示的請求資料轉換成一個Dictionary<string, object>對象,並最終建立一個DictionaryValueProvider<object>對象。

RouteDataValueProviderFactory:根據給定的Controller上下文建立一個RouteDataValueProvider對象。

QueryStringValueProviderFactory:根據給定的Controller上下文建立一個QueryStringValueProvider對象。

HttpFileCollectionValueProviderFactory:根據給定的Controller上下文建立一個HttpFileCollectionValueProvider對象。

二、ValueProviderFactory的註冊

ValueProviderFactory在ASP.NET MVC應用中的註冊通過靜態類型ValueProviderFactories實現。如下面的代碼片斷所示,ValueProviderFactories具有一個靜態唯讀屬性Factories返回一個表示ValueProviderFactory集合的ValueProviderFactoryCollection類型。

   1: public static class ValueProviderFactories
2: {
3: public static ValueProviderFactoryCollection Factories { get; }
4: }
5: 
6: public class ValueProviderFactoryCollection : Collection<ValueProviderFactory>
7: {
8: public ValueProviderFactoryCollection();
9: public ValueProviderFactoryCollection(IList<ValueProviderFactory> list);
10: public IValueProvider GetValueProvider(ControllerContext controllerContext);
11: }

ValueProviderFactoryCollection的GetValueProvider方法返回的是一個ValueProviderCollection對象,集合中的每個ValueProvider通過對應的ValueProviderFactory來建立。ValueProviderFactory在ValueProviderFactoryCollection集合中的先後次序決定了建立的ValueProvider在ValueProviderCollection中的次序,而次序決定了使用優先順序。

在預設的情況下ValueProviderFactories的Factories屬性工作表示的ValueProviderFactoryCollection包含了上面我們介紹的6種ValueProviderFactory,次序(優先順序)為:ChildActionValueProviderFactory、FormValueProviderFactory、JsonValueProviderFactory、RouteDataValueProviderFactory、QueryStringValueProviderFactory和。如果具有相同的名稱的請求書去同時存在於請求表單和查詢字串中,前者會被選用。

以ValueProvider為核心的值提供系統中涉及到了三類組件/類型,即用於具體實現資料值提供的ValueProvider,ValueProvider通過ValueProviderFactotry,而ValueProviderFactotry通過ValueProviderFactotries進行註冊。圖5-4所示的UML體現了三者之間的關係。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.