[Original address] ASP. net mvc Framework
[Original article published on] Sunday, October 14,200
In the past few years, many people have asked ASP. one thing about net is the built-in support for developing Web applications using the Model-View-controller (Model-View-Controller) architecture.
At the alt. Net Conference held in Austin last weekend, I made a public demonstration of my team's new ASP. NET MVC framework. You can view the video of my lecture on Scott hanselman's blog.
We will release a Public Preview of the framework later this year, and then release it as a fully supported ASP. NET feature in the first half of next year.
What is the Model-View-controller (MVC) framework?
MVC is a framework technology that divides the implementation of an application into three component roles: model, view, and controller.
- In MVC-based applications, model is an application component that maintains the state. This State usually persists in the database (for example, we may have a product class to represent the order data in the products data table in SQL ).
- In MVC-based applications, view is a component used to display user interfaces. This UI is usually created using model data (for example, we may generate a product "edit" view to display text boxes, drop-down boxes, and check boxes based on the status of the current product object ).
- In MVC-based applications, the controller is a component that processes user interaction, the operation model, and the final view used to display the UI. In MVC applications, views are only used to display information. They are used by controllers to process and respond to user input and interaction.
One advantage of using the MVC method is that it helps to promote the clear separation of attention between models, views, and controllers in applications. Keeping a clear separation of focus makes it extremely easy to test applications, because the definition and Expression of contracts between different application components are clearer.
The MVC mode is also conducive to the development of the red/green test driver (TDD). Through it, You can compile the applicationCodePreviously, automated unit tests were implemented, which defined and verified the requirements of new code.
Brief details of ASP. net mvc Framework
After a few weeks, the relevant code can be downloaded, I will write some information about this new ASP. net MVC Framework in-depth tutorial posts (at the same time, the best way to learn more about it is to watch my alt.net lecture video ):
Here are some brief details about the ASP. net mvc framework:
- It will promote clear separation of focus, testability, and TDD. In the MVC Framework, the core contract is based on interfaces and can be easily simulated using mock (including interface-based basic items such as ihttprequest/ihttpresponse ). You don't need to run controllers in ASP. NET processes (this makes unit testing fast), just unit testing your applications. You can use any unit testing framework you want to use for unit testing, including nunit, mbunit, MS test and so on.
- This framework is highly scalable and pluggable. All the things in the MVC Framework are designed in this way. They can be easily replaced or customized (for example, you can insert your own view engine and routing policy ), parameter serialization ). It also supports the use of existing dependency injection and IOC container models (Windsor, spring. net, nhib.pdf, and so on ).
- It includes a very powerful URL ing component that allows you to use very clean URLs to build applications. URLs do not need to have file extensions. They are designed to easily support Seo and rest-friendly naming patterns. For example, in my project above, I can easily/Products/edit/4Map to the Edit Method of the productscontroller class, or/Blogs/scottgu/10-10-2007/sometopic/Map to the displaypost method of the blogenginecontroller class.
- The MVC Framework supports the existing ASP. net. aspx ,. ascx, and. the master Identity file is used as a view template (which means you can easily use a lot of existing ASP. NET features, such as nested master pages, <% = %> blocks, declarative Service controls, templates, data binding, and localization ). However, it does not use the existing PostBack model that returns interactions to the server. Instead, you will forward all user interactions to the Controller class for scheduling, this helps to clarify the separation of attention and improve testability (this also means that there is no viewstate or page lifecycle in the MVC-based view ).
- ASP. net MVC Framework fully supports authentication like forms/Windows, URL Authorization, members/roles, output and data cache, session/profile status management, health monitoring, and Configuration System, and the provider architecture and other existing ASP.. net.
Conclusion
If you want to use MVC to build your web application, I think you will find that this new ASP. NET MVC framework option is very clean and easy to use. It will allow you to easily keep focus on separation in your application and help with clean testing and TDD.
A few weeks later, I will write an article describing how the new MVC features work and how to use them.
I hope this article will help you,
[Appendix] Scott Guthrie's reply to some comments: 1. [Q] You can use the existing aspx, ascx, and so on... If a service control requires viewstate and page lifecycle to work, what are the consequences of using server controls in MVC-based views? [Answer] The view itself is standard. aspx ,. ascx ,. master file, so there is still a corresponding background (Code-behind) class, you can process the render event of the control in it, in order to clean the view markup and view display logic separated. For declarative controls, the control model is the same for building controls. The difference is that you cannot use the PostBack feature. Logically, this is like Creating An ASPX webpage without <form runat = "server"> today. So like <asp: repeater> and other controls still work, you need to use standard form submission or <a href = ""> URL for post operations. We will also provide some server controls that are conscious of MVC and can be used in view pages without PostBack. They will be cleanly integrated with the Controller-based logic. 2. [Q] Will the MVC framework be included in vs 2008 RTM? [Answer] the MVC framework can work in vs 2008 RTM. We will release the first preview version at the same time as vs 2008 RTM. The MVC framework itself will first reside in a separate Program And then it becomes part of. Net 3.5 SP1. 3. [question] If we use the MVC Framework, are we back to the original position? Will it provide us with some appropriate benefits for those who think life is short? I don't want to re-build the tap every time I shower ). [Answer] note that the MVC Framework does not replace the existing web form model. Obviously, we will continue to fully support and enhance the functions of the web form model. If you like PostBack interaction, I suggest you continue with the current practice and use the MVP-based model for testing. The MVC model gives you more control over HTML display. But as you have noticed, there are both advantages and disadvantages. The advantage is that you have more control. The disadvantage is that the more control, the more things you need to take care. We will provide a good way to handle errors and maintain the form state, so you don't have to write ugly code to handle these things. For your interface, the server control model provides a very clean way to encapsulate the view-helper function, in addition, we will provide a rich set of controls to help you. 4. [Q] the advantage of The brail view engine is that the view is compiled independently of the main application. If you modify the view, it can be re-compiled immediately. I assume that the DLR-based view is not compiled, but in the case of the aspx view, do I need to re-load the application to change the view, resulting in a long delay? It would be nice if you don't need it. [Answer] After the. aspx web page is changed, ASP. NET automatically generates a new assembly for it after it monitors its changes. In this way, we do not need to restart the application. In the case of a DLR-based. aspx web page, we do not actually create an assembly, but can compile it in the memory. This is how ironpython-based. aspx web page works. Note that the ASP. net mvc framework is pluggable. You can use the monorail Brail view engine to display your views. So if you like The brail model, you can continue to work collaboratively with the new MVC framework. 5. [Q] Can I use model verification to launch Automatic client verification (for example, through CSS attributes) for MVC-conscious server controls )? [Answer] We will study how to allow client-side style processing and client-side JavaScript error verification from model verification whenever possible. But this will not appear in the first preview in a few weeks, but this is what we have seen recently and will be studied later. 6. [Q] Can you discuss the support for DLR, Dynamic Language, and ASP. NET ures in MVC? [Answer] LINQ will certainly be fully supported in the MVC framework. We will also add DLR support, allowing you to build views and controllers using dynamic languages including ironpython and ironruby. 7. [question] What kind of support will Ajax-related features such as updatepanel and others dependent on the PostBack model receive? [Answer] updatepanel does use PostBack, so you cannot directly use that control in the MVC-based view. However, we will provide a control with similar functions as this control and related auxiliary methods. It will call a method of the controller, allowing you to easily perform local updates. It will allow you to easily use the ASP. NET Ajax library. In a few weeks, I will write a blog to discuss more details. 8. [Q] The view code in your lecture video looks very dangerous as the old ASP (No. net ). The same is true for monorail nvelocity. I hope you can combine the advantages of both. [Answer] Some people like the <% = %> model. At the alt.net Conference, participants who demonstrated the MVC model asked me to use this method (so I wrote such code ), you can also use <asp: repeater> to create a list and use code-behind to bind data. This allows you to construct your view display code more clearly. 9. [Q] I'm just curious. How many changes do you need to make to the inner code to make the declarative server control work without using the PostBack model? Does ASP. net mvc support existing server controls on pages without <form runat = "server">? Can we use multiple HTML forms on the same page? [Answer] We don't actually need to change the internal code to support the MVC Framework. ASP. NET actually contains most of the required hooks. The ASP. net mvc framework allows you to use multiple HTML forms on the same page. Like <asp: repeater> and other controls that support read-only mode, PostBack operations are not supported. 10. [Q] In your alt. Net demo, you passed the anonymous type to the view in the form of new {"value", "value2"} from the Controller. My impression is that the anonymous type cannot be passed beyond the scope of a method. Can you explain it? [Answer] anonymous types can be passed out of their range, but the type name is unknown (for example, you can pass or return these objects in the form of objects ). The auxiliary method I use accepts an object as an optional parameter type, and then obtains the name/array pair using idictionary or reflection. So I can write <% = html. Link ("Push me", new {Action = "edit", id = P. productid}); %>