Architecture to improve program reusability ~ Sixth, my system structure ~ Abstract all projects that can be abstracted (finale)

Source: Internet
Author: User

Layer-N architecture and communication standards between layers

1. Overall Structure

Introduction to each module

Project. Common: it is a combination of projects that are open to all projects. It mainly provides a general function unrelated to the field.CodeLibrary

Core Project module project. Core: it is a project combination abstracted from a framework (such as LINQ to SQL as the underlying architecture) and has nothing to do with the field.

The domain project module is a specific project, such as the XXB project. It is also an N-layer architecture. Generally, its UI Layer inherits our product. web. commons project, and the corresponding entity corresponds to entity. commons project, because the BLL layer is for a special business field. the bll layer abstraction does not appear in core.

The following is the structure of a demo project:

3. Communication Standards between different layers of a specific domain Module

Because the project. common and project. core is relatively stable and shared for all solutions, so here it is not the focus of our discussion. We mainly talk about the communication problems between different layers of modules in specific fields.

Overall domain module diagram:

       
   
   
 

3.1 communication between the UI Layer and BLL Layer

The Delete and update operations use the object type. To return information, use the unified message class.

 
1 /// <Summary>2 ///Insert Product3 /// </Summary>4Vmessage addproduct (product entity );

 

query uses a unified dictionary type to store conditions and a struct to store paging parameters.

1 /// <Summary>2 ///Obtain the paging result set based on the conditions.3 /// </Summary>4 /// <Param name = "VP"> </param>5 /// <Param name = "PP"> </param>6 /// <Returns> </returns>7Pagedlist <product> getproduct (vpredication VP, pagingparam pp );

 

3.2 communication between BLL layer and Dal Layer

After the BLL layer combines the data, you can call the unified method of the data layer. The data layer only provides the most basic Gurd operations, involving multi-Table insertion and query, the bll layer is integrated for implementation.

The following is the implementation of the business-Layer Code:

 1   ///   <Summary>  2   ///  Product module implementation  3   ///   </Summary>  4   Public   Class  Productservice: servicebase, iproductservice  5  {  6 Iproductrepository = Null  ;  7     Public  Productservice ()  8   {  9 Iproductrepository = New  Productrepository ();  10   }  11   12  # Region Iproductservice becomes "employee" 13   Public  Entity. vmessage addproduct (entity. car_internal.product entity)  14   {  15   Try  16   {  17   Iproductrepository. insert (entity );  18 Vmessage. iscomplete = True  ; 19   }  20   Catch  (Exception)  21   {  22    //  Throw;  //  Comment it out after release  23 Vmessage. iscomplete = False  ;  24   } 25    Return  Vmessage;  26   }  27   Public Entity. pagedlist <entity. car_internal.product> Getproduct (entity. vpredication VP, entity. pagingparam pp)  28   {  29     VaR LINQ = Iproductrepository. getdetailmodel ();  30      Return  New Entity. pagedlist <entity. car_internal.product> (LINQ, pp. pageindex, pp. pagesize );  31   }  32   # Endregion  33 }

 

Four entity Verification Mechanism

It should be a combination of foreground special effect verification and entity validity verification. Foreground verification generally refers to JS verification, which may produce better user experience, entity verification is mostly completed at the Entity layer. Some verification is required when adding or modifying objects.

4.1 unified entity base class:

The entitybase class has completed the validation of non-null entities. The getruleviolations method returns the iteration result set of verification failures, which is a virtual method, child classes can be rewritten based on their own logic. Isvalid is an attribute. It is assigned a value when determining the object verification. True indicates that the object is successful. Otherwise, the verification fails.

For composite entities, such as the combination of multiple entities in the front-end form, you can use the MVC method to write the page elements in a viewmodel for this page view, use. net attribute can easily implement attribute verification.

At the Controller layer (which may be abstracted based on business needs), we need to determine the isvalid of the entity during object addition and modification operations, and then call the BLL layer method, the general code is as follows:

 1   [Httppost]  2   Public  Actionresult create (formcollection collection)  3   {  4 News entity = New  News ();  5   Tryupdatemodel (entity );  6 Entity. createdate =Datetime. now;  7 Entity. updatedate = Datetime. now;  8 Entity. Status = ( Int  ) Status. normal;  9   If  (Entity. isvalid)  10   {  11   Inewsrepository. insert (entity );  12   Return Alerttourl ("  Index  "  );  13   }  14   Else  15   {  16 Entity. getruleviolations (). tolist (). foreach (I => 17   {  18 Modelstate. addmodelerror (I. propertyname, I. errormessage ); //  Verification Failed Information 19   });  20   }  21   Return  View ();  22 }

 

5. Importance of interfaces

The interface may be successful. It is inconvenient to add the amount of code, and (F12) to the definition. Should we discard the interface?

Benefits:

1. Unified and Stable Operation Specifications

2. Using interfaces, configurations, and IOC, you can switch between multiple implementations of one operation.

Abstract diagram of the interface and Implementation of the Dal Layer

 
 

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.