步步为营 .NET三层架构解析 四、Model设计(四种设计方式)
说到Model设计,我们先谈谈它的作用:
Model又叫实体类,model层里面的一个类对应数据库里面的一张表, 类里面的每一个属性对应表里面的一个字段,每个属性都有自己的 GET 和 SET 方法, 项目中的数据存取都要依靠GET和SET方法来实现.确切的说它不属于纵向的哪一层,而是所有层都要用到的业务实体层。其实最主要的作用还是用来传递参数.
下面我就用四种方式来创建Model类:
方式一:
创建department的实体类department.cs:view sourceprint?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:
view sourceprint?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
view sourceprint?public class department
{
public int id
{
set ;
get;
}
public string departname
{
set ;
get ;
}
public string description
{
set;
get ;
}
}
custom.cs:
view sourceprint?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:
view sourceprint?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; }
}
p
补充:Web开发 , ASP.Net ,