符合OCP原则(开闭原则):对扩展开放,对修改关闭,为了代码重用。
符合SRP原则:单一职责原则,让一个类有且只有一个职责,降低耦合,,便于代码维护,SRP原则被称为“导致类代码的原因”。
运用了DIP原则:模块要依赖于抽象,不要依赖于具体。运用接口或则抽象类,为项目打基础:1.高层模块不应该依赖于低层模块,二者都应该依赖于抽象。2.应该面向接口,而不是面向功能编程,利于基类稳定,又便于扩展,一次封装便增强了代码的内聚性。
1.2 DALFactory(数据工厂文件夹)
//--------------------------数据工厂方法类-------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PhotoBooks.Web.IDAL;
using PhotoBooks.Web.DAL;
namespace PhotoBooks.Web.DALFactory
{
public class DataAccess
{
public static IUser CreateUser()
{
///使用工厂创建User 数据访问对象实例
return (IUser)new User();
}
public static IAffiche CreateAffiche()
{
///使用工厂创建Affiche数据访问对象实例
return (IAffiche)new Affiche();
}
public static ICriticise CreateCriticise()
{
///使用工厂创建Criticise 数据访问对象实例
return (ICriticise)new Criticise();
}
public static IFriend CreateFriend()
{
///使用工厂创建 Friend 数据对象访问实例
return (IFriend)new Friend();
}
public static IMessage CreateMessage()
{
///使用工厂创建Message 数据对象访问实例
return (IMessage)new Message();
}
public static IPhoto CreatePhoto()
{
///使用工厂创建Photo 数据对象访问实例
return (IPhoto)new Photo();
}
public static IPhotoBook CreatePhotoBook()
{
///使用工厂创建PhotoBook 数据对象访问实例
return (IPhotoBook)new PhotoBook("");
}
public static IConfig CreateConfig()
{
return (IConfig)new PhotoBooks.Web.DAL.Config();
}
}
}
1.3 BLL(逻辑业务层文件夹)
//-------------------------------用户业务逻辑类----------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PhotoBooks.Web.IDAL;
using PhotoBooks.Web.DALFactory;
using System.Data;
using PhotoBooks.Web.Model;
using PhotoBooks.Web.DAL;
//逻辑层。
namespace PhotoBooks.Web.BLL
{
public class UserInfoClass
{
//创建数据访问层对象
private static IUser userInfo = DataAccess.CreateUser();
public int userID;
//获得登录用户ID
public int LoginUserID(int userid)
{
userID=userid;
return userID;
}
/// <summary>
/// 逻辑层添加用户方法
/// </summary>
/// <param name="userName"></param>
/// <param name="userPass"></param>
/// <returns></returns>
public int AddUser(string userName, string userPass,string sex,string email,
string homepage, string QQ, string MSN, string usercontent)
{
//逻辑层对表示层数据进行处理
if (userName.Equals(""))
return 0;
if (userPass.Equals(""))
return 0;
//调用数据访问层,添加用户
return userInfo.AddUser(userName,userPass,sex,email,homepage,
QQ, MSN,usercontent);
}
//查找用户名是否存在
public int CheckUname(string name)
{
return userInfo.CheckUname(name);
}
/// <summary>
/// 得到所有用户信息
/// </summary>
/// <returns></returns>
public List<UserInfo> GetAllUserInfo()
{
return userInfo.GetAllUserInfo();
}
//用户登录
public int UserLogin(string name,string pwd)
{
if (name.Equals(""))
return -1;
if (pwd=="")
return -1;
return userInfo.UserLogin(name, pwd);
//调用逻辑层方法
}
/// <summary>
/// 用户头像,等级
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public DataTable InfoUser(int userid)
{
return userInfo.InfoUser(userid);
}
/// <summary>
/// 母板页头像,用户名
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public DataTable MasterUser(int userid)
{
return userInfo.MasterUser(userid);
}
/// <summary>
/// 用户所有信息
/// </summary>
/// <returns></returns>
public List<UserInfo> ActivityUserW()
{
return userInfo.ActivityUserW();
}
/// <summary>
/// 根据用户ID获得用户角色
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public int UserRolse(int userid)
{
return userInfo.UserRolse(userid);
}
/// <summary>
/// 根据用户ID获得用户名
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public string UserName(int userid)
{
return userInfo.UserName(userid);
}
//修改用户信息
public bool AmandUserInfo(string userName, string sex, string email,
string homepage, string QQ, string MSN, string usercontent,int userID)
{
return userInfo.AmandUserInfo(userName, sex, email,
homepage, QQ, MSN, usercontent, userID);
}
//验证原密码是否正确
public int CheckUnamePassWord(string oldPassWord, int userID)
{
return userInfo.CheckUnamePassWord(oldPassWord, userID);
}
//修改密码
public int AmandUserPassWord(string newPassWord, int userID)
{
return userInfo.AmandUserPassWord(newPassWord, userID);
}
//设置忘记密码问题提示
public bool PassWordRegister(int QuestionIndex, string RegisterResult, int userId)
{
return userInfo.PassWordRegister(QuestionIndex, RegisterResult, userId);
}
//忘记密码问题提示
public string ForgetPassWordRegister(int QuestionIndex, string RegisterResult, string userName)
{
return userInfo.ForgetPassWordRegister(QuestionIndex, RegisterResult, userName);
}
//查看在线状态
public int OnlienState(int userId)
{
return userInfo.OnlienState(userId);
}
//添加在线状态
public bool AddOnlienState(int userId)
{
return userInfo.AddOnlienState(userId);
}
public DataTable UserListBind(int userid)
{
User UserBind = new User();
return UserBind.Bind(userid);
}
}
}
BLL文件夹内还有其他业务逻辑类(省略……)
1.4 IDAL文件内抽象接口:
//-------------------------------用户信息接口类---------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PhotoBooks.Web.DAL;
using PhotoBooks.Web.Model;
using System.Data;
namespace PhotoBooks.Web.IDAL
{
//数据访问层,具体的数据库操作
//IUser,与User相关的数据操作
public interface IUser
{
/// <summary>
/// 在数据库中
/// </summary>
/// <param name="userName"></param>
/// <param name="userPass"></param>
/// <returns></returns>
int AddUser(string userName, string userPass,string sex,string email, string homepage, string QQ, string MSN, string usercontent);
List<UserInfo> GetAllUserInfo();
//用户登录
int UserLogin(string username, string userpwd);
//查找用户名是否存在
int CheckUname(string name);
//用户头像和姓名
DataTable InfoUser(int userid);
// 母板页头像,用户名
DataTable MasterUser(int userid);
// 用户所有信息
List<UserInfo> ActivityUserW();
// 根据用户ID获得用户角色
int UserRolse(int userid);
/// 根据用户ID获得用户名
string UserName(int userid);
//修改用户信息
bool AmandUserInfo(string userName, string sex, string email,
string homepage, string QQ, string MSN, string usercontent, int userID);
//验证原密码是否正确
int CheckUnamePassWord(string oldPassWord, int userID);
//修改密码
int AmandUserPassWord(string newPassWord,int userID);
//设置忘记密码问题提示
bool PassWordRegister(int QuestionIndex, string RegisterResult, int userId);
//忘记密码问题提示
string ForgetPassWordRegister(int QuestionIndex, string RegisterResult, string userName);
//查看在线状态
int OnlienState(int userId);
//添加在线状态
bool AddOnlienState(int userId);
//修改用户信息绑定
DataTable Bind(int userid);
}
}
1.5 DLL文件内数据层:
//-------------------------------用户信息数据层类--------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using PhotoBooks.Web.IDAL;
using PhotoBooks.Web.Model;
using PhotoBooks.Web.SqlDB;
namespace PhotoBooks.Web.DAL
{
//数据访问层,具体的数据库操作,User,与User相关的数据操作
public class User : PhotoBooks.Web.IDAL.IUser
{
SqlDb sql;
//数据库增删查改操作……
/// <summary>
/// 构造函数
/// </summary>
public User()
{
// SqlConnection sqlconn;
}
/// <summary>
/// 获得用户所有信息
/// </summary>
/// <returns></returns>
public List<UserInfo> GetAllUserInfo()
{
try
{
List<UserInfo> userList = new List<UserInfo>();
sql = new SqlDb();
DataTable dt = sql.GetTable("select * from usersinformation");
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
UserInfo user = new UserInfo((int)dt.Rows[i][0], (string)dt.Rows[i][1], (string)dt.Rows[i][2], (int)dt.Rows[i][3], (string)dt.Rows[i][4], (int)dt.Rows[i][5], (string)dt.Rows[i][6], (string)dt.Rows[i][7], (string)dt.Rows[i][8], (string)dt.Rows[i][9], (string)dt.Rows[i][10], (DateTime)dt.Rows[i][11], (DateTime)dt.Rows[i][12]);
userList.Add(user);
}
return userList;
}
return null;
}
catch
{
return null;
}
}
/// <summary>
/// 根据用户ID获得用户名
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public string UserName(int userid)
{
try
{
sql = new SqlDb();
return (string)sql.ExcuteSqlRetVal("select username from usersinformation where userid=" + userid);
}
catch
{
return "";
}
}
/// <summary>
/// 获得用户的头像和等级
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public DataTable InfoUser(int userid)
{
try
{
sql = new SqlDb();
return sql.GetTable("select userroleid,sex,headportrait from usersinformation where userid=" + userid);
}
catch
{
return null;
}
}
public int ConActivityN()
{
try
{
sql = new SqlDb();
return (int)sql.ExcuteSqlRetVal("select activityusern from config ");
}
catch
{
return 1;
}
}
/// <summary>
/// 用户所有信息
/// </summary>
/// <returns></returns>
public List<UserInfo> ActivityUserW()
{
try
{
sql = new SqlDb();
List<UserInfo> userList = new List<UserInfo>();
int actiN = ConActivityN();
DataTable dt = sql.GetTable("select top " + actiN + " * from usersinformation order by lastlogintime desc");
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
UserInfo user = new UserInfo((int)dt.Rows[i][0], (string)dt.Rows[i][1], (string)dt.Rows[i][2], (int)dt.Rows[i][3], (string)dt.Rows[i][4], (int)dt.Rows[i][5], (string)dt.Rows[i][6], (string)dt.Rows[i][7], (string)dt.Rows[i][8], (string)dt.Rows[i][9], (string)dt.Rows[i][10], (DateTime)dt.Rows[i][11], (DateTime)dt.Rows[i][12]);
userList.Add(user);
}
return userList;
}
return null;
}
catch
{
return null;
}
}
/// <summary>
/// 根据用户ID获得用户角色
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public int UserRolse(int userid)
{
try
{
sql = new SqlDb();
return (int)sql.ExcuteSqlRetVal("select userroleid from usersinformation where userid=" + userid);
}
catch
{
return 1;
}
}
/// <summary>
/// 母板页头像,用户名
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public DataTable MasterUser(int userid)
{
try
{
sql = new SqlDb();
return sql.GetTable("select username,sex,headportrait from usersinformation where userid=" + userid);
}
catch
{
return null;
}
}
#region IUser 成员
/// <summary>
/// 用户登录
/// </summary>
/// <param name="username"></param>
/// <param name="userpwd"></param>
/// <returns></returns>
public int UserLogin(string username, string userpwd)
{
string str = "select userID from UsersInformation where username='" + username + "' and userPassWord='" + userpwd + "'";
sql = new SqlDb();
return (int)sql.ExcuteSqlRetVal(str);
}
#endregion
#region 添加用户
/// <summary>
/// 添加用户
/// </summary>
/// <param name="userName"></param>
/// <param name="userPass"></param>
/// <param name="sex"></param>
/// <param name="email"></param>
/// <param name="homepage"></param>
/// <param name="QQ"></param>
/// <param name="MSN"></param>
/// <param name="usercontent"></param>
/// <returns></returns>
public int AddUser(string userName, string userPass,string sex,string email, string homepage, string QQ, string MSN, string usercontent)
{
if (CheckUname(userName) == 0)
{
sql = new SqlDb();
string sqlstr = "insert into UsersInformation (userName,userPassWord,userRoleId,sex,headportrait,email,homepage";
sqlstr += ",qq,msn,usercon,registerTime,lastLoginTime) values('" + userName + "','" + userPass + "'," + 2 + ",'" + sex + "',"+0+",'" + email + "','" + homepage + "','" + QQ + "','" + MSN + "','"+usercontent+"','" + DateTime.Now.Date + "','" + DateTime.Now.Date + "')";
if (sql.ExcuteStrNonRet(sqlstr))
{
return (int)sql.ExcuteSqlRetVal("select userid from usersinformation where username='" + userName + "'");
}
else
{
return 0;
}
}
return -1;
}
//查找用户名是否存在
public int CheckUname(string name)
{
try
{
sql = new SqlDb();
return (int)sql.ExcuteSqlRetVal("select userid from usersinformation where username='" + name + "'");
}
catch
{
return 0;
}
}
#endregion
#region 修改用户信息
//修改用户信息
public bool AmandUserInfo(string userName, string sex, string email,
string homepage, string QQ, string MSN, string usercontent, int userID)
{
try
{
sql = new SqlDb();
string sqlstr = "UPDATE UsersInformation SET userName = '" + userName + "', sex='" + sex + "',email='" + email + "',homepage='" + homepage + "'";
sqlstr += ",qq= '" + QQ + "',msn= '" + MSN + "',usercon ='" + usercontent + "' WHERE userID = '" + userID + "'";
return sql.ExcuteStrNonRet(sqlstr);
}
catch
{
return false ;
}
}
//修改用户信息绑定
public DataTable Bind(int userid)
{
string str = "SELECT * from UsersInformation where userid=" + userid;
sql = new SqlDb();
return sql.GetTable(str);
}
//验证原密码是否正确
public int CheckUnamePassWord(string oldPassWord, int userID)
{
try
{
sql = new SqlDb();
return (int)sql.ExcuteSqlRetVal("select * from usersinformation where userid='" + userID + "' and userPassWord='" + oldPassWord + "'");
}
catch
{
return 0;
}
}
//修改密码
public int AmandUserPassWord(string newPassWord, int userID)
{
try
{
sql = new SqlDb();
string sqlstr = "UPDATE UsersInformation SET userPassWord = '" + newPassWord + "' WHERE userID = '" + userID + "'";
if (sql.ExcuteStrNonRet(sqlstr))
{//////////////////////////////////////////尽量简化
return (int)sql.ExcuteSqlRetVal("select * from usersinformation where userPassWord = '" + newPassWord + "' and userID = '" + userID + "'");
}
else
{
return 0;
}
}
catch
{
return 0;
}
}
#endregion
//设置忘记密码问题提示
public bool PassWordRegister(int QuestionIndex, string RegisterResult, int userId)
{
try
{
sql = new SqlDb();
string sqlstr = "UPDATE PasswordRegister SET passWordQuestion= " + QuestionIndex + ",passWordResult= '" + RegisterResult + "' WHERE userId = " + userId;
return sql.ExcuteStrNonRet(sqlstr);
}
catch
{
return false;
}
}
//忘记密码问题提示
public string ForgetPassWordRegister(int QuestionIndex, string RegisterResult, string userName)
{
try
{
sql = new SqlDb();
string sqlstr = "SELECT SC1.userPassWord FROM usersinformation AS SC1 , PasswordRegister AS SC2 WHERE SC1.userId=SC2.userId AND SC2.passWordQuestion= " + QuestionIndex + " AND SC2.passWordResult= '" + RegisterResult + "' AND SC1.userName ='" + userName+"'";
return (string )sql.ExcuteSqlRetVal(sqlstr);
}
catch
{
return "";
}
}
//查看在线状态
public int OnlienState(int userId)
{
try
{
sql = new SqlDb();
string sqlstr = "SELECT onLineState FROM usersinformation WHERE userId=" + userId ;
return (int)sql.ExcuteSqlRetVal(sqlstr);
}
catch
{
return 0;
}
}
//添加在线状态
public bool AddOnlienState(int userId)
{
try
{
sql = new SqlDb();
string sqlstr = "UPDATE UsersInformation SET onLineState = 1 WHERE userID = " + userId;
return sql.ExcuteStrNonRet(sqlstr);
}
catch
{
return false ;
}
}
#region IUser 成员
public int AddUser(string userName, string userPass, int userRoleId, string sex, string photoID, string email, string homepage, string QQ, string MSN, string usercontent, DateTime registerTime, DateTime lastLoginTime)
{
throw new NotImplementedException();
}
#endregion
}
}
//-------------------------------其他信息数据层类(略)---------------------------//
1.6 SqlDB文件夹内:
//---------------------数据底层类----------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Collections;
using System.Data;
using System.Configuration;
namespace PhotoBooks.Web.SqlDB
{
public class SqlDb
{
SqlCommand comm;
SqlConnection sqlconn;
SqlDataReader reader;
SqlDataAdapter dap;
public SqlDb()
{//连接数据库字符串
sqlconn = new SqlConnection("Data Source=10.10.5.33;Initial Catalog=PhotoBooksDB;Integrated Security=True"); //sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["OfficeAnywhereSqlServer"].ToString());
}
public DateTime GetDate()
{
return (DateTime)ExcuteSqlRetVal("select GetDate()");
}
/// <summary>
/// 执行一条SQL命令,返回是否执行成功
/// </summary>
/// <param name="str">SQL语句</param>
/// <returns></returns>
public bool ExcuteStrNonRet(string str)
{
comm = new SqlCommand(str, sqlconn);
try
{
sqlconn.Open();
if (comm.ExecuteNonQuery() >= 1)
return true;
else
return false;
}
catch
{
return false;
}
finally
{
sqlconn.Close();
}
}
/// <summary>
/// 返回一个值
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public object ExcuteSqlRetVal(string Str)
{
object ret;
comm = new SqlCommand(Str, sqlconn);
try
{
sqlconn.Open();
ret = comm.ExecuteScalar();
return ret;
}
catch
{
return "";
}
finally
{
sqlconn.Close();
}
}
/// <summary>
/// 返回一张表
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public DataTable GetTable(string Str)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter Dadp = new SqlDataAdapter(Str, sqlconn);
try
{
sqlconn.Open();
Dadp.Fill(ds);
dt = ds.Tables[0];
return dt;
}
catch
{
return null;
}
finally
{
sqlconn.Close();
}
}
public DataTable PaginationTable(int currentpage, int pagesize, string table, string str)
{
try
{
int starpage;
starpage = currentpage * pagesize;
SqlDataAdapter sdap = new SqlDataAdapter(str, sqlconn);
DataSet ds = new DataSet();
sdap.Fill(ds, starpage, pagesize, table);
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
sqlconn.Close();
}
}
}
}