情景假定:
需要写个新闻的通类,在不同的项目中可以复用,假定有两张表,一个是新闻类别表,一个是新闻内容表。我用新闻类别表来说明。
类设计如下:
public class NewsCategory
{
private static INewsCategory dalNewsCategory = DataAccess.CreateNewCategoryDAL();
#region 属性
private int _newscategoryid;
private string _newscategorytitle;
private int? _parentid;
private int _rankindex;
private int _language;
/// <summary>
/// 分类ID
/// </summary>
public int NewsCategoryID
{
set { _newscategoryid = value; }
get { return _newscategoryid; }
}
/// <summary>
/// 分类名
/// </summary>
public string NewsCategoryTitle
{
set { _newscategorytitle = value; }
get { return _newscategorytitle; }
}
/// <summary>
/// 父类ID
/// </summary>
public int? ParentID
{
set { _parentid = value; }
get { return _parentid; }
}
/// <summary>
/// 排序种子
/// </summary>
public int RankIndex
{
set { _rankindex = value; }
get { return _rankindex; }
}
/// <summary>
/// 语言版本
/// </summary>
public int Language
{
set { _language = value; }
get { return _language; }
}
#endregion
#region 构造函数
public NewsCategory()
{
}
/// <summary>
/// 根据ID构造
/// </summary>
/// <param name="newsCategoryID"></param>
public NewsCategory(int newsCategoryID)
{
DataRow dr = dalNewsCategory.GetModel(newsCategoryID);
if (dr != null)
{
if (dr["NewsCategoryID"].ToString() != "")
{
_newscategoryid = int.Parse(dr["NewsCategoryID"].ToString());
}
_newscategorytitle = dr["NewsCategoryTitle"].ToString();
if (dr["ParentID"].ToString() != "")
{
_parentid = int.Parse(dr["ParentID"].ToString());
}
if (dr["RankIndex"].ToString() != "")
{
_rankindex = int.Parse(dr["RankIndex"].ToString());
}
if (dr["Language"].ToString() != "")
{
_language = int.Parse(dr["Language"].ToString());
}
}
}
#endregion
#region 方法
/// <summary>
/// 添加类别
/// </summary>
/// <returns></returns>
public int Create()
{
return dalNewsCategory.Create(_newscategorytitle, _parentid, _rankindex, _language);
}
/// <summary>
/// 更新类别
/// </summary>
/// <returns></returns>
public int Update()
{
return dalNewsCategory.Update(_newscategoryid, _newscategorytitle, _parentid, _rankindex, _language);
}
/// <summary>
/// 删除类别
/// </summary>
/// <returns></returns>
public int Delete()
{
return dalNewsCategory.Delete(_newscategoryid);
}
/// <summary>
/// 获取类别列表
/// </summary>
/// <returns></returns>
public DataSet GetList()
{
return dalNewsCategory.GetList("");
}
#endregion
}
{
private static INewsCategory dalNewsCategory = DataAccess.CreateNewCategoryDAL();
#region 属性
private int _newscategoryid;
private string _newscategorytitle;
private int? _parentid;
private int _rankindex;
private int _language;
/// <summary>
/// 分类ID
/// </summary>
public int NewsCategoryID
{
set { _newscategoryid = value; }
get { return _newscategoryid; }
}
/// <summary>
/// 分类名
/// </summary>
public string NewsCategoryTitle
{
set { _newscategorytitle = value; }
get { return _newscategorytitle; }
}
/// <summary>
/// 父类ID
/// </summary>
public int? ParentID
{
set { _parentid = value; }
get { return _parentid; }
}
/// <summary>
/// 排序种子
/// </summary>
public int RankIndex
{
set { _rankindex = value; }
get { return _rankindex; }
}
/// <summary>
/// 语言版本
/// </summary>
public int Language
{
set { _language = value; }
get { return _language; }
}
#endregion
#region 构造函数
public NewsCategory()
{
}
/// <summary>
/// 根据ID构造
/// </summary>
/// <param name="newsCategoryID"></param>
public NewsCategory(int newsCategoryID)
{
DataRow dr = dalNewsCategory.GetModel(newsCategoryID);
if (dr != null)
{
if (dr["NewsCategoryID"].ToString() != "")
{
_newscategoryid = int.Parse(dr["NewsCategoryID"].ToString());
}
_newscategorytitle = dr["NewsCategoryTitle"].ToString();
if (dr["ParentID"].ToString() != "")
{
_parentid = int.Parse(dr["ParentID"].ToString());
}
if (dr["RankIndex"].ToString() != "")
{
_rankindex = int.Parse(dr["RankIndex"].ToString());
}
if (dr["Language"].ToString() != "")
{
_language = int.Parse(dr["Language"].ToString());
}
}
}
#endregion
#region 方法
/// <summary>
/// 添加类别
/// </summary>
/// <returns></returns>
public int Create()
{
return dalNewsCategory.Create(_newscategorytitle, _parentid, _rankindex, _language);
}
/// <summary>
/// 更新类别
/// </summary>
/// <returns></returns>
public int Update()
{
return dalNewsCategory.Update(_newscategoryid, _newscategorytitle, _parentid, _rankindex, _language);
}
/// <summary>
/// 删除类别
/// </summary>
/// <returns></returns>
public int Delete()
{
return dalNewsCategory.Delete(_newscategoryid);
}
/// <summary>
/// 获取类别列表
/// </summary>
/// <returns></returns>
public DataSet GetList()
{
return dalNewsCategory.GetList("");
}
#endregion
}
现在遇到的问题,想不通的是:
1、我需要删除一个类别,调用Delete方法,执行此方法后,数据库中是不存在此记录的,可此时这个实体还是存在。
2、如果我要删除很多个类别,每一次都要实例化一个类,觉得有点多余。
3、如果Delete接受参数的话,是不是有点欠妥。
不知道前辈们会怎样设计,希望可以教教我,大家一起讨论下。
注:其实我这个不是Model