Exploration of web development architecture based on ASP

Source: Internet
Author: User

Origin of the problem

Recently in the research for the team development of the Web Architecture solution, the architecture is to be suitable for the division of cooperation and a certain degree of extensibility, suitable for different database needs, so I looked up some information, initially conceived a set of architecture, please give me a lot of advice.

Explore

The most classic of web development architecture is three layer architecture, representing layer, logical layer and data processing layer.

Data Access layer: Its function is mainly to be responsible for database access.

Business Logic Layer: is the core of the whole system, it is related to the business (domain) of this system.

Presentation layer: Is the UI part of the system that 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 explanations, and if you want to fit into a different database, you need to join the Factory mode, which uses an interface-oriented approach for polymorphic invocation. Is it a bit like petshop? So the initial idea for the architecture is this:

The following is an example of obtaining user information, outlining the process of this architecture: (Below is a classic approach like PetShop, which can be skipped)

Step 1 , first of all we should establish the physical model of the project, where the entity model of the new user information, UserInfo.cs. This class is stored in the model project.

Step 2 , we will then write the unit function of the project to the relevant interface, here to obtain the user information function as an example. Create a new Iuser interface in the Idal project.

Get user information based on user ID
UserInfo Getuserbyid (int userId);

Step 3, complete the interface, we will implement it, now we use SQL Server, Oracle Two 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)
{
Implementing operations

}

}

Oracledal is similar in reality ....

Step 4, in this database access layer should be basically written, the following should be called to the logical layer, but how to call the two implementation methods, or how to choose to call it, petshop is such processing, in Dalfactory in the DataAccess class To instantiate the required classes by loading the assembly with reflection:


private static readonly string path = configurationmanager.appsettings["Webdal"];
public static Iuser CreateUser ()
{

String className = Path + ". User ";
Return (Iuser) assembly.load (path). CreateInstance (ClassName);

}

As for which database access layer to select, configure the Webdal in the configuration file. such as: <add key= "Webdal" value= "Sqlserverdal"/>.

This basically solves the coupling between the logical layer and the data access layer.

Step 5, the following should write the logic layer, in the BLL create User.cs class. Roughly as follows:

public class User
{
private static readonly Iuser dal = DALFactory.DataAccess.CreateUser ();

Public UserInfo getuserinfo (int userId) {
Return DAL. Getuserbyid (USERID);
}

}

Does not feel that the BLL is meaningless, because it is only a simple call to the data access layer method, but this is not the case, there is only one simple example, in the actual project in a BLL is a very complex logic, and the result of this complex logic is provided to the presentation layer display.

Step 6, and finally the presentation layer, as if there is nothing to say, the data from the BLL to bind to your page on the line.

The above is the architectural design of imitation petshop, it seems that there is no doubt, after all, Microsoft's classic case. You may complain about two points, one is not a bit more layer, the relationship is too complex; two if I need to change or add a database field, it's not going to be very painful because it's going to be a joint modification. These two problems, I can not solve, if too many layers and cumbersome, then I write the following seems more complex, forgive .... Second, I feel that generally layered development, as long as the database field based on the establishment of the entity model, there will be a joint modification of the problem. Unless all in a DataTable, then in the BLL, the presentation layer calls do not know what the DataTable is loaded, so it is undoubtedly more convenient to invoke. To reduce the issue of joint modification, please advise if there is a workaround.

Modifications to the above schema

I have analyzed the design part of the data access factory of the above architecture, namely the DataAccess class in Dalfactory. In this class, a call to a different database access layer is implemented

。 But if there is a project with SQL Server in it and the reality of Oracle, are we going to do this:

private static readonly string path = configurationmanager.appsettings["Webdal"];//a call to the SQL Server database access layer

private static readonly string path2 = configurationmanager.appsettings["WebDAL2"];//calls to Oracel database access layer

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);

}

If you want to create additional access classes, we also write Createproduct (), Createarticle,createmenu ... Then such a class would be cumbersome, can we

Only one way, the other work only needs to be done by the developer through the configuration file. There are two of my solutions:

First, Spring.net

This thing is dedicated to decoupling, and we load its associated assemblies 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, and let Spring.net find the assembly of that type in its object configuration file and load it, creating the corresponding object. Objectconfig file

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>

So this is called in the BLL.

private static readonly Iuser dal = etouraf.shared.dalfactory.dataaccess.createobject<iuser> ();

This allows the developer to add an object to the object-config by adding a related configuration. Hey, this becomes a petshop+spring.net, yy no limit ....

Second, also with reflection

Here we just use a key-value pair way, as usual in the configuration file to configure the corresponding interface and objects, but we have to configure him in the Web. config:

<add key= "Idal. Iuser "value=" Sqlserverdal.function "/>

In DataAccess, we write:

public static T createobject<t> ()
{
String interfacefullname = typeof (T). FullName;
String className = Configurationmanager.appsettings[interfacefullname];
String nameSpace = classname.substring (0, Classname.lastindexof ("."));
Return (T) Assembly.Load (nameSpace). CreateInstance (ClassName);
}

Someone might say

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

Here this interception is not feel a bit hard, I now only think of this method, but absolutely no problem.

Well, these are probably my two days of some harvest, please advise.

Exploration of web development architecture based on ASP

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.