Three-tier architecture for ASP and reflective knowledge

Source: Internet
Author: User

Project File Schema

The implementation steps are: 4-3-6-5-2-1
Id
Project
Describe
Use


Project Reference Relationships


Required files for the instance


Related methods

1


Web


Presentation Layer


Web pages and controls


References to BLL


Webui.aspx

WebUI.aspx.cs

GetContent ()

2


Bll


Business Logic Layer


Business logic Components


Referencing Idal,model, creating an instance with Dalfactory


Content.cs


ContentInfo getcontentinfo (int id)

3


Idal


Data Access Layer Interface definition


A set of interfaces to be implemented for each DAL implementation


Reference Model


IContent.cs


ContentInfo getcontentinfo (int id)

4


Model


Business Entities


Containers that pass a variety of data


No references


ContentInfo.cs


5, Dalfactory

Abstract factory of the data layer

Create reflection to determine which database is loaded to access the class of the Assembly

Reference Idal, which reads the assembly set in Web. config, loads an instance of the class and returns it to the BLL.

Content.cs

Idal. Icontent Create ()

6, Sqlserverdal

SQL Server data Access layer

Microsoft SQL Server-specific Pet shop DAL implementation, using the Idal interface

Referencing the model and Idal, the assembly that is loaded by the dalfactory, implements the method in the interface.

SqlHelper.cs

Content.cs

SqlDataReader ExecuteReader ()

PrepareCommand ()

ContentInfo getcontentinfo (int id)

Oracledal

Oracle Data Access Layer

7, Dbutility

Database Access Component base class

Getsqlserverconnectionstring Get the database connection string, you can also save the project, in Sqlserverdal.sqlhelper with static readonly string sqlConnectionString instead.


No references

Implementing the Step Process

1. Create model to realize business entity.

2, create idal, implement interface.

3, create Sqlserverdal, implement the method in the interface.

4. Add the configuration information in Web. config to the Sqlserverdal assembly.

5. Create a dalfactory that returns an instance of the specified class of the assembly.

6, create the BLL, call Dalfactory, get an instance of the specified class of the assembly, complete the data operation method.

7. Create the Web and call the data manipulation method in the BLL.

Attention:

1. The assembly name in Web. config must match the name of the output assembly in Sqlserverdal.

2, dalfactory only need a dataaccess class, you can complete the creation of all the assembly instances.

3. After the project is created, be careful to modify the default namespace and assembly name for each.

4. Be careful to modify the project dependencies in the solution.

5, pay attention to add the project references in the solution.

Iii. the access process between the parties

1. Pass in the value and convert the value to the integer type.

2, create the Content.cs object C of the BLL layer, the method of accessing the BLL layer by object C Getcontentinfo (ID) calls the BLL layer.

3, the BLL layer method Getcontentinfo (ID) obtains the data access Layer Sqlserverdal instance, instantiates the Idal layer interface object Dal, this object is created by the factory layer Dalfactory, It then returns the method Dal.getcontentinfo (ID) of the Idal layer to find the contents of the incoming value.

4. The Data Factory accesses the Sqlserverdal layer through the Webdal string given in the Web. config file, returning a complete call to the Sqlserverdal layer's path to the BLL layer.

5, to call the Sqlserverdal layer, the Sqlserverdal layer to complete the assignment of the model layer of the object value is empty, given a parameter, call Sqlserverdal layer SqlHelper ExecuteReader method, read out the data assignment for each field to the object defined as the empty model layer.

6, SqlHelper Execute SQL command, return a specified connection database recordset, where you need to reference the parameter type, provide to open the connection command to prepare PrepareCommand.

7, return the model layer to the query to get a row of record value assigned to the Sqlserverdal layer of the introduction of the model layer of the object CI, and then return this object to the BLL.

8, back to the web layer of the BLL layer method call, the resulting object value assigned to the lable tag, in the foreground display to the interface

Iv. List of documents in the project

1. Dbutility Project

(1) ConnectionInfo.cs

Using System;
Using System.Configuration;
Namespace Utility
{
<summary>
A summary description of the ConnectionInfo.
</summary>
public class ConnectionInfo
{
public static string Getsqlserverconnectionstring ()
{
return configurationsettings.appsettings["sqlconnstring"];
}
}
}

2. Sqlserverdal Project
(1) SqlHelper.cs abstract class
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using Dbutility;

Namespace Sqlserverdal
{
A summary description of the SqlHelper.
Public abstract class SqlHelper
{
public static readonly String conn_str = Connectioninfo.getsqlserverconnectionstring ();
Executes the SQL command with the provided function, returning a database recordset from the specified connection
For example:
SqlDataReader r = ExecuteReader (connstring, CommandType.StoredProcedure, "Publishorders", New SqlParameter ("@ ProdID ", 24));

</remarks>

<param name= "connectionString" >sqlconnection a valid SQL connection string </param>

<param name= "CommandType" >commandtype:commandtype.text, commandtype.storedprocedure</param>

<param name= "CommandText" >sql statements or stored procedures </param>

<param name= "Commandparameters" >sqlparameter[] parameter array </param>

<returns>sqldatareader: Record set of execution results </returns>

public static SqlDataReader ExecuteReader (String connstring, CommandType cmdtype, String cmdtext, params sqlparameter[] C Mdparms)

{

SqlCommand cmd = new SqlCommand ();

SqlConnection conn = new SqlConnection (connstring);


We use Try/catch here because if this method throws an exception, we aim to close the database connection and then throw an exception,

Since there will be no DataReader, then commandbehaviour.closeconnection will not work.

Try

{

PrepareCommand (CMD, conn, null, Cmdtype, Cmdtext, cmdparms);

SqlDataReader rdr = cmd. ExecuteReader (commandbehavior.closeconnection);

Cmd. Parameters.clear ();

return RDR;

}

Catch

{

Conn. Close ();

Throw

}

}

<summary>

Prepare for the command: Open the database connection, command statement, set the command type (SQL statement or stored procedure), function language.

</summary>

<param name= "cmd" >sqlcommand components </param>

<param name= "conn" >sqlconnection components </param>

<param name= "Trans" >sqltransaction component, can be null</param>

<param name= "Cmdtype" > Statement type: CommandType.Text, commandtype.storedprocedure</param>

<param name= "Cmdtext" >sql statement that can be used for stored procedures </param>

<param name= "cmdparms" >sql parameter array </param>

private static void PrepareCommand (SqlCommand cmd, SqlConnection conn, SqlTransaction Trans, CommandType cmdtype, string C Mdtext, sqlparameter[] cmdparms)

{

IF (Conn. State = ConnectionState.Open)

Conn. Open ();


Cmd. Connection = conn;

Cmd.commandtext = Cmdtext;

if (trans! = null)

Cmd. Transaction = trans;

Cmd.commandtype = Cmdtype;

if (cmdparms! = null)

{

foreach (SqlParameter parm in cmdparms)

Cmd. Parameters.Add (Parm);

}

}

}

}

(2) Content.cs class
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using Model;
Using Idal;

Namespace Sqlserverdal
{
<summary>

A summary description of the Content.

</summary>

public class Content:icontent

{

Private Const string parm_id = "@ID";

Private Const string sql_select_content = "Select ID, Title, CONTENT, Adddate, CategoryID from newscontent where ID = @ID" ;

Public ContentInfo getcontentinfo (int id)

{

Creative article content Class

ContentInfo ci = null;


Create a parameter

SqlParameter parm = new SqlParameter (parm_id, Sqldbtype.bigint, 8);

Assigning the ID value

Parm. Value = ID;

using (SqlDataReader SDR = Sqlhelper.executereader (Sqlhelper.conn_str, CommandType.Text, Sql_select_content, Parm))

{

if (SDR). Read ())

{

CI = new ContentInfo (SDR. GetInt32 (0), SDR. GetString (1), SDR. GetString (2),

Sdr. GetDateTime (3), SDR. GetInt32 (4), SDR. GetInt32 (5), SDR. GetString (6));

}

}

Return CI;

}

}

}


3. Model Project

(1) ContentInfo.cs

Using System;

Namespace Model

{

<summary>

A summary description of the CLASS1.

</summary>

public class ContentInfo

{

private int _id;

private string _content;

private string _title;

private string _from;

Private DateTime _adddate;

private int _clsid;

private int _tmpid;

<summary>

Article content Constructors

</summary>

<param name= "id" > article Serial number id</param>

<param name= "Content" > article content </param>

<param name= "title" > article title </param>

<param name= "from" > Article source </param>

<param name= "CLSID" > Article Classification Properties id</param>

<param name= "Tmpid" > Article Template Properties id</param>

Public contentinfo (int id,string title,string content,string from,datetime adddate,int clsid,int tmpid)

{

this._id = ID;

This._content = Content;

This._title = Title;

This._from = from;

This._adddate = adddate;

This._clsid = ClsID;

This._tmpid = Tmpid;

}

Property

public int ID

{

get {return _id;}

}

public string Content

{

get {return _content;}

}

public string Title

{

get {return _title;}

}

public string from

{

get {return _from;}

}

Public DateTime Adddate

{

get {return _adddate;}

}

public int ClsID

{

get {return _clsid;}

}

public int Tmpid

{

get {return _tmpid;}

}

}

}

4. Idal Project

(1) Icontent.cs

Using System;

Using Model;

Namespace Idal

{

<summary>

Article content Operation interface

</summary>

public interface Icontent

{

<summary>

Get the content of the article.

</summary>

<param name= "id" > Article id</param>

<returns></returns>

ContentInfo getcontentinfo (int id);

}

}

5. Dalfactory Project

(1) Content.cs

Using System;

Using System.Reflection;

Using System.Configuration;

Using Idal;

Namespace Dalfactory

{

<summary>

The production mode implements the article interface.

</summary>

public class Content

{

public static Idal. Icontent Create ()

{

The DAL interface class can be viewed here.

String path = system.configuration.configurationsettings.appsettings["Webdal"]. ToString ();

String className = path+ ". Content ";

class combinations specified with a configuration file

Return (Idal. icontent) Assembly.Load (path). CreateInstance (ClassName);

}

}

}

6. BLL Project

(1) Content.cs

Using System;

Using Model;

Using Idal;

Namespace BLL

{

<summary>

A summary description of the Content.

</summary>

public class Content

{

Public ContentInfo getcontentinfo (int id)

{

Get an example of an article content from the data access layer

Icontent dal = DALFactory.Content.Create ();

Find article content with the DAL

Return DAL. Getcontentinfo (ID);

}

}

}

7. Web Project

1. Web. config:

<appSettings>

<add key= "sqlconnstring" value= "Data source=localhost; Persist Security info=true;initial Catalog=newsdb; User Id=sa; Password= "/>

<add key= "Webdal" value= "Sqlserverdal"/>

</appSettings>


2, Webui.aspx

<%@ page language= "C #" codebehind= "WebUI.aspx.cs" autoeventwireup= "false" inherits= "Web.webui"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

<HTML>

<HEAD>

<title>WebUI</title>

<meta name= "GENERATOR" content= "Microsoft Visual Studio. NET 7.1" >

<meta name= "Code_language" content= "C #" >

<meta name= "vs_defaultClientScript" content= "JavaScript" >

<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >

</HEAD>

<body ms_positioning= "GridLayout" >

<form id= "Form1" method= "POST" runat= "Server" >

<font "> Song body" ></FONT>

<table width= "border=" 1 ">

<tr>

<TD style= "width:173px" >&nbsp;</td>

<td>&nbsp;

<asp:label id= "Lbltitle" runat= "Server" ></asp:Label></td>

</tr>

<tr>

<TD style= "WIDTH:173PX; height:22px ">&nbsp;</td>

<TD style= "height:22px" >&nbsp;

<asp:label id= "Lbldatatime" runat= "Server" ></asp:Label></td>

</tr>

<tr>

<TD style= "width:173px" >&nbsp;</td>

<td>&nbsp;

<asp:label id= "lblcontent" runat= "Server" ></asp:Label></td>

</tr>

<tr>

<TD style= "width:173px" >&nbsp;</td>

<td>&nbsp;</td>

</tr>

<tr>

<TD style= "WIDTH:173PX; Height:23px ">&nbsp;</td>

<TD style= "height:23px" >&nbsp;</td>

</tr>

<tr>

<TD style= "width:173px" >&nbsp;</td>

<td>&nbsp;</td>

</tr>

<tr>

<TD style= "width:173px" >&nbsp;</td>

<td>&nbsp;</td>

</tr>

<tr>

<TD style= "width:173px" >&nbsp;</td>

<td>&nbsp;</td>

</tr>

<tr>

<TD style= "width:173px" >&nbsp;</td>

<td>&nbsp;

<asp:label id= "lblmsg" runat= "Server" >Label</asp:Label></td>

</tr>

</table>

</form>

</body>

</HTML>

Three-tier architecture for ASP and reflective knowledge

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.