在MVC的结构模型中,数据实体类是很重要的结构之一,也就是数据实体类Data.而数据模型中还有其他类,如业务逻辑类BLL、数据访问类DAL、还有数据集类DataSet。
这4个类一般情况下,是一个数据库表对应一组"类",也就是说,一个数据库表对应一个Data类,一个DataSet类,一个DAL类,BLL类。
下面我将遇到的问题和优化方案归纳如下:
在fotosay项目中,在开发过程中,为了满足需求,改变了数据库表,所以相应的模型就要改变,为了不影响以前开发的代码,于是新建了多个数据实体类Data和数据集类DataSet。譬如PhotoBasicData、PhotoBasicData1,PhotoBasicData2...。 PhotoBasicDataSet,PhotoBasicDataSet1,PhotoBasicDataSet2...这样一个数据表就有了多个模型,给程序带来了较大的冗余,而且不容易维护和修改,代码呈现散乱。
解法1、在项目中有出现,就是利用"继承",代码虽然有减少,但是没有优化到最好!
using System;
namespace Fotosay.Model
{
[Serializable]
public class SystemRecordData2 : SystemRecordData
{
public SystemRecordData2()
{
}
public SystemRecordData2(int recordID, int forumsID, string shortTitle, string recordTitle, int recordType, int commendUserID, int objectID, string picUrl, int userID, string userName, string nickName, int sort, int systemColumnID, string navigateUrl, DateTime createDate)
:base(recordID, forumsID, shortTitle, recordTitle, recordType, commendUserID, objectID, picUrl, userID, userName, nickName)
{
this.sort = sort;
this.systemColumnID = systemColumnID;
this.navigateUrl = navigateUrl;
this.createDate = createDate;
}
}
}
解法2、利用方法的"重载",可以既不改动以前的代码,又可以让程序达到最优化.
而且保证了代码的整洁完美,实现了一个数据库表对应一个Data类,一个DataSet类,一个DAL类,BLL类。
using System;
namespace Fotosay.Model
{
[Serializable]
public class SystemRecordData
{
public SystemRecordData()
{
}
public SystemRecordData(int recordID, int forumsID, string shortTitle, string recordTitle, int recordType, int commendUserID, int objectID, string picUrl, int userID, string userName, string nickName)
{
this.recordID = recordID;
this.forumsID = forumsID;
this.shortTitle = shortTitle;
this.recordTitle = recordTitle;
this.recordType = recordType;
this.commendUserID = commendUserID;
this.objectID = objectID;
this.picUrl = picUrl;
this.userID = userID;
this.userName = userName;
this.nickName = nickName;
}
public SystemRecordData(int recordID, int forumsID, string shortTitle, string recordTitle, int recordType, int commendUserID, int objectID, string picUrl, int userID, string userName, string nickName, int sort, int systemColumnID, string navigateUrl, DateTime createDate)
{
this.recordID = recordID;
this.forumsID = forumsID;
this.shortTitle = shortTitle;
this.recordTitle = recordTitle;
this.recordType = recordType;
this.commendUserID = commendUserID;
this.objectID = objectID;
this.picUrl = picUrl;
this.userID = userID;
this.userName = userName;
this.nickName = nickName;
this.sort = sort;
this.systemColumnID = systemColumnID;
this.navigateUrl = navigateUrl;
this.createDate = createDate;
}
}
}
又待解决问题:如果链表查询的话怎么保证Model代码的整洁统一和高效啊?
可能解决方案:用“解法1”,还是没达到perfect,还有什么高招啊...郁闷中...
关于数据模型Model构架研究
最新推荐文章于 2024-11-09 21:51:58 发布