今天看了看工厂模式把他们整理了一下,下面是我整理好的。
下面就由我介绍一下如何使用工厂模式
win7 操作系统 vs2008
数据库: zhihuigu
表名:uinfo
字段:id int primary key identity(1,1),
uname varchar(10),
pwd varchar(30),
1,创建网站
2,创建Model层,BLL层,IDAL层,Factory层,AccessDAL层,DButility层,SQlServerDAL层
3,准备工作完毕,下面开始写代码
三层架构不会的请看【浅谈】<.net>三层架构,因为这和三层架构还有关系
写的不是很严谨,有什么不对的地方 还希望你们多多赐教。
Model层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
/// <summary>
/// 用户信息
/// </summary>
public class UserInfo
{
/// <summary>
/// 用户id
/// </summary>
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
/// <summary>
/// 用户名称
/// </summary>
private string uname;
public string Uname
{
get { return uname; }
set { uname = value; }
}
/// <summary>
/// 用户密码
/// </summary>
private string pwd;
public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
}
}
IDAL层:引用Model层
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Model; namespace IDAL { public interface IUser //定义接口
{ Boolean IsLogin(UserInfo info); } }
Factory层:引用IDAL,SQlServerDAL,AccessDAL,System.configuration,
using System; using System.Collections.Generic; using System.Linq; using System.Text; using IDAL; using System.Configuration; using System.Reflection; using SQLServerDAL; //此处要引用System.Configuration Factory下面的--> 右击引用-->添加引用-->.net-->System.Configuration-->确定 最后在此页面添加命名空间 namespace Factory { public class DataAccessFactory { private static string path = ConfigurationManager.AppSettings["WebDAL"].ToString(); //此处得到配置文件里面的<AppSettings/> //将其改为<AppSettings></AppSettings> //添加数据 //( // <appSettings> // <add key="WebDAL" value="SQLServerDAL"/> // </appSettings> // ) //其中的value值是如果你想用sql数据库 就用SQlServerDAL(此处和你添加的类库名称相同) // public static IUser CreateUser() { string className = path + ".User"; return (IUser)Assembly.Load(path).CreateInstance(className); } //如果还想写一些别的话可以再重新创建一个按照以下格式当然也可以自己写 //public static IBlog CreateBlog() //{ // string className = path + ".Blog"; // return (IBlog)Assembly.Load(path).CreateInstance(className); //} } }
DButility层:引用SQLHelper
BLL层:引用Factory,Model,IDAL
using System; using System.Collections.Generic; using System.Linq; using System.Text; using IDAL; using Model; using Factory; namespace BLL { public class User { IUser user = DataAccessFactory.CreateUser(); public Boolean IsLogin(UserInfo info) { return user.IsLogin(info); } } }
AccessDAL层 引用DButility,Model,IDAL,
using System; using System.Collections.Generic; using System.Linq; using System.Text; using IDAL; using Model; using DBUtility; using System.Data.OleDb; namespace AccessDAL { public class User:IUser//此处实现接口 { /// <summary> /// 得到access的数据连接对象 /// </summary> /// <returns></returns> public OleDbConnection GetConn() { OleDbConnection conn = new OleDbConnection(); //此处为连接对象 conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\桌面\2010-12-22上午\web\App_Data\zhihuigu.mdb;Persist Security Info=False"; conn.Open(); return conn; } #region IUser 成员 /// <summary> /// 数据库中是否有此人的信息 /// </summary> /// <param name="info"></param> /// <returns>有还是没</returns> public bool IsLogin(UserInfo info) { OleDbConnection conn = GetConn(); string sql = "select count(*) from uinfo where uname=@uname and pwd=@pwd"; OleDbCommand comm = new OleDbCommand(sql,conn); comm.Parameters.AddWithValue("@uname", info.Uname); comm.Parameters.AddWithValue("@pwd", info.Pwd); int cnt = (int)AccessHelper.ExecuteScalar(comm); return cnt > 0 ? true : false; } #endregion } }
SQlServerDAL层:引用DButility,IDAL,Model
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using IDAL; using BibleDAL.SqlHelper; using Model; namespace SQLServerDAL { public class User:IUser //此处实现接口s
{ /// <summary> /// 得到数据库连接对象 /// </summary> /// <returns></returns> public SqlConnection GetConn() { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "server=.;database=zhihuigu;uid=sa;pwd=;"; conn.Open(); return conn; } #region IUser 成员 public bool IsLogin(UserInfo info) { SqlConnection conn = GetConn(); string sql = "select count(*) from uinfo where uname=@uname and pwd=@pwd"; SqlCommand comm = new SqlCommand(sql, conn); comm.Parameters.AddWithValue("@uname", info.Uname); comm.Parameters.AddWithValue("@pwd", info.Pwd); int cnt = (int)SqlHelper.ExecuteScalar(comm); return cnt > 0 ? true : false; } #endregion } }