Go to. Net Architecture Design-Chapter 3-layered design, initial architecture (Part II)

Source: Internet
Author: User

Go to. Net Architecture Design-Chapter 3-layered design, initial architecture (Part II)

This article focuses on the first two articles.ArticleContinue!

the topics in this article are as follows:

4. data access layer design

5. display layer design

6. UI layer design

 

 

4. Data access layer design

There is not much to say about the data access layer. However, it should be clarified that data access is not necessarily a database access. In most cases, we do store the data in the database.

Here we use a database to store data andLINQ to SQLTo access data.

 

Below we will implement some data operationsCode: 

Code

Public   Class Productrepository: iproductrepository
{
Public Ilist < Model. Product > Findall ()
{
VaR Products = From P In   New Shopdatacontext (). Products
Select New Model. Product
{
ID = P. productid,
Name = P. productname,
Price =   New Model. Price (P. RRP, P. sellinuplice)
};

ReturnProducts. tolist ();
}
}

 

5. Display Layer Design

We useModel-View-presenterMode to extract the display logic fromUILayer to become a display layer. In fact, the benefits of doing this: convenience of unit testing, but also let us changeViewFor example, we can changeAspxPage display, you can also useWinform. AboutMVPI will introduce the detailed knowledge in subsequent articles. This article is only"Initial Architecture"----It is equivalent to mentioning all the knowledge in subsequent articles.

Read the code to describe it. InAsppatterns. chap3.layered. PresentationAdd an interface class to the project:Iproductlistview.

Public   Interface Iproductlistview
{
Void Display (ilist < Productviewmodel > Products );
Model. customertype { Get ;}
String Errormessage { Set ;}
}

 

 This interface will beAspxOfWeb form.

Next we will createProductlistpresenterTo connectViewAndService.PresenterResponsible for transferring data fromServiceAnd then hand itViewTo display. The Code is as follows: 

Code

Public   Class Productlistpresenter
{
Private Iproductlistview _ productlistview;
Private Service. productservice _ productservice;

Public Productlistpresenter (iproductlistview productlistview, Service. productservice)
{
_ Productservice = Productservice;
_ Productlistview = Productlistview;
}

Public void display ()
{< br> productlistrequest = New productlistrequest ();
productlistrequest. customertype = _ productlistview. customertype;

Productlistresponse productresponse=_ Productservice. getallproductsfor (productlistrequest );

If (Productresponse. Success)
{
_ Productlistview. Display (productresponse. products );
}
Else  
{
_ Productlistview. errormessage = Productresponse. message;
}

}
}

After this implementation, we can now write some test code to test whether the data is obtained correctly. At this time, we do not have to use the page display to know the correctness of the data. The benefits of this implementation have also been mentioned before: we can give dataWPFOrWinform. 

 

6.UILayer Design

In the end, we still need to display the data.

The interface is as follows:

 

AspxThe page code is as follows: 

Code

Public   Partial   Class _ Default: system. Web. UI. Page, iproductlistview
{
Private Productlistpresenter _ presenter;

Protected   Void Page_init ( Object Sender, eventargs E)
{
_ Presenter =   New Productlistpresenter ( This , Objectfactory. getinstance < Service. productservice > ());
This . Ddlcustomertype. selectedindexchanged + =   Delegate {_ Presenter. Display ();};
}

Protected   Void Page_load ( Object Sender, eventargs E)
{
If (Page. ispostback ! =   True )
_ Presenter. Display ();
}

Public   Void Display (ilist < Productviewmodel > Products)
{
Rptproducts. datasource = Products;
Rptproducts. databind ();
}

Public Customertype
{
Get { Return (Customertype) enum. toobject ( Typeof (Customertype ), Int . Parse ( This . Ddlcustomertype. selectedvalue ));}
}


Public   String Errormessage
{
Set {Lblerrormessage. Text = String. Format ( " <P> <strong> error </strong> <br/> {0} <p/> " , Value );}
}

}

I hope you can see that the above Code is not dizzy. The following figure shows the entire process:

Default

PageCreateProductlistpresenterAnd we useStructuremapOfObjectfactory. getinstanceMethod to obtain a portalProductservice.DefaultThe page delegates any calls to its eventsPresenter,That is to say, we basically do notDefaultWhat is the logic processing behind the Page code?Presenter. 

Finally, the structure we designed is very conducive to testing and maintenance, and also has strong scalability. 

This article (before. In the middle and back) Here we are. Let's take a look at the three articles. If you have any questions, please leave a message! Thank you for your support! :)

 

Related Article

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.