概述
设备管理系统设计和实现
从1月份到3月初,一直在做一个系统,之前做项目都不考虑什么架构,直接把SQL写到cs文件中,后来学会了分层架构,我想以后如果要做类似的网站肯定就这样做了。
1、 架构设计,采用和微软经典的petshop架构类似的分层方式,但是考虑到不需要针对多类数据库,而且也不需要考虑过于复杂的设计,所以就简单进行这样分层
2、 Model说明,model表示数据实体,一个类与数据库中的一张表是相对应的,考虑到代码复用,这里有一个基类baseinfo,其他的类均继承该基类
BaseInfo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VideoWorks.ITmanage.Model
{
public class BaseModel
{
public int ID;
//等待插入时需要的表名
virtual public string TableName
{
get { return ""; }
}
//插入时的字段
virtual public string Filed
{
get { return ""; }
}
//等待插入时需要的值
virtual public string Values
{
get { return ""; }
}
//SQL中的where条件
virtual public string Where
{
get { return ""; }
}
//更新数据时sql语句中的匹配对
virtual public string Filedpair
{
get { return ""; }
}
//查询数据时sql语句中的查询结果字段,含有中文
virtual public string Queryfiled
{
get { return ""; }
}
}
}
派生类CharacterInfo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VideoWorks.ITmanage.Model
{
public class CharacterInfo : BaseModel
{
//数据成员
private string name;
private string privilege;
private int deleteflag; //对应数据库中字段, 为1表示删除,0表示正常
//默认构造函数
public CharacterInfo()
{
}
//带参构造函数
public CharacterInfo(string name,string privilege,int deleteflag)
{
this.name = name;
this.privilege = privilege;
this.deleteflag = deleteflag;
}
//get set函数
public string Name
{
get { return name; }
set { name = value; }
}
public string Privilege
{
get { return privilege; }
set { privilege = value; }
}
public int Deleteflag
{
get { return deleteflag; }
set { deleteflag = value; }
}
//重载BaseModel中的成员函数
override public string TableName
{
get { return "`character`"; }
}
override public string Values
{
get
{
return Name+"','"+Privilege+"','"+Deleteflag;
}
}
override public string Filed
{
get
{
return "(name,privilege,deleteflag)";
}
}
public override string Filedpair
{
get
{
return "name='" + Name + "',privilege='" + Privilege+"', deleteflag="+Deleteflag;
}
}
public override string Queryfiled
{
get
{
return "ID as 编号, name as 角色类型, privilege as 权限";
}
}
}
}
3、DAL层,DAL层是直接与mysql数据库交互的层次,为BLL层提供接口,调用Model实体,同样每一个类对应数据库中的一张表,这里也有基类BaseDAL
BaseDAL:其中提供的是基本的操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using VideoWorks.ITmanage.Model;
namespace VideoWorks.ITmanage.DAL
{
public class BaseDAL
{
//添加数据
public bool Add(BaseModel model)
{
string sql = "insert into " + model.TableName + model.Filed + " values('" + model.Values + "')";
MySqlHelper.ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text, sql, null);
return true;
}
//删除数据,条件为ID
public bool Delete(BaseModel model)
{
string sql="delete from "+model.TableName+" where ID="+model.ID;
MySqlHelper.ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text, sql, null);
return true;
}
//删除数据其实是更新数据表中的deleteflag字段,deleteflag为1表示已经删除
public bool Deleteflag(BaseModel model)
{
string sql = "update " + model.TableName + " set deleteflag=1 where ID=" + model.ID;
MySqlHelper.ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text, sql, null);
return true;
}
//更新数据,条件为ID
public bool Update(BaseModel model)
{
string sql = "update " + model.TableName + " set " + model.Filedpair + " where ID=" + model.ID;
MySqlHelper.ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text, sql, null);
return true;
}
//查询数据,条件为ID
public DataSet Query(BaseModel model)
{
string sql = "select " + model.Queryfiled + " from " + model.TableName + " where ID=" + model.ID;
return MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, sql, null);
}
}
}
派生类CharacterDAL,这里可以设置用户自定义的函数,实现更加高级的功能:
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using VideoWorks.ITmanage.Model;
namespace VideoWorks.ITmanage.DAL
{
public class FilterCharacter
{
public string Keyword{get;set;}
public int Deleteflag { get; set; }
}
/// <summary>
///Character 的摘要说明,BaseDAL基类中已经提供了对数据表的基本操作,并且都是以ID为关键字
/// </summary>
public class CharacterDAL: BaseDAL
{
public CharacterDAL()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//查询角色信息,拓展的查询条件
public DataSet Query(FilterCharacter filter,BaseModel model)
{
string sql = "select " + model.Queryfiled + " from " + model.TableName+" where ";
if(filter.Keyword !=null)
{
sql += " name like '%" + filter.Keyword + "%' and ";
}
if(filter.Deleteflag!=-1)
{
sql += " deleteflag=" + filter.Deleteflag + " and ";
}
sql += " 1=1";
return MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, sql, null);
}
}
}
4、BLL层,实现web界面和DAL层的通信,其中包括各种处理逻辑,是程序中考虑最多的地方,这里是一个类对应一个功能模块,不过考虑到程序也是按照表进行划分的,故也是按照DAL层对应划分了
CharacterBLL,实现角色功能模块的管理:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using VideoWorks.ITmanage.DAL;
using VideoWorks.ITmanage.Model;
namespace VideoWorks.ITmanage.BLL
{
public class CharacterBLL
{
CharacterDAL characterop = new CharacterDAL();
//添加新角色
public bool Add(CharacterInfo character)
{
return characterop.Add(character);
}
//更新角色
public bool Update(CharacterInfo character)
{
return characterop.Update(character);
}
//删除角色,其实是设置标志字段
public bool Delete(CharacterInfo character)
{
return characterop.Deleteflag(character);
}
//查询角色
public DataSet Query(FilterCharacter filter,CharacterInfo character)
{
return characterop.Query(filter, character);
}
//返回角色列表,添加用户时使用
public DataSet Showlist(int flag)
{
CharacterInfo character = new CharacterInfo();
FilterCharacter filter = new FilterCharacter();
filter.Deleteflag = flag;
return characterop.Query(filter, character);
}
//依据ID返回角色类型
public string Getname(int ID)
{
CharacterInfo character = new CharacterInfo();
character.ID = ID;
return characterop.Query(character).Tables[0].Rows[0]["角色类型"].ToString();
}
//依据角色类型返回ID
public int GetID(string name,int flag)
{
CharacterInfo character = new CharacterInfo();
FilterCharacter filter = new FilterCharacter();
filter.Keyword = name;
filter.Deleteflag = flag;
return Convert.ToInt32(characterop.Query(filter,character).Tables[0].Rows[0]["编号"].ToString());
}
//依据下拉框填充字符串
public DataSet Fill(int index)
{
CharacterInfo character = new CharacterInfo();
character.ID = index;
return characterop.Query( character);
}
//判断用户是否具有用户管理权限
public bool isuser(string name)
{
CharacterInfo character = new CharacterInfo();
FilterCharacter filter = new FilterCharacter();
filter.Keyword = name;
filter.Deleteflag = 0;
string right = characterop.Query(filter,character).Tables[0].Rows[0]["权限"].ToString();
if(right[0]=='1')
{
return true;
}
else
{
return false;
}
}
//判断用户是否具有职责管理权限
public bool isresponse(string name)
{
CharacterInfo character = new CharacterInfo();
FilterCharacter filter = new FilterCharacter();
filter.Keyword = name;
filter.Deleteflag = 0;
string right = characterop.Query(filter,character).Tables[0].Rows[0]["权限"].ToString();
if (right[2] == '1')
{
return true;
}
else
{
return false;
}
}
//判断用户是否具有设备管理权限
public bool ismachine(string name)
{
CharacterInfo character = new CharacterInfo();
FilterCharacter filter = new FilterCharacter();
filter.Keyword = name;
filter.Deleteflag = 0;
string right = characterop.Query(filter,character).Tables[0].Rows[0]["权限"].ToString();
if (right[1] == '1')
{
return true;
}
else
{
return false;
}
}
//判断用户是否具有运维管理权限
public bool isoperation(string name)
{
CharacterInfo character = new CharacterInfo();
FilterCharacter filter = new FilterCharacter();
filter.Keyword = name;
filter.Deleteflag = 0;
string right = characterop.Query(filter,character).Tables[0].Rows[0]["权限"].ToString();
if (right[3] == '1')
{
return true;
}
else
{
return false;
}
}
}
}
5、web界面
界面中比较简单,只需要调用BLL和model即可,BLL会调用DAL进行数据操作
opcharacter.cs,这里只列举一下简单的用法:
//添加角色
public bool Add()
{
if (txtname.Text.Trim() == "")
{
lblmessage.Text = "角色名称不能为空";
lblmessage.Visible = true;
return false;
}
else
{
string privilege = "";
privilege += (cbxprivilege.Items[0].Selected == true) ? "1" : "0";
privilege += (cbxprivilege.Items[1].Selected == true) ? "1" : "0";
privilege += (cbxprivilege.Items[2].Selected == true) ? "1" : "0";
privilege += (cbxprivilege.Items[3].Selected == true) ? "1" : "0";
CharacterInfo character = new CharacterInfo(txtname.Text.Trim(), privilege, 0);
CharacterBLL characterop = new CharacterBLL();
if (characterop.Add(character) == true)
{
lblmessage.Text = "角色添加成功";
lblmessage.Visible = true;
return true;
}
else
{
lblmessage.Text = "角色添加失败";
lblmessage.Visible = true;
return false;
}
}
}
//修改角色
public bool Update(int ID)
{
if (txtname.Text.Trim() == "")
{
lblmessage.Text = "角色名称不能为空";
lblmessage.Visible = true;
return false;
}
else
{
string privilege = "";
privilege += (cbxprivilege.Items[0].Selected == true) ? "1" : "0";
privilege += (cbxprivilege.Items[1].Selected == true) ? "1" : "0";
privilege += (cbxprivilege.Items[2].Selected == true) ? "1" : "0";
privilege += (cbxprivilege.Items[3].Selected == true) ? "1" : "0";
CharacterBLL characterop = new CharacterBLL();
CharacterInfo character = new CharacterInfo(txtname.Text.Trim(), privilege, 0);
character.ID = ID;
if (characterop.Update(character) == true)
{
lblmessage.Text = "更新角色成功";
lblmessage.Visible = true;
return true;
}
else
{
lblmessage.Text = "更新角色失败";
lblmessage.Visible = true;
return false;
}
}
}
最后的工程结构如图:
这个项目就这么做完了,学到不少东西,其中好多代码都能够复用,例如BaseInfo,BaseDAL,以后遇到类似的就这样做了。
最后
以上就是喜悦店员为你收集整理的设备管理系统设计和实现的全部内容,希望文章能够帮你解决设备管理系统设计和实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复