ASP. net mvc implementation mode-modelbuilder

Source: Internet
Author: User
ArticleDirectory
    • General situation
    • 1. Problem
    • 2. Analysis
    • 3. Transformation

SetViewmodelFromControllerToModelbuilderIn essence, it is to separate duties and improve the readability of the controller.

General situation

When using MVC, the controller contains manyCode.
Let's start with an example.
Suppose we have a page for displaying the order (~ FT-BJS-95486 \ order \), The corresponding action is as follows:

 
Public actionresult Order (string productno) {var P = productservice. getproduct (productno); VAR model = new ordermodel {productno = productno, productname = P. productname,}; return view (model );}

Here, ordermodelViewmodelIs defined:

Public class ordermodel {Public String productno {Get; set;} Public String productname {Get; set;} public int count {Get; set;} Public String address {Get; set ;}}

Next, in order to receive the order information entered by the user, we may have suchAction:

 
[Httppost] public actionresult Order (ordermodel model) {var P = productservice. getproduct (productno); If (! Modelstate. isvaidate () {// assume: // 1. productname does not post back via hidden input. // 2. productno is returned to the model with the input information of other users. productname = P. productname; return view (model);} var model2 = new orderconfirmmodel {productno = model. productno, productname = P. productname, Count = model. count, address = model. address,}; return view ("orderconfirm", model2 );}
1. Problem
    1. Some viewmodel creation may be "complicated". From the perspective of the controller, It is not "master control flow"
    2. Viewmodel is created at different stages. duplicate code appears during different stages of creation.
2. Analysis

In the above example, although the structure and creation process of ordermodel are very simple, we can still identify the repeated parts:

 
VaR P = productservice. getproduct (productno); Model. productname = product. productname;

We can sum up the repeated parts, which belong to two parts.The "phase" of the viewmodel":

    1. Create
    2. Rebuild

Based on this, we abstractImodelbuilderInterface

 
Public interface imodelbuilder <tviewmodel> {tviewmodel create (); tviewmodel rebuild (tviewmodel model );}
3. Transformation

Let's write an ordermodelbuilder to implement the imodelbuilder interface.

Public class ordermodelbuilder: imodelbuilder <ordermodel> {private string _ productno; private string _ productname; Public ordermodelbuilder (string productno, string productname) {_ productno = productno; _ productname = productname ;} public ordermodel create () {VAR model = new ordermodel {productno = _ productno,}; model = This. rebuild (model); Return Model;} public ordermodel rebuild (ordermodel model) {model. productname = _ productname; Return Model ;}}

Use ordermodelbuilder to simplify ordercontroller

Private ordermodelbuilder getbuilder (string productno) {var P = productservice. getproduct (productno); var builder = new ordermodelbuilder (p); Return builder;} public actionresult Order (string productno) {var builder = getbuilder (productno); VAR model = builder. create (); Return view (model);} [httppost] public actionresult Order (ordermodel model) {If (! Model. isvaidate () {var builder = getbuilder (model. productno); VAR model = builder. rebuild (model); Return view (model) ;}// create orderconfirmmodel var builder2 = new orderconfirmmodelbuilder (model) based on ordermodel; var model2 = builder2.create (); return view ("orderconfirm", model2 );}

In this way, we solve the two problems we raised in section 1st.

This article introduces the content of this article, regardless of language and platform.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.