WebForm-带接口工厂模式的三层架构

ylbtech-Architecture:WebForm-工厂模式
 
1.A,概念
工厂模式定义:实例化对象,用工厂方法代替new操作.

工厂,
作用:需要什么,制造什么

好处:有助有技术保护,后期维护。
1.B,解决方案资源管理器截图

 

1.C,类库(网站)-创建流程及引用类
带工厂的三层架构
DBUtility  工具类

Model 属性类

IDAL 接口类 <--Model

SqlServerDAL  访问SqlServer数据库的操作 <---Model,IDAL

DALFactory 工厂类 <--IDAL,SqlServerDAL

BLL 业务类  <---Model,IDAL,DALFactory

Web <--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
{
    /// <summary>
    /// 接口,
    /// 作用:定义方法规范和约束
    /// 特点:只有方法头,没有方法体
    /// </summary>
    public interface IPerson
    {
        /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="dal"></param>
        /// <returns></returns>
        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
    {
        /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="dal"></param>
        /// <returns></returns>
        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 
    {
        /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="dal"></param>
        /// <returns></returns>
        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
{
    /// <summary>
    /// 工厂,
    /// 作用:需要什么,制造什么
    /// </summary>
    /// 
    /// 
    /// <summary>
    /// 对象事例化的实现方法有2种
    /// 1,通过关键字"new"
    /// 2,通过字符串的映射
    /// </summary>
    /// 
    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();

         /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="dal"></param>
        /// <returns></returns>
        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
<?xml version="1.0"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <connectionStrings>
        <add name="ConnStr_Northwind" connectionString="Server=.;Database=Northwind;uid=sa;pwd=abc"/>
    </connectionStrings>
    <appSettings>
        <add key="WebDAL" value="OracleDAL"/>
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    </system.web>
</configuration>
1.E,注意事项

 相关引用:

百度百科-工厂模式  百度百科-三层架构  百度百科-三层架构体系
1.F,代码下载
谷歌开源代码下载 http://code.google.com/p/ylbtechframework/downloads/list
2,
https://files.cnblogs.com/files/ylbtech/Framework_SolutionFactory.rar
请单击“SolutionFactory”
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2013-02-21 14:32  ylbtech  阅读(1233)  评论(0编辑  收藏  举报