博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
步步为营 .NET三层架构解析 四、Model设计(四种设计方式)
阅读量:6441 次
发布时间:2019-06-23

本文共 6689 字,大约阅读时间需要 22 分钟。

说到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类库了,这是三层架构中的核心部分,下回分解,敬请拍砖.

 


转载于:https://www.cnblogs.com/springyangwc/archive/2011/03/24/1994281.html

你可能感兴趣的文章
Spring @Value注解问题
查看>>
P1886 滑动窗口
查看>>
实施vertex compression所遇到的各种问题和解决办法
查看>>
ubuntu 12.04 rails server 时候报错 execjs
查看>>
linux下文件压缩与解压操作
查看>>
使用树莓派实现微信远程监控
查看>>
在 SQL Server 中查询EXCEL 表中的数据遇到的各种问题
查看>>
linux sed命令
查看>>
浅谈当下网页设计趋势
查看>>
TCP 滑动窗口和 拥塞窗口
查看>>
VS2008调试程序时出现"XXX mutex not created."
查看>>
解决Java连接MySQL存储过程返回参数值为乱码问题
查看>>
c++ 字符检测 TCharacter
查看>>
MalformedObjectNameException: Invalid character '' in value part of property
查看>>
Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
查看>>
android 40 Io编程
查看>>
STL之Vector(不定长数组)
查看>>
Ext江湖
查看>>
一起谈.NET技术,实战ASP.NET大规模网站架构:Web加速器
查看>>
RHEL 6.6下Python 2.6.6升级到Python 3.6.6
查看>>