ylbtech-Architecture:WebForm-工厂模式 |
1.A,概念 |
工厂模式定义:实例化对象,用工厂方法代替new操作.
工厂,
作用:需要什么,制造什么 好处:有助有技术保护,后期维护。
1.B,解决方案资源管理器截图 |
1.C,类库(网站)-创建流程及引用类 |
带工厂的三层架构DBUtility 工具类Model 属性类IDAL 接口类 <--ModelSqlServerDAL 访问SqlServer数据库的操作 <---Model,IDALDALFactory 工厂类 <--IDAL,SqlServerDALBLL 业务类 <---Model,IDAL,DALFactoryWeb <--Model,BLL
1.D,功能实现代码(要点代码) |
1.D.1,DBUtility/SqlHelper.cs
View Code
using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using System.Data;namespace DBUtility{ public abstract class SqlHelper { public static readonly String ConnStr_Northwind = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr_Northwind"].ConnectionString; //配置 SqlCommand对象 private static void PrepareCommand(SqlCommand com, SqlConnection con, SqlTransaction trans, System.Data.CommandType cmdType, string cmdText, SqlParameter[] cmdParams)//预处理SqlCommand { if (con.State != ConnectionState.Open) { con.Open(); } com.Connection = con; com.CommandType = cmdType; com.CommandText = cmdText; //配置事务 if (trans != null) { com.Transaction = trans; } if (cmdParams != null) { foreach (SqlParameter param in cmdParams) { com.Parameters.Add(param); } //for (int i = 0; i < cmdParams.Length; i++) //{ // com.Parameters.Add(cmdParams[i]); //} } } //增删改建 ExecuteNonQuery 3个重载 public static int ExecuteNonQuery(string connectionString , System.Data.CommandType cmdType ,string cmdText , params SqlParameter []cmdParams) { SqlCommand com = new SqlCommand(); using ( SqlConnection con = new SqlConnection(connectionString) ) { PrepareCommand(com, con, null, cmdType, cmdText, cmdParams); int value = com.ExecuteNonQuery(); //清除com上的参数 com.Parameters.Clear(); return value; } } public static int ExecuteNonQuery(SqlConnection con , System.Data.CommandType cmdType, string cmdText, params SqlParameter[] cmdParams) { SqlCommand com = new SqlCommand(); PrepareCommand(com, con, null, cmdType, cmdText, cmdParams); int value = com.ExecuteNonQuery(); com.Parameters.Clear(); return value; } public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams) { SqlCommand com = new SqlCommand(); PrepareCommand(com, trans.Connection, trans, cmdType, cmdText, cmdParams); int value = com.ExecuteNonQuery(); com.Parameters.Clear(); return value; } //查询 ExecuteReader public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams) { SqlCommand com = new SqlCommand(); SqlConnection con = new SqlConnection(connectionString); PrepareCommand(com, con, null, cmdType, cmdText, cmdParams); SqlDataReader sdr = com.ExecuteReader(CommandBehavior.CloseConnection); return sdr; } //首行首列 ExecuteScalar public static object ExecuteScalar(string connectionString, System.Data.CommandType cmdType, string cmdText, params SqlParameter[] cmdParams) { SqlCommand com = new SqlCommand(); using (SqlConnection con = new SqlConnection(connectionString)) { PrepareCommand(com, con, null, cmdType, cmdText, cmdParams); object obj = com.ExecuteScalar(); com.Parameters.Clear(); return obj; } } public static object ExecuteScalar(SqlConnection con, System.Data.CommandType cmdType, string cmdText, params SqlParameter[] cmdParams) { SqlCommand com = new SqlCommand(); PrepareCommand(com, con, null, cmdType, cmdText, cmdParams); object obj = com.ExecuteScalar(); com.Parameters.Clear(); return obj; } public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams) { SqlCommand com = new SqlCommand(); PrepareCommand(com, trans.Connection, trans, cmdType, cmdText, cmdParams); object obj = com.ExecuteScalar(); com.Parameters.Clear(); return obj; } }}
1.D.1,Model/PersonInfo.cs
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Model{ public class PersonInfo { //字段 string id; string name; // 构造 public PersonInfo(string id, string name) { this.id = id; this.name = name; } //封装字段 public string Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } }}
1.D.2,IDAL/IPerson.cs
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Model;namespace IDAL{ ////// 接口, /// 作用:定义方法规范和约束 /// 特点:只有方法头,没有方法体 /// public interface IPerson { ////// 输出信息 /// /// ///string Write(PersonInfo dal); }}
1.D.3,SqlServerDAL/Person.cs
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Model;using IDAL;using DBUtility;using System.Data.SqlClient;namespace SqlServerDAL{ //:代表继承 //一个类可以实现很多接口,只能有一个父类 public class Person:IPerson { ////// 输出信息 /// /// ///public string Write(PersonInfo dal) { string str = ""; str = string.Format("SqlServer 数据库 编号{0},名称{1}",dal.Id,dal.Name); return str; } }}
1.D.3,OracleDAL/Person.cs
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Model;using IDAL;using DBUtility;using System.Data.SqlClient;namespace OracleDAL{ public class Person:IPerson { ////// 输出信息 /// /// ///public string Write(PersonInfo dal) { string str = ""; str = string.Format("Oracle 数据库 编号{0},名称{1}", dal.Id, dal.Name); return str; } }}
1.D.4,DALFactory/DataAccess.cs
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using IDAL;using SqlServerDAL;using OracleDAL;namespace DALFactory{ ////// 工厂, /// 作用:需要什么,制造什么 /// /// /// ////// 对象事例化的实现方法有2种 /// 1,通过关键字"new" /// 2,通过字符串的映射 /// /// public class DataAccess { //路径 private static readonly string path = System.Configuration.ConfigurationManager.AppSettings["WebDAL"]; //创建Person对象 public static IPerson CreatePerson() { //创建类的位置 SqlServerDAL.Person string className = path + ".Person"; //通过字符串反射事例化对象 IPerson Person = (IPerson)System.Reflection.Assembly.Load(path).CreateInstance(className); return Person; } }}
1.D.5,BLL/Person.cs
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Model;using IDAL;using DALFactory;namespace BLL{ public class Person { //创建Person IPerson person = DataAccess.CreatePerson(); ////// 输出信息 /// /// ///public string Write(PersonInfo dal) { return person.Write(dal); } }}
1.D.6,Web/
1.D.6.1,Web/Default.aspx.cs
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Model;using BLL;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { PersonInfo dal = new PersonInfo("11111","ylb"); Response.Write(new BLL.Person().Write(dal)); }}
1.D.6.1,Web/web.config
View Code
1.E,注意事项 |
相关引用:
1.F,代码下载 |
谷歌开源代码下载
2,
请单击“SolutionFactory”
作者:出处:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |