笑笑博客

站长介绍

网名:笑笑 今年18岁,计算机国家四级,北大青鸟软件工程师,JAVA SCJD认证. 自封:代码民工。。。 没啥文化,就会敲敲键盘。今年准备考网络工程师,嘎嘎~

« 乱吹世界最大粒子对撞机正式启动 VS 地球毁灭? »

.NET 三曾抽象工厂模式

作者:笑笑;

连接:www.oucao.com.cn

抽象工厂的顺序

models
DBUtility
IDAL 接口
DAL
DALFactory
BLL
UI

Models代码如下

using System;
using System.Collections.Generic;
using System.Text;

namespace StuManagementModels
{
 public class UserInfoModel
 {
  private string _userCode;

  public string UserCode
  {
   get { return _userCode; }
   set { _userCode = value; }
  }

  private string _userPassword;

  public string UserPassword
  {
   get { return _userPassword; }
   set { _userPassword = value; }
  }

 }
}

DBUtility的代码如下,这里主要是SQL-Server的数据库连接

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DBUtility
{
 public static class SqlHelper
 {
  public static string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

  public static int ExecuteQuery(string sql, params SqlParameter[] sqlParams)
  {
   using (SqlConnection conn=new SqlConnection(connectionString))
   {
    SqlCommand cmd = new SqlCommand(sql, conn);
    PrepareCommand(cmd, sqlParams);
    int rows = 0;
    try
    {
     conn.Open();
     rows = cmd.ExecuteNonQuery();
     return rows;
    }
    catch (Exception ex)
    {
     throw ex;
    }
   }
  }

  public static SqlDataReader ExecuteReader(string sql, SqlConnection conn, params SqlParameter[] sqlParams)
  {
   SqlCommand cmd = new SqlCommand(sql, conn);
   PrepareCommand(cmd,sqlParams);
   SqlDataReader dr = cmd.ExecuteReader();
   return dr;
  }

  public static DataTable GetDataTable(string sql)
  {
   using (SqlConnection conn=new SqlConnection(connectionString))
   {
    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    DataTable dt = new DataTable();
    try
    {
     da.Fill(dt);
     return dt;
    }
    catch (Exception ex)
    {
     throw ex;
    }
   }
  }

  private static void PrepareCommand(SqlCommand cmd, params SqlParameter[] sqlParams)
  {
   if (sqlParams != null)
   {
    foreach (SqlParameter p in sqlParams)
     cmd.Parameters.Add(p);
   }
  }
 }
}


IDAL  接口代码如下

using System;
using System.Collections.Generic;
using System.Text;

using StuManagementModels;

namespace StuManagementIDAL
{
 public interface IUserInfoService
 {
  int AddUserInfo(UserInfoModel userinfo);
  int UpdateUserInfo(UserInfoModel userinfo);
  int DeleteUserInfo(UserInfoModel userinfo);
  List<UserInfoModel> GetUserInfoAll();
  List<UserInfoModel> GetUserInfoByLogin(UserInfoModel userinfo);
 }
}
 

DAL代码如下

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using DBUtility;
using StuManagementIDAL;
using StuManagementModels;

namespace StuManagementDAL.SqlServer
{
 public class UserInfoService:IUserInfoService
 {
  #region IUserInfoService 成员

  string sql;

  public int AddUserInfo(UserInfoModel userinfo)
  {
   throw new Exception("The method or operation is not implemented.");
  }

  public int UpdateUserInfo(UserInfoModel userinfo)
  {
   throw new Exception("The method or operation is not implemented.");
  }

  public int DeleteUserInfo(UserInfoModel userinfo)
  {
   throw new Exception("The method or operation is not implemented.");
  }

  public List<UserInfoModel> GetUserInfoAll()
  {
   throw new Exception("The method or operation is not implemented.");
  }

  public List<UserInfoModel> GetUserInfoByLogin(UserInfoModel userinfo)
  {
   sql = "SELECT UserCode,UserPassword FROM UserInfo " +
      "WHERE UserCode=@code AND " +
        "UserPassword=@password";
   SqlParameter[] ps = new SqlParameter[2];
   ps[0] = new SqlParameter("@code", SqlDbType.NVarChar, 20);
   ps[0].Value = userinfo.UserCode;
   ps[1] = new SqlParameter("@password", SqlDbType.NVarChar, 20);
   ps[1].Value = userinfo.UserPassword;

   using (SqlConnection conn=new SqlConnection(SqlHelper.connectionString))
   {
    conn.Open();
    SqlDataReader dr = SqlHelper.ExecuteReader(sql, conn, ps);
    List<UserInfoModel> list = new List<UserInfoModel>();
    MakeUserInfoList(dr, list);
    return list;
   }
  }

  private void MakeUserInfoList(SqlDataReader dr, List<UserInfoModel> list)
  {
   if (dr == null)
    return;
   while (dr.Read())
   {
    UserInfoModel userinfo = new UserInfoModel();
    userinfo.UserCode = dr["UserCode"].ToString();
    userinfo.UserPassword = dr["UserPassword"].ToString();

    list.Add(userinfo);
   }
  }

  #endregion
 }
}


DALFactory 如下

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Reflection;

using StuManagementIDAL;

namespace StuManagementDALFactory
{
 public abstract class AbstractDALFactory
 {
  public static AbstractDALFactory ChooseFactory()
  {
   string className = ConfigurationManager.AppSettings["factory"].ToString();
   AbstractDALFactory factory = null;
   factory = (AbstractDALFactory)Assembly.Load("StuManagementDALFactory").CreateInstance("StuManagementDALFactory." + className);
   return factory;
  }

  public abstract IUserInfoService CreateUserInfoService();
  public abstract IStuInfoService CreateStuInfoService();
 }
}
using System;
using System.Collections.Generic;
using System.Text;

using StuManagementDAL.SqlServer;
using StuManagementIDAL;

namespace StuManagementDALFactory
{
 public class SqlServerDALFactory:AbstractDALFactory
 {
  public override StuManagementIDAL.IUserInfoService CreateUserInfoService()
  {
   return new UserInfoService();
  }

  public override StuManagementIDAL.IStuInfoService CreateStuInfoService()
  {
   return new StuInfoService();
  }
 }
}


BLL

using System;
using System.Collections.Generic;
using System.Text;

using StuManagementDALFactory;
using StuManagementIDAL;
using StuManagementModels;

namespace StuManagementBLL
{
 public class UserInfoBLL
 {
  private static AbstractDALFactory factory = AbstractDALFactory.ChooseFactory();
  private static IUserInfoService userinfoService = factory.CreateUserInfoService();

  public bool IsLogin(UserInfoModel userinfo)
  {
   if (userinfoService.GetUserInfoByLogin(userinfo).Count == 1)
    return true;
   else
    return false;
  }
 }
}
 

UI曾就不贴出来了,源代码可以自行下载

下载代码

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

INDEX | 法语 | 希腊语
Powered By Z-Blog 1.8 Devo Build 80201

Copyright 2008-2008 Your WebSite. Some Rights Reserved. 牛B站不用备案