Exploring the web development architecture based on ASP.net)

Source: Internet
Author: User

Problem cause

Recently, I have studied a Web architecture solution suitable for team development. This architecture is suitable for division of labor, collaboration, and scalability. It is suitable for different database needs. Therefore, I have consulted some materials, I have come up with a preliminary architecture. Please give me more advice.

 

Exploration

The most typical web development architecture is the three-tier architecture, presentation layer, logic layer, and data processing layer.

Data access layer: This layer is mainly used to access databases.

Business logic layer: it is the core of the entire system and is related to the business (domain) of the system.

Presentation Layer: it is the UI part of the system and is responsible for user interaction with the entire system. The ideal state is that the presentation layer should not include the business logic of the system.

These are classic interpretations. If you want to adapt to different databases, you need to add the factory mode, which uses the interface-oriented method for multi-state invocation. Is it a bit like petshop. Therefore, the initial idea of the architecture is as follows:

The following uses getting user information as an example to briefly describe the process of this architecture: (The following is a classic practice similar to petshop, And you can skip it)

Step 1First, we should establish the entity model required by the project. Here we will create the entity mode of user information, userinfo. CS. This class is saved in the model project.

Step 2, We will then write the unit function of the project to the relevant interface. Here we take the function of getting user information as an example. Create an iuser interface in the idal project.

// Obtain user information based on the user ID
Userinfo getuserbyid (INT userid );

Step 3After the interface is completed, we need to implement it. Now we use sqlserver and Oracle database access methods to implement it. The following is the reality of the user class interface in sqlserverdal:

Public class user: iuser
{


Public userinfo getuserbyid (INT userid)
{
// Implement the operation

}

}

In structured ledal, the actual method is similar ....

Step 4In this case, the database access layer should have been basically written, and the logic layer should be called below. But how can we call the two implementation methods or choose to call them, petshop handles this. In dalfactory, The dataaccess class loads data using reflection.ProgramSet to instantiate the required class:

Private Static readonly string Path = configurationmanager. deleettings ["webdal"];
Public static iuser createuser ()
{

String classname = path + ". User ";
Return (iuser) Assembly. Load (PATH). createinstance (classname );

}

To select the database access layer, configure webdal in the configuration file. For example, <add key = "webdal" value = "sqlserverdal"/>.

In this way, the coupling between the logic layer and the data access layer is basically solved.

Step 5And the write logic layer. Create the user. CS class in BLL. It is roughly as follows:

Public class user
{
Private Static readonly iuser Dal = dalfactory. dataaccess. createuser ();

Public userinfo getuserinfo (INT userid ){
Return Dal. getuserbyid (userid );
}

}

Do you think BLL is meaningless, because it is just a simple call to the data access layer method, but it is not like this. Here is only a simple example, in an actual project, BLL may process a very complex logic, and the results of this complex logic are provided to the presentation layer for display.

Step 6And finally the presentation layer. It seems that there is nothing to say. Just bind the data obtained from BLL to your page.

The above is the petshop-like architecture design. It seems that there is no question about it. After all, it is a classic case of Microsoft. There are two points you may complain about. One is whether there are too many layers and the relationship is too complex. The other is not very painful if I need to change or add a database field, because it needs to be modified in combination. I can't solve either of these two problems. If we say that too many layers are too complicated, then what I wrote below seems to be more complicated. Forgive me .... 2. I think that any hierarchical development, as long as the entity model is built based on database fields, will have the problem of joint modification. Unless datatable is used, you do not know what datatable is installed during BLL and presentation layer calls, which makes it more inconvenient to call. If you have any solutions to the problem of reducing the number of consortium modifications, please advise.

Modify the above Architecture

I focused on the design of the data access factory in the above architecture, that is, the dataaccess class in dalfactory. In this class, different database access layers are called.

. However, if there is a project with sqlserver and Oracle in it, do we want to do this:

Private Static readonly string Path = configurationmanager. appsettings ["webdal"]; // call the sqlserver database access layer.

Private Static readonly string path2 = configurationmanager. deleettings ["webdal2"]; // call the access layer of the replicel Database

Public static iuser createuser ()
{

String classname = path + ". User ";
Return (iuser) Assembly. Load (PATH). createinstance (classname );

}

Public static iorder createorder ()
{

String classname = path2 + ". Order ";
Return (iuser) Assembly. Load (path2). createinstance (classname );

}

To create other category classes, we also need to write createproduct (), createarticle, createmenu ..... So this kind of class will be very cumbersome. Can we

Only one method is required. Other work only needs to be done by the developer through the configuration file. My solutions include:

I. spring.net

This is specifically used for decoupling. we load the related assembly into dalfactory, so in dataaccess, we can do:

Private Static readonly string configpath = httpcontext. Current. Request. physicalapplicationpath +

Configurationmanager. appsettings ["objectconfig"]; // This is the physical location of the spring.net object configuration file on the server.
Public static t Createobject <t> ()
{

Iresource rs = new filesystemresource (configpath );
Iobjectfactory factory = new xmlobjectfactory (RS );
String id = typeof (T). fullname;
Return (t) Factory. GetObject (ID );

}

Here we pass in a generic type so that spring.net can find the assembly of this type in its object configuration file and load it to create the corresponding object. Objectconfig File

It is roughly as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Objects xmlns = "http://www.springframework.net "/
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.net/
Http://www.springframework.net/xsd/spring-objects.xsd>

<Object ID = "idal. iuser" type = "sqlserverdal. function"> </Object>

</Objects>

This is called in BLL.

Private Static readonly iuser Dal = etouraf. Shared. dalfactory. dataaccess. Createobject <iuser> ();

In this way, if a developer wants to add an object, add the relevant configuration in object-config. Hey, this becomes petshop + spring.net, and there is no limit to YY ....

2. Reflection is also used

Here we only use a key-Value Pair method. As shown in the following example, we configure the corresponding interface and object in the configuration file, but we configure it to Web. config:

<Add key = "idal. iuser" value = "sqlserverdal. function"/>

In dataaccess, we write as follows:

Public static t Createobject <t> ()
{
String interfacefullname = typeof (T). fullname;
String classname = configurationmanager. receivettings [interfacefullname];
String namespace = classname. substring (0, classname. lastindexof ("."));
Return (t) Assembly. Load (namespace). createinstance (classname );
}

Some may say

String namespace = classname. substring (0, classname. lastindexof ("."));

Is this interception a bit hard? I only want to do it now, but it will never be a problem.

Well, these are some of the gains of the monks over the past two days. Please advise.

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.