说到Model设计,我们先谈谈它的作用:
Model又叫实体类,model层里面的一个类对应数据库里面的一张表, 类里面的每一个属性对应表里面的一个字段,每个属性都有自己的 GET 和 SET 方法, 项目中的数据存取都要依靠GET和SET方法来实现.确切的说它不属于纵向的哪一层,而是所有层都要用到的业务实体层。其实最主要的作用还是用来传递参数.
下面我就用四种方式来创建Model类:
方式一:
创建department的实体类department.cs:
public class department
{
private int _id;
private string _departname;
private string _description;
public int id
{
set { _id = value; }
get { return _id; }
}
public string departname
{
set { _departname = value; }
get { return _departname; }
}
public string description
{
set { _description = value; }
get { return _description; }
}
}
再来创建custom的实体类custom.cs:
public class custom
{
private int _id;
private string _cname;
private string _ename;
private int _departID;
private int _age;
private string _password;
private string _departname;
public string departname
{
set { _departname = value; }
get { return _departname; }
}
public int id
{
set { _id = value; }
get { return _id; }
}
public string cname
{
set { _cname = value; }
get { return _cname; }
}
public string ename
{
set { _ename = value; }
get { return _ename; }
}
public int departID
{
set { _departID = value; }
get { return _departID; }
}
public int age
{
set { _age = value; }
get { return _age; }
}
public string password
{
set { _password = value; }
get { return _password; }
}
}
实体类我们就建好了,其实也有一种更简化方式,效果和上面是一样的:
方式二:
department.cs
public class department
{
public int id
{
set ;
get;
}
public string departname
{
set ;
get ;
}
public string description
{
set;
get ;
}
}
custom.cs:
public class custom
{
public string departname
{
set ;
get;
}
public int id
{
set ;
get;
}
public string cname
{
set ;
get;
}
public string ename
{
set;
get ;
}
public int departID
{
set;
get ;
}
public int age
{
set;
get;
}
public string password
{
set ;
get;
}
}
也可采用如下这种方式创建:
方式三:
custom.cs:
public class custom
{
private int _id;
private string _cname;
private string _ename;
private int _departID;
private int _age;
private string _password;
private string _departname;
public string departname
{
get { return _departname; }
}
public int id
{
get { return _id; }
}
public string cname
{
get { return _cname; }
}
public string ename
{
get { return _ename; }
}
public int departID
{
get { return _departID; }
}
public int age
{
get { return _age; }
}
public string password
{
get { return _password; }
}
public custom(int ID,string DepartName,string CName,string EName,int DepartID,int Age,string PassWord )
{
_id = ID;
_departname = DepartName;
_cname = CName;
_ename = EName;
_departID = DepartID;
_age = Age;
_password = PassWord;
}
}
department.cs:
public class department
{
private int _id;
private string _departname;
private string _description;
public int id
{
set { _id = value; }
}
public string departname
{
set { _departname = value; }
}
public string description
{
set { _description = value; }
}
public department(int ID,string DepartName,string Description)
{
_id = ID;
_departname = DepartName;
_description = Description;
}
}
还有一种方式就是结合方式一和方式三来设计
方式四:
custom.cs:
public class custom
{
private int _id;
private string _cname;
private string _ename;
private int _departID;
private int _age;
private string _password;
private string _departname;
public string departname
{
set { _departname = value; }
get { return _departname; }
}
public int id
{
set { _id = value; }
get { return _id; }
}
public string cname
{
set { _cname = value; }
get { return _cname; }
}
public string ename
{
set { _ename = value; }
get { return _ename; }
}
public int departID
{
set { _departID = value; }
get { return _departID; }
}
public int age
{
set { _age = value; }
get { return _age; }
}
public string password
{
set { _password = value; }
get { return _password; }
}
public custom()
{ }
public custom(int ID, string DepartName, string CName, string EName, int DepartID, int Age, string PassWord)
{
_id = ID;
_departname = DepartName;
_cname = CName;
_ename = EName;
_departID = DepartID;
_age = Age;
_password = PassWord;
}
}
department.cs:
public class department
{
private int _id;
private string _departname;
private string _description;
public int id
{
set { _id = value; }
get { return _id; }
}
public string departname
{
set { _departname = value; }
get { return _departname; }
}
public string description
{
set { _description = value; }
get { return _description; }
}
public department()
{ }
public department(int ID, string DepartName, string Description)
{
_id = ID;
_departname = DepartName;
_description = Description;
}
}
方式三的赋值和方式一 方式二是不同的。我们采用的是方式一来设计。
其种各种方式的优缺点我就不讲了,代码己说明很清楚了.
Model类建好了,我们就要开始创建DAL类库了,这是三层架构中的核心部分,下回分解,敬请拍砖.