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
- Some viewmodel creation may be "complicated". From the perspective of the controller, It is not "master control flow"
- 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":
- Create
- 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.