<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_NEE's Blog</title><subtitle type="text">微博PC遥控器--支持新浪和腾讯微博</subtitle><id>http://feed.cnblogs.com/blog/u/13486/rss</id><updated>2012-04-08T07:00:10Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><generator>feed.cnblogs.com</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/13486/rss"/><entry><id>http://www.cnblogs.com/NickYao/archive/2012/04/08/do_bdd_use_behat_and_mink.html</id><title type="text">(PHP)使用Behat和Mink对Web应用做BDD（行为测试驱动开发）</title><summary type="text">首先，按我自己的理解，简单说一下TDD 和 BDD 有何区别？首先是思路上的区别, 传统的 TDD 关注的是接口是否被正确地实现了, 所以通常每个接口有一个对应的单元测试类。而BDD是通过故事模板和场景，描述产品在用户操作时的具体功能表现，有点类似传统的Use Case。写BDD的测试时更像测试人员平常写的测试用例。由于BDD一般是使用自然语言来表达，所以就算不是开发人员也看得懂。以下就是一个BDD的例子：Behat是PHP下的一个BDD框架，可以配合PHPUnit来使用。更详细的介绍可以去behat的官方网站查看，http://behat.org/在对Web应用做自动化测试时，碰到界面测试时</summary><published>2012-04-07T16:48:00Z</published><updated>2012-04-07T16:48:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2012/04/08/do_bdd_use_behat_and_mink.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2012/04/08/do_bdd_use_behat_and_mink.html"/><content type="html">&lt;p&gt;首先，按我自己的理解，简单说一下TDD 和 BDD 有何区别？&lt;/p&gt;&lt;p class="p2"&gt;&lt;span class="s1"&gt;首先是思路上的区别, 传统的 TDD 关注的是接口是否被正确地实现了, 所以通常每个接口有一个对应的单元测试类。而&lt;/span&gt;BDD是通过故事模板和场景，描述产品在用户操作时的具体功能表现，有点类似传统的Use Case。写BDD的测试时更像测试人员平常写的测试用例。由于BDD一般是使用自然语言来表达，所以就算不是开发人员也看得懂。以下就是一个BDD的例子：&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/9932/2012040800404339.png" alt="" /&gt;&lt;/p&gt;&lt;p class="p2"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="p5"&gt;Behat是PHP下的一个BDD框架，可以配合PHPUnit来使用。&lt;/p&gt;&lt;p class="p5"&gt;更详细的介绍可以去behat的官方网站查看，&lt;a href="http://behat.org/"&gt;&lt;span class="s2"&gt;http://behat.org/&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p class="p4"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="p5"&gt;在对Web应用做自动化测试时，碰到界面测试时，无论是使用TDD和BDD都会遇到一些麻烦。&lt;/p&gt;&lt;p class="p5"&gt;通常有两种解决方案：&lt;/p&gt;&lt;p class="p5"&gt;1. 绕过GUI的测试。由于大部分的Web开发都会使用MVC框架，根据MVC的原理，Controller只需将正确的数据传递给View就算完成任务了。因此，我们在做测试时只需只需验证Controller是否把正确的数据传递给指定的View就可以了，至于View能否正确展现数据，那就要靠集成测试来验证了。&lt;/p&gt;&lt;p class="p5"&gt;2. 使用一些GUI测试框架或浏览器模拟器。今天我要讲的使用浏览器模拟器。&lt;/p&gt;&lt;p class="p5"&gt;浏览器模拟器大概可以分为两种：&lt;/p&gt;&lt;ul class="ul1"&gt;&lt;li class="li6"&gt;&lt;em&gt;Headless browser emulators&lt;/em&gt;&amp;nbsp;- browser emulators, that can be executed fully without GUI through console. Such emulators can do HTTP request and emulate browser applications on high level (HTTP stack), but on lower level (JS, CSS) they are totally limited. But they are much faster than real browsers, cuz you don&amp;rsquo;t need to parse CSS or execute JS in order to open pages or click links with them.&lt;/li&gt;&lt;ul class="ul2"&gt;&lt;li class="li6"&gt;简单说这种模拟器比真浏览器要快速，但无法处理CSS或JS。&lt;/li&gt;&lt;/ul&gt;&lt;li class="li6"&gt;&lt;em&gt;In-browser emulators&lt;/em&gt;&amp;nbsp;- this emulators works with real browsers, taking full controll of them and using them as zombies for their testing needs. This way, you&amp;rsquo;ll have standart fully-configured real browser, which you will be able to controll. CSS styling, JS and AJAX execution - all supported out of the box.&lt;/li&gt;&lt;ul class="ul2"&gt;&lt;li class="li6"&gt;这种模拟器更接近真实浏览器，可以处理CSS和JS，但是速度相对来说会比第一种慢。&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;p class="p7"&gt;但其实在真正做测试时，一般两种模拟器都需要用到。因为你不能全部都使用In-browser模拟器做测试，因为当测试比较多时，那速度是无法忍受的。但是当你需要测试类似Ajax请求时，你又不能使用Headless模拟器，必须使用In-browser模拟器。&lt;/p&gt;&lt;p class="p8"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="p7"&gt;由于这两种模拟器是很不同的，各自拥有不同的API，同时使用两套API来做测试是很麻烦的。&lt;/p&gt;&lt;p class="p7"&gt;这时MINK的优势就出来了，MINK是一个浏览器模拟器抽象层，用同一套API同时支持In-browser和Headless模拟器。&lt;/p&gt;&lt;p class="p8"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="p7"&gt;下面就介绍如何使用Behat和MINK对WEB应用做BDD。&lt;/p&gt;&lt;p class="p7"&gt;1. 安装Behat&lt;/p&gt;&lt;p class="p9"&gt;$ pear channel-discover &lt;a href="http://pear.symfony.com/"&gt;&lt;span class="s2"&gt;pear.symfony.com&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p class="p9"&gt;$ pear channel-discover &lt;a href="http://pear.behat.org/"&gt;&lt;span class="s2"&gt;pear.behat.org&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p class="p9"&gt;$ pear install behat/behat&lt;/p&gt;&lt;p class="p9"&gt;2. 安装MINK&lt;/p&gt;&lt;p class="p9"&gt;$ pear channel-discover &lt;a href="http://pear.behat.org/"&gt;&lt;span class="s2"&gt;pear.behat.org&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p class="p9"&gt;$ pear install behat/mink-beta&lt;/p&gt;&lt;p class="p9"&gt;3. 新建一个sosobaike目录，进入sosozhidao目录，执行behat --init命令&amp;nbsp; &amp;nbsp;&lt;/p&gt;&lt;p class="p9"&gt;$ mkdir sosobaike&lt;/p&gt;&lt;p class="p9"&gt;$ cd sosobaike&lt;/p&gt;&lt;p class="p9"&gt;$ behat --init&lt;/p&gt;&lt;p class="p12"&gt;&lt;span class="s3"&gt;这时behat会帮你创建一个features目录和&lt;/span&gt;&lt;em&gt;features/bootstrap/FeatureContext.php文件&lt;/em&gt;&lt;/p&gt;&lt;p class="p12"&gt;&lt;em&gt;FeatureContext.php是真正存放单元测试的方法的地方&lt;/em&gt;&lt;/p&gt;&lt;p class="p11"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="p13"&gt;4. 创建features/search.feature文件&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/9932/2012040800404339.png" alt="" /&gt;&lt;/p&gt;&lt;p class="p5"&gt;但如果我们没有使用MINK，直接运行behat命令时，behat会建议你在features/boostrap/FeatureContext.php加上一些测试方法，你需要自己在这些测试方法使用累似Selenium之类的脚本进行断言。&lt;/p&gt;&lt;p class="p4"&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/9932/2012040800424477.png" alt="" /&gt;&lt;/p&gt;&lt;p class="p14"&gt;&lt;span class="s4"&gt;5. 但如果你使用MINK的话，事情就简单多了，只需修改&lt;/span&gt;&lt;em&gt;features/bootstrap/FeatureContext.php文件，&lt;/em&gt;&lt;/p&gt;&lt;p class="p12"&gt;&lt;em&gt;加入两行代码。&lt;/em&gt;&lt;/p&gt;&lt;p class="p12"&gt;&lt;em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a) require_once 'mink/autoload.php';&lt;/em&gt;&lt;/p&gt;&lt;p class="p12"&gt;&lt;em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b) 将FeatureContext的父类由BehatContext修改为Behat\Mink\Behat\Context\MinkContext&lt;/em&gt;&lt;/p&gt;&lt;p class="p9"&gt;&lt;span class="s5"&gt;&lt;em&gt;6. 执行&lt;/em&gt;&lt;/span&gt;behat -dl命令，如果一切正常的会，你将会看到累似以下的结果&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/9932/2012040800452353.png" alt="" width="800" /&gt;&lt;/p&gt;&lt;p class="p5"&gt;以上其实就是MINK支持的一些API。&lt;/p&gt;&lt;p class="p5"&gt;7. &amp;nbsp;执行命令behat features/search.feature，你将会看到以下累似的结果&lt;/p&gt;&lt;p class="p4"&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/9932/2012040800444015.png" alt="" width="800" /&gt;&lt;/p&gt;&lt;p class="p5"&gt;到此，就算完成了一个BDD测试，整个过程做起来很简单，并不比写一个测试用例所耗费的时间长，并且获得测试用例所没有的功能 -- 可以自动执行去验证功能。&lt;/p&gt;&lt;p class="p4"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="p5"&gt;因此，个人觉得做测试的同学可以尝试使用此方法代替测试用例，可以节省不少时间。&lt;/p&gt;&lt;p class="p4"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="p5"&gt;对于开发的同学，我个人认为对Web应用做BDD或TDD时，应该使用绕过GUI的测试的方案，也就是只测试Controller的方案。&lt;/p&gt;&lt;p class="p5"&gt;原因有以下几点：&lt;/p&gt;&lt;p class="p5"&gt;1. 开发人员和测试人员对测试的运行速度要求是不一样的，测试人员对测试的运行速度没有太高的要求，他们可以把测试起起来，然后去做其他事情，一会回来看结果就好了。但是开发人员就不一样了，他们每修改一处代码都需要运行一次完整的测试，以确保自己的修改没有破坏其他功能。如果运行一次完整的测试很耗时间的话，那么就会有开发人员想做单元测试了。使用MINK虽然比在真正的浏览器里访问要快，但是由于始终是访问真正的产品代码，也就因为着很多测试会访问数据库，一旦测试很多的话，所有测试跑完是很耗费时间的。&lt;/p&gt;&lt;p class="p4"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="p5"&gt;2. 由于使用MINK是会访问真正的产品代码，那就意味者需要配置相关环境（依赖库，数据库等），甚至需要恢复数据库到初始状态。只要有任何一个配置环节出错，都有可能引起原来通过的单元测试会无法通过。这是无法达到一个好的单元测试的标准的。好的单元测试必须任何时候运行都必须保证运行结果是一致的。要达到这个效果，那就不能依赖外部条件（例如：数据库、网络），对于这些外部条件应该使用代码去尽量模拟。&lt;/p&gt;&lt;p class="p4"&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/2437205.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2012/04/08/do_bdd_use_behat_and_mink.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/07/08/2100597.html</id><title type="text">微博应用--PC遥控器 正式开源</title><summary type="text">首先简单介绍以下PC遥控器是个神马东西。你只需在被控电脑端安装上PC遥控器，设置好微博登录账号（最新版同时支持新浪和腾讯微博）。这样无论你在何处何地，只要发个微博，就可以对你的电脑进行关机、重启、关闭显示器、查看屏幕截图等操作！简单方便！非常实用！PC遥控器目前是靠我业余时间往里面添砖加瓦，但是一个人的力量和精力是有限的，为了让这款微博小应用的功能更加丰富和实用，我决定把PC遥控器开源，希望园里面...</summary><published>2011-07-07T18:02:00Z</published><updated>2011-07-07T18:02:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/07/08/2100597.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/07/08/2100597.html"/><content type="html">&lt;p&gt;首先简单介绍以下&lt;a target="_blank" href="httP://suchuanyi.sinaapp.com"&gt;PC遥控器&lt;/a&gt;是个神马东西。&lt;/p&gt;&lt;p&gt;你只需在被控电脑端安装上&lt;a target="_blank" href="httP://suchuanyi.sinaapp.com"&gt;&amp;nbsp;PC遥控器&lt;/a&gt;，设置好微博登录账号（最新版同时支持新浪和腾讯微博）。这样无论你在何处何地，只要发个微博，就可以对你的电脑进行关机、重启、关闭显示器、查看屏幕截图等操作！简单方便！非常实用！&lt;/p&gt;&lt;p&gt;&lt;a href="httP://suchuanyi.sinaapp.com" target="_blank"&gt;PC遥控器&lt;/a&gt;目前是靠我业余时间往里面添砖加瓦，但是一个人的力量和精力是有限的，为了让这款微博小应用的功能更加丰富和实用，我决定把&lt;a href="httP://suchuanyi.sinaapp.com" target="_blank"&gt;PC遥控器&lt;/a&gt;开源，希望园里面有朋友能为&lt;a href="httP://suchuanyi.sinaapp.com" target="_blank"&gt;PC遥控器&lt;/a&gt;贡献点代码。&lt;/p&gt;&lt;p&gt;&lt;a href="httP://suchuanyi.sinaapp.com" target="_blank"&gt;PC遥控器&lt;/a&gt;的官方博客：&lt;a href="http://suchuanyi.sinaapp.com/"&gt;http://suchuanyi.sinaapp.com/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;源码是托管在github上面，地址是：&lt;a href="https://github.com/terryso/PCRemote"&gt;https://github.com/terryso/PCRemote&lt;/a&gt;&lt;/p&gt;&lt;p&gt;对&lt;a target="_blank" href="httP://suchuanyi.sinaapp.com"&gt;PC遥控器&lt;/a&gt;有兴趣，想贡献代码的园友，你只需把你的源码提交到你自己的仓库，然后在github上面通知我去拉就可以了。&lt;/p&gt;&lt;p&gt;&lt;a target="_blank" href="httP://suchuanyi.sinaapp.com"&gt;PC遥控器&lt;/a&gt;的源码里面还包含了一个我自己写的微博SDK，目前支持新浪和腾讯微博的API，只不过目前只包含&lt;a target="_blank" href="httP://suchuanyi.sinaapp.com"&gt;PC遥控器&lt;/a&gt;用到的几个API而已。&lt;br /&gt;不过在这个SDK的基础上新增一个API也是很简单的，对这个SDK有兴趣的园友也可以看看，给些意见。&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/2100597.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/07/08/2100597.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/03/21/pcremote.html</id><title type="text">新浪微博小工具--PC遥控器1.0发布</title><summary type="text">PC遥控器是一款新浪微博的PC端小工具，有了这个小工具，你就可以随时随地通过发微博来控制你家里的电脑。 使用场景： 有时你要急着出门，但是你想下载的电影还没下载完成，你希望回来的时候可以马上观看。这时PC遥控器就发挥作用了，你出门前只需打开PC遥控器，设置好微博账号后就可以了。两个小时后，你只需发一条微博：屏幕截图$$看看我的电脑在干什么。一分钟后，PC遥控器就会把你家里电脑当前的屏幕截图发到你的微博上。这时，你看到电影已经下载完成，马上发一条微博：关机$$省电，这时PC遥控就会帮你关闭你的电脑了，既省电又环保。</summary><published>2011-03-21T03:18:00Z</published><updated>2011-03-21T03:18:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/03/21/pcremote.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/03/21/pcremote.html"/><content type="html">&lt;p&gt;PC遥控器是一款新浪微博的PC端小工具，有了这个小工具，你就可以随时随地通过发微博来控制你家里的电脑。&lt;/p&gt;&lt;p&gt;使用场景：&lt;/p&gt;&lt;p&gt;有时你要急着出门，但是你想下载的电影还没下载完成，你希望回来的时候可以马上观看。这时PC遥控器就发挥作用了，你出门前只需打开PC遥控器，设置好微博账号后就可以了。两个小时后，你只需发一条微博：屏幕截图$$看看我的电脑在干什么。一分钟后，PC遥控器就会把你家里电脑当前的屏幕截图发到你的微博上。这时，你看到电影已经下载完成，马上发一条微博：关机$$省电，这时PC遥控就会帮你关闭你的电脑了，既省电又环保。&lt;/p&gt;&lt;p&gt;安装须知：&lt;/p&gt;&lt;ol&gt;&lt;li&gt;WIN7直接安装就可以使用&lt;/li&gt;&lt;li&gt;XP需要安装.net framework 3.5&lt;/li&gt;&lt;li&gt;下载地址：&lt;a target="_blank" href="http://pcremote.sinaapp.com/publish.htm"&gt;http://pcremote.sinaapp.com/publish.htm&lt;/a&gt;&lt;/li&gt;&lt;li&gt;产品主要：&lt;a href="http://suchuanyi.sinaapp.com"&gt;http://suchuanyi.sinaapp.com&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;命令格式：命令 + $$ + 任意内容 （任意内容就是指随便写什么都可以）&lt;/p&gt;&lt;p&gt;例子：关机$$今天天气真是哈，哈，哈&lt;/p&gt;&lt;p&gt;目前支持的命令列表：&lt;/p&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;命令&lt;/th&gt;&lt;th&gt;作用&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;关机&lt;/td&gt;&lt;td&gt;默认5分钟后关闭计算机&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;注销&lt;/td&gt;&lt;td&gt;注销计算机&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;重启&lt;/td&gt;&lt;td&gt;重启计算机&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;关闭显示器&lt;/td&gt;&lt;td&gt;关闭显示器&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;减小音量&lt;/td&gt;&lt;td&gt;减小20%的音量&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;加大音量&lt;/td&gt;&lt;td&gt;加大20%的音量&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;静音&lt;/td&gt;&lt;td&gt;将计算机设置为静音&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;终止关机&lt;/td&gt;&lt;td&gt;取消关闭计算机&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;取消静音&lt;/td&gt;&lt;td&gt;取消静音&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;屏幕截图&lt;/td&gt;&lt;td&gt;把计算机当前的屏幕截图发到微博上&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/1990054.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/03/21/pcremote.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/01/25/1944837.html</id><title type="text">Brad Wilson写的 ASP.NET MVC 3 Service Location 系列文章索引</title><summary type="text">Brad Wilson在他自己的博客写了有关ASP.NET MVC3依赖注入支持的一系列文章，但由于某些原因（你懂的），需要翻墙才能阅读。为了方便自己也方便喜欢的同学，特意翻墙再拷贝回来发布，以下是这一系列文章的索引：Part 1: IntroductionPart 2: ControllersPart 3: View Engines/View PagesPart 4: FiltersPart 5: IDependencyResolverPart 6: Model ValidationPart 7: Model MetadataPart 8: Value ProvidersPart 9: Mo</summary><published>2011-01-25T10:22:00Z</published><updated>2011-01-25T10:22:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944837.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944837.html"/><content type="html">&lt;p&gt;Brad Wilson在他自己的博客写了有关ASP.NET MVC3依赖注入支持的一系列文章，但由于某些原因（你懂的），需要翻墙才能阅读。&lt;/p&gt;&lt;p&gt;为了方便自己也方便喜欢的同学，特意翻墙再拷贝回来发布，以下是这一系列文章的索引：&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944792.html"&gt;Part 1: Introduction&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944796.html"&gt;Part 2: Controllers&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944802.html"&gt;Part 3: View Engines/View Pages&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944805.html"&gt;Part 4: Filters&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944806.html"&gt;Part 5: IDependencyResolver&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944807.html"&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;Part 6: Model Validation&lt;/span&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944816.html"&gt;Part 7: Model Metadata&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944817.html"&gt;Part 8: Value Providers&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944819.html"&gt;Part 9: Model Binders&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944821.html"&gt;Part 10: Controller Activator&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span face="Arial" style="font-family: Arial;"&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944823.html"&gt;Part 11: View Page Activator&lt;/a&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/1944837.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944837.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/01/25/1944823.html</id><title type="text">【转】ASP.NET MVC 3 Service Location, Part 11: View Page Activator</title><summary type="text">View Page ActivatorIn ASP.NET MVC, it’s common for views to be compiled into classes. In MVC 1.0, we shipped theWebFormViewEngineas the default view engine, which allowed the user to write views using the familiar &lt;% %&gt; syntax from ASP.NET WebForms. The first time the view is rendered, the view file</summary><published>2011-01-25T09:59:00Z</published><updated>2011-01-25T09:59:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944823.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944823.html"/><content type="html">&lt;p&gt;&lt;span class="Apple-style-span" style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="text-align: left; font-family: 'Segoe UI', Arial, sans-serif; color: #333333; font-size: 14px;"&gt;&lt;div class="entry-body" style="line-height: 19px; clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;View Page Activator&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;In ASP.NET MVC, it&amp;rsquo;s common for views to be compiled into classes. In MVC 1.0, we shipped the&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMud2ViZm9ybXZpZXdlbmdpbmUuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;WebFormViewEngine&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;as the default view engine, which allowed the user to write views using the familiar &amp;lt;% %&amp;gt; syntax from ASP.NET WebForms. The first time the view is rendered, the view file goes through a code generation step, which is compiled into a class. The&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMud2ViZm9ybXZpZXcuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;WebFormView&lt;/a&gt;class is responsible for creating and executing the class that resulted from the view.&lt;/p&gt;&lt;p&gt;In ASP.NET MVC 3, we introduced a new Razor view engine. Like the WebForm view engine, the .cshtml and .vbhtml files from Razor go through a code generation step at run-time which compiles down into classes. The RazorView class is the Razor view engine counterpart to the WebFormView class.&lt;/p&gt;&lt;p&gt;We have introduced a new base class (BuildManagerCompiledView) and a new service (IViewPageActivator) which is responsible for instantiating instances of the view classes that were compiled from the view files. We have made the IViewPageActivator instance findable via the dependency resolver.&lt;/p&gt;&lt;/div&gt;&lt;a id="more" style="color: #56763a; text-decoration: underline;"&gt;&lt;/a&gt;&lt;div class="entry-more" style="clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9hYm91dC5odG1s&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;contact me&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;if you have comments.&lt;/em&gt;&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;New Base Class: BuildManagerCompiledView&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;When we introduced the new Razor view engine, it was clear that there was a lot of common code between the infrastructure classes of our two view engines; namely, the WebFormView and RazorView (which both implement the&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMuaXZpZXcuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;IView&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;interface) had significant shared code that was centered around BuildManager.&lt;/p&gt;&lt;p&gt;In the ASP.NET runtime, BuildManager is the class that is responsible for converting view files (like .aspx and .ascx files in WebForms, and .cshtml and .vbhtml files in Razor) into the underlying classes using code generation. BuildManager uses build providers to do most of that transformation work (which is outside the scope of this blog post). The important takeaway is that we extracted a common base class for the two view classes: BuildManagerCompiledView. The logic which creates view page class instances was centralized into this class, and the new IViewPageActivator service was introduced to allow pluggability of the creation of the view page classes.&lt;/p&gt;&lt;p&gt;Developers which are creating view engines which use build providers and BuildManager to convert view files into classes can take advantage of this new base class for their implementation(s) of the IView interface.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Implementing IViewPageActivator&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The new IViewPageActivator interface contains a single method for creating a view page instance:&lt;/p&gt;public interface IViewPageActivator {&lt;br/&gt;    object Create(ControllerContext controllerContext, Type type);&lt;br/&gt;}&lt;p&gt;Given the controller context and the view page class type, implementers of this interface must create the view page instance.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Location: IViewPageActivator&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is a &amp;ldquo;singly registered&amp;rdquo; style service introduced in MVC 3. There is no static registration point for this service as its purpose is strictly to support dependency injection; as such, the only way to register an instance of this service is through the dependency resolver.&lt;/p&gt;&lt;p&gt;The logic in BuildManagerCompiledView consults the dependency resolver, calling GetSerivce(typeof(IViewPageActivator)) and using the provided service when present. If there is no IViewPageActivator present in the dependency resolver, we will then ask the dependency resolver to create the concrete view page type by calling GetService(viewPageType). If the dependency resolver also fails to create the concrete view page type, we finally fall back to the MVC 2 behavior of using Activator.CreateInstance to create the view page type.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;What's Next?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is the end of the road for MVC 3 Beta. We believe we are now reasonably feature complete for MVC 3 and dependency resolution. If there are specific areas where you wish the framework would better enable dependency resolution, please don't hesitate to discuss them on the&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vZm9ydW1zLmFzcC5uZXQvMTE0Ni5hc3B4&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;MVC Forums&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Thanks for reading!&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/1944823.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944823.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/01/25/1944821.html</id><title type="text">【转】ASP.NET MVC 3 Service Location, Part 10: Controller Activator</title><summary type="text">Controller ActivatorIn MVC 1.0, we introducedIControllerFactoryto allow better dependency injection of controller instances. We also provided theDefaultControllerFactory, which created controller instances with Activator.CreateInstance. Some of this is discussed inPart 2of this series.We realized th</summary><published>2011-01-25T09:58:00Z</published><updated>2011-01-25T09:58:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944821.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944821.html"/><content type="html">&lt;p&gt;&lt;span class="Apple-style-span" style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="text-align: left; font-family: 'Segoe UI', Arial, sans-serif; color: #333333; font-size: 14px;"&gt;&lt;div class="entry-body" style="line-height: 19px; clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Controller Activator&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;In MVC 1.0, we introduced&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMuaWNvbnRyb2xsZXJmYWN0b3J5LmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;IControllerFactory&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;to allow better dependency injection of controller instances. We also provided the&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMuZGVmYXVsdGNvbnRyb2xsZXJmYWN0b3J5LmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;DefaultControllerFactory&lt;/a&gt;, which created controller instances with Activator.CreateInstance. Some of this is discussed in&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9ibG9nLzIwMTAvMDcvc2VydmljZS1sb2NhdGlvbi1wdDItY29udHJvbGxlcnMuaHRtbA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;Part 2&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;of this series.&lt;/p&gt;&lt;p&gt;We realized that DefaultControllerFactory was actually doing two things: turning the controller name into the controller type, and then instantiating the instance of that type. In ASP.NET MVC 3, we&amp;rsquo;ve split out the action of instantiating the controller instance into a new service: IControllerActivator. We have made the IControllerActivator instance findable via the dependency resolver.&lt;/p&gt;&lt;/div&gt;&lt;a id="more" style="color: #56763a; text-decoration: underline;"&gt;&lt;/a&gt;&lt;div class="entry-more" style="clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9hYm91dC5odG1s&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;contact me&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;if you have comments.&lt;/em&gt;&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Implementing IControllerActivator&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The new IControllerActivator signature is identical to the&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvZWUyNjQwNTIuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;DefaultControllerFactory.GetControllerInstance&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;virtual method:&lt;/p&gt;public interface IControllerActivator {&lt;br/&gt;    IController Create(RequestContext requestContext, Type controllerType);&lt;br/&gt;}&lt;p&gt;Developers who previously implemented IControllerFactory by deriving from DefaultControllerFactory just to override the GetControllerInstance method for dependency injection purposes should now implement IControllerActivator instead.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Location: IControllerActivator&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is a &amp;ldquo;singly registered&amp;rdquo; style service introduced in MVC 3. There is no static registration point for this service as its purpose is strictly to support dependency injection; as such, the only way to register an instance of this service is through the dependency resolver.&lt;/p&gt;&lt;p&gt;The logic in DefaultControllerFactory was updated to consult the dependency resolver, calling GetSerivce(typeof(IControllerActivator)) and using the provided service when present. If there is no IControllerActivator present in the dependency resolver, we will then ask the dependency resolver to create the concrete controller type by calling GetService(controllerType). If the dependency resolver also fails to create the concrete controller type, we finally fall back to the MVC 2 behavior of using Activator.CreateInstance to create the controller type.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;What&amp;rsquo;s Next?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;In the final post of this series, we will discuss the new&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9ibG9nLzIwMTAvMTAvc2VydmljZS1sb2NhdGlvbi1wdDExLXZpZXctcGFnZS1hY3RpdmF0b3IuaHRtbA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;View Page Activator&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;service.&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/1944821.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944821.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/01/25/1944817.html</id><title type="text">【转】ASP.NET MVC 3 Service Location, Part 8: Value Providers</title><summary type="text">Value ProvidersASP.NET MVC 2 introduced a new method to find value providers: theValueProviderFactoryclass. Value providers are used by the model binding system in MVC to populate the values of model objects. MVC includes value providers for several common value sources (including query string, form</summary><published>2011-01-25T09:57:00Z</published><updated>2011-01-25T09:57:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944817.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944817.html"/><content type="html">&lt;p&gt;&lt;span class="Apple-style-span" style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="text-align: left; font-family: 'Segoe UI', Arial, sans-serif; color: #333333; font-size: 14px;"&gt;&lt;div class="entry-body" style="line-height: 19px; clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Value Providers&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;ASP.NET MVC 2 introduced a new method to find value providers: the&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMudmFsdWVwcm92aWRlcmZhY3RvcnkuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ValueProviderFactory&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;class. Value providers are used by the model binding system in MVC to populate the values of model objects. MVC includes value providers for several common value sources (including query string, form, route data, uploaded files, and JSON postbacks); MVC Futures includes several mode (including cookies, server values, session values, and temp data values). In ASP.NET MVC 3, we have made ValueProviderFactory instances findable via the dependency resolver.&lt;/p&gt;&lt;/div&gt;&lt;a id="more" style="color: #56763a; text-decoration: underline;"&gt;&lt;/a&gt;&lt;div class="entry-more" style="clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9hYm91dC5odG1s&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;contact me&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;if you have comments.&lt;/em&gt;&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Implementing ValueProviderFactory&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Developers who implement this class must provide an implementation of GetValueProvider which, given a controller context, optionally returns an instance of a class which implements the&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMuaXZhbHVlcHJvdmlkZXIuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;IValueProvider&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;interface. The developer may also choose to return null if there is no appropriate value provider or values.&lt;/p&gt;&lt;p&gt;For reference purposes, there are several implementations of ValueProviderFactory in the MVC and MVC Futures source code. For a simple example, see the pair of classes:&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMucXVlcnlzdHJpbmd2YWx1ZXByb3ZpZGVyZmFjdG9yeS5hc3B4&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;QueryStringValueProviderFactory&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;and&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMucXVlcnlzdHJpbmd2YWx1ZXByb3ZpZGVyLmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;QueryStringValueProvider&lt;/a&gt;.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Implementing IValueProvider&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Developers who implement this interface provide implementations of two methods: ContainsPrefix and GetValue. The former method is used to determine if there are any values in the provider with the given prefix (so the model binding system knows when to stop recursively binding). The latter method is used to get the value for a specific key.&lt;/p&gt;&lt;p&gt;There are two implementations of IValueProvider (&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubmFtZXZhbHVlY29sbGVjdGlvbnZhbHVlcHJvdmlkZXIuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;NameValueCollectionValueProvider&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;and&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvZWU3MDM0NzEuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;DictionaryValueProvider&lt;/a&gt;) will accept collections of values to be used for the value provider implementation. Most of the value providers in the MVC source code actually derive from one of these two base classes.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Location: ValueProviderFactory&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is a &amp;ldquo;multiply registered&amp;rdquo; style service introduced in MVC 2. The static registration point for this service is at&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMudmFsdWVwcm92aWRlcmZhY3Rvcmllcy5mYWN0b3JpZXMuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ValueProviderFactories.Factories&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;for non-DI users.&lt;/p&gt;&lt;p&gt;The logic in&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMudmFsdWVwcm92aWRlcmZhY3Rvcnljb2xsZWN0aW9uLmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ValueProviderFactoryCollection&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;(which implements ValueProviderFactories.Factories) was updated to consult the dependency resolver, calling GetServices(typeof(ValueProviderFactory)) and adding any services found to the list of statically registered services. Value providers all collaborate to provide values for model binding, with a &amp;ldquo;first one to provide the value wins&amp;rdquo; strategy, so registration order is important. The factories found in the dependency resolver will always run in the order they are returned from the resolver (and before the statically registered value provider factories); similarly, the value providers will be consulted in the order they are returned from the factories.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;What&amp;rsquo;s Next?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Next up we&amp;rsquo;ll talk about&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9ibG9nLzIwMTAvMTAvc2VydmljZS1sb2NhdGlvbi1wdDktbW9kZWwtYmluZGVycy5odG1s&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;Model Binders&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/1944817.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944817.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/01/25/1944819.html</id><title type="text">【转】ASP.NET MVC 3 Service Location, Part 9: Model Binders</title><summary type="text">Model BindersASP.NET MVC 1.0 introduced theIModelBinderinterface. Developers who implement this interface are responsible for creating models from values obtained from value providers. In ASP.NET MVC 3, we introduced a new interface (IModelBinderProvider) which allows developers to dynamically provi</summary><published>2011-01-25T09:57:00Z</published><updated>2011-01-25T09:57:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944819.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944819.html"/><content type="html">&lt;p&gt;&lt;span class="Apple-style-span" style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="text-align: left; font-family: 'Segoe UI', Arial, sans-serif; color: #333333; font-size: 14px;"&gt;&lt;div class="entry-body" style="line-height: 19px; clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Model Binders&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;ASP.NET MVC 1.0 introduced the&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMuaW1vZGVsYmluZGVyLmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;IModelBinder&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;interface. Developers who implement this interface are responsible for creating models from values obtained from value providers. In ASP.NET MVC 3, we introduced a new interface (IModelBinderProvider) which allows developers to dynamically provide implementations of IModelBinder. We have made IModelBinderProvider instances findable via the dependency resolver.&lt;/p&gt;&lt;/div&gt;&lt;a id="more" style="color: #56763a; text-decoration: underline;"&gt;&lt;/a&gt;&lt;div class="entry-more" style="clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9hYm91dC5odG1s&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;contact me&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;if you have comments.&lt;/em&gt;&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Implementing IModelBinderProvider&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;In prior versions of MVC, developers could only register static mappings from types to model binder instances (through&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubW9kZWxiaW5kZXJzLmJpbmRlcnMuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ModelBinders.Binders&lt;/a&gt;). While this satisfied simple uses, it made complex and powerful model binders a little more challenging to write.&lt;/p&gt;&lt;p&gt;When we decided to open model binding up for dependency resolution, we knew that we were going to need to introduce a layer of indirection, since model binders are usually only valid for a limited number of types (unless you are the default model binder).&lt;/p&gt;&lt;p&gt;We solved both problems by introducing a new interface:&lt;/p&gt;public interface IModelBinderProvider {&lt;br/&gt;    IModelBinder GetBinder(Type modelType);&lt;br/&gt;}&lt;p&gt;Developers who implement this interface can optionally return an implementation of IModelBinder for a given type (they should return null if they cannot create a binder for the given type).&lt;/p&gt;&lt;p&gt;The system continues to rely on a &amp;ldquo;default model binder&amp;rdquo; if the system cannot find any appropriate model binder for the given type. You can change the default model binder by setting&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubW9kZWxiaW5kZXJkaWN0aW9uYXJ5LmRlZmF1bHRiaW5kZXIuYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ModelBinders.Binders.DefaultBinder&lt;/a&gt;. Out of the box, this is set to an instance of&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMuZGVmYXVsdG1vZGVsYmluZGVyLmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;DefaultModelBinder&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;There are no implementations of IModelBinderProvider in MVC. However, there is a similar system in MVC Futures from MVC 2 (in the ModelBinding folder), and there are several model binder providers that could be used to spark ideas about writing providers for MVC 3, including model binder providers for arrays, binary data, collections, complex objects, and dictionaries.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Location: IModelBinderProvider&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is a &amp;ldquo;multiply registered&amp;rdquo; style service introduced in MVC 3. The static registration point for this service is at ModelBinderProviders.BinderProviders for non-DI users.&lt;/p&gt;&lt;p&gt;The logic in ModelBinderProviderCollection (which implements ModelBinderProviders.BinderProviders) consults the dependency resolver, calling GetSerivces(typeof(IModelBinderProvider)) and adding any services found to the list of statically registered services. Model binder provides are run in-order, with a &amp;ldquo;first one to provide a model binder wins&amp;rdquo; strategy, so registration order is important. The providers found in the dependency resolver will always run in the order they are returned from the resolver (and before the static registered providers).&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;What&amp;rsquo;s Next?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Next up we&amp;rsquo;ll look at the new&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9ibG9nLzIwMTAvMTAvc2VydmljZS1sb2NhdGlvbi1wdDEwLWNvbnRyb2xsZXItYWN0aXZhdG9yLmh0bWw%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;Controller Activator&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;service.&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/1944819.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944819.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/01/25/1944816.html</id><title type="text">【转】ASP.NET MVC 3 Service Location, Part 7: Model Metadata</title><summary type="text">Model MetadataASP.NET MVC 2 introduced an extensible model metadata system where developers could implement a class which derived fromModelMetadataProviderto provide meta-information about the models in the system. In ASP.NET MVC 3, we have made the metadata provider findable via the dependency reso</summary><published>2011-01-25T09:56:00Z</published><updated>2011-01-25T09:56:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944816.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944816.html"/><content type="html">&lt;p&gt;&lt;span class="Apple-style-span" style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="text-align: left; font-family: 'Segoe UI', Arial, sans-serif; color: #333333; font-size: 14px;"&gt;&lt;div class="entry-body" style="line-height: 19px; clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Model Metadata&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;ASP.NET MVC 2 introduced an extensible model metadata system where developers could implement a class which derived from&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubW9kZWxtZXRhZGF0YXByb3ZpZGVyLmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ModelMetadataProvider&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;to provide meta-information about the models in the system. In ASP.NET MVC 3, we have made the metadata provider findable via the dependency resolver.&lt;/p&gt;&lt;/div&gt;&lt;a id="more" style="color: #56763a; text-decoration: underline;"&gt;&lt;/a&gt;&lt;div class="entry-more" style="clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9hYm91dC5odG1s&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;contact me&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;if you have comments.&lt;/em&gt;&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Writing a Metadata Provider&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;For more information about ModelMetadata and what would be involved in writing a metadata provider, please see my blog post&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9ibG9nLzIwMDkvMTAvYXNwbmV0LW12Yy0yLXRlbXBsYXRlcy1wYXJ0LTItbW9kZWxtZXRhZGF0YS5odG1s&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ASP.NET MVC 2 Templates, Part 2: ModelMetadata&lt;/a&gt;.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Location: ModelMetadataProvider&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is a &amp;ldquo;singly registered&amp;rdquo; style service introduced in MVC 2. The static registration point for this service is at&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubW9kZWxtZXRhZGF0YXByb3ZpZGVycy5jdXJyZW50LmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ModelMetdataProviders.Current&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;for non-DI users.&lt;/p&gt;&lt;p&gt;The model metadata provider inspects models, and returns meta-information about the models, include information like display names, formatting strings for display and editing, flags on whether a value is editable or not, etc.&lt;/p&gt;&lt;p&gt;The logic in&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubW9kZWxtZXRhZGF0YXByb3ZpZGVycy5hc3B4&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ModelMetadataProviders&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;was updated to attempt to find the implementation of ModelMetadataProvider first by calling DependencyResolver.GetService(typeof(ModelMetadataProvider)). If the service does not exist in the dependency resolver, then it falls back to the static registration point. If you register a model metadata provider in both the static registration point and in the dependency resolver, MVC will throw an exception (since the service is intended to be singly registered).&lt;/p&gt;&lt;p&gt;The default model metadata provider continues to be&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMuRGF0YUFubm90YXRpb25zTW9kZWxNZXRhZGF0YVByb3ZpZGVyLmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;DataAnnotationsModelMetadataProvider&lt;/a&gt;.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;What&amp;rsquo;s Next?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Next up in the series is&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9ibG9nLzIwMTAvMTAvc2VydmljZS1sb2NhdGlvbi1wdDgtdmFsdWUtcHJvdmlkZXJzLmh0bWw%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;Value Providers&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/1944816.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944816.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/NickYao/archive/2011/01/25/1944807.html</id><title type="text">【转】ASP.NET MVC 3 Service Location, Part 6: Model Validation</title><summary type="text">Model ValidatorsASP.NET MVC 2 introduced an extensible model validation system where developers could implement a class which derived fromModelValidatorProviderto influence the validation process on both the client-side and server-side. In ASP.NET MVC 3, we have made these validator providers findab</summary><published>2011-01-25T09:48:00Z</published><updated>2011-01-25T09:48:00Z</updated><author><name>四眼蒙面侠</name><uri>http://www.cnblogs.com/NickYao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944807.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944807.html"/><content type="html">&lt;p&gt;&lt;span class="Apple-style-span" style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="text-align: left; font-family: 'Segoe UI', Arial, sans-serif; color: #333333; font-size: 14px;"&gt;&lt;div class="entry-body" style="line-height: 19px; clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Model Validators&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;ASP.NET MVC 2 introduced an extensible model validation system where developers could implement a class which derived from&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubW9kZWx2YWxpZGF0b3Jwcm92aWRlci5hc3B4&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ModelValidatorProvider&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;to influence the validation process on both the client-side and server-side. In ASP.NET MVC 3, we have made these validator providers findable via the dependency resolver.&lt;/p&gt;&lt;/div&gt;&lt;a id="more" style="color: #56763a; text-decoration: underline;"&gt;&lt;/a&gt;&lt;div class="entry-more" style="clear: both;"&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9hYm91dC5odG1s&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;contact me&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;if you have comments.&lt;/em&gt;&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Writing a Validator Provider&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;For an example implementation of ModelValidatorProvider, see my blog post:&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9ibG9nLzIwMDkvMTAvZW50ZXJwcmlzZS1saWJyYXJ5LXZhbGlkYXRpb24tZXhhbXBsZS1mb3ItYXNwbmV0LW12Yy0yLmh0bWw%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;Enterprise Library Validation example for ASP.NET MVC 2&lt;/a&gt;.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;Location: ModelValidatorProvider&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is a &amp;ldquo;multiply registered&amp;rdquo; style service introduced in MVC 2. The static registration point for this service is at&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubW9kZWx2YWxpZGF0b3Jwcm92aWRlcnMucHJvdmlkZXJzLmFzcHg%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ModelValidatorProviders.Providers&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;for non-DI users.&lt;/p&gt;&lt;p&gt;Model validator providers inspect models, and return validators that ensure that the models are valid. They also participate in the client-side validation by returning client &amp;ldquo;validation hints&amp;rdquo; consumed by JavaScript.&lt;/p&gt;&lt;p&gt;The logic in&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vbXNkbi5taWNyb3NvZnQuY29tL2VuLXVzL2xpYnJhcnkvc3lzdGVtLndlYi5tdmMubW9kZWx2YWxpZGF0b3Jwcm92aWRlcmNvbGxlY3Rpb24uYXNweA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;ModelValidatorProviderCollection&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;(which implements ModelValidatorProviders.Providers) was updated to consult the dependency resolver, calling GetServices(typeof(ModelValidatorProvider)) and adding any services found to the list of statically registered service. Validator providers all collaborate to potentially validate a model value, so the ordering of validator providers is not important. Every provider will get an opportunity to provide validators.&lt;/p&gt;&lt;h2 style="margin-top: 10px; font-family: 'Segoe UI', Arial, sans-serif; margin-bottom: 10px; font-weight: bold !important;"&gt;What&amp;rsquo;s Next?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Next up we&amp;rsquo;ll talk about&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="http://la-3.azurf.com/browse.php?u=Oi8vYnJhZHdpbHNvbi50eXBlcGFkLmNvbS9ibG9nLzIwMTAvMTAvc2VydmljZS1sb2NhdGlvbi1wdDctbW9kZWwtbWV0YWRhdGEuaHRtbA%3D%3D&amp;amp;amp;b=6" style="color: #56763a; text-decoration: underline;"&gt;Model Metadata&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/NickYao/aggbug/1944807.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/NickYao/archive/2011/01/25/1944807.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry></feed>
