iBATIS.NET Tips & Tricks(1) : 使用Nullable类型
来源:互联网 发布:二手玫瑰 知乎 编辑:程序博客网 时间:2024/06/06 03:15
在iBatis中,我们面对的通常会是Domain Model,而不是DataSet和DataTable。这样在处理业务逻辑时,就可以不必再关心数据持久相关的东东了。
Domain Model类型的属性常常会对应数据库中表的一个字段(也可能是其它Domain类型的)。比如下面的Product类:
ProductId属性对应表Product的ProductId字段,CategoryId对应CategoryId。为了数据的参照完整性,我们可以将CategoryId设置为外键。
这样问题就来了,如果我们通过new Product()的方式新建一个实例,但没有设置CategoryId的值,那么CategoryId的值实际上为int类型的默认值0,这样在插入数据的时候就会发生外键冲突,因为Category表中不存在主键为0的记录。
另外,如果有类型为DateTime的属性,也会有麻烦。DateTime类型的默认值为0001 年 1 月 1 日午夜 12:00:00,而这不在SQL Server的datetime类型的范围内,在插入数据时也会引发错误。难道我们要手工处理所有这些字段?
幸好还有Nullable类型。如果类型的属性为Nullable类型,那么其默认值将为null。从而省却了上面的烦恼。
我们可将Product类的定义改为:
[Serializable]
public partial class Product
{
#region private fields
private int _productid;
private string _productname = String.Empty;
private int? _categoryid;
private string _description = String.Empty;
#endregion
#region constructors
public Product() { }
public Product(int productid)
{
this._productid = productid;
}
#endregion
#region Public Properties
public int Productid
{
get { return _productid; }
set { _productid = value; }
}
public string Productname
{
get { return _productname; }
set { _productname = value; }
}
public int? CategoryId
{
get { return _categoryid; }
set { _categoryid = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
#endregion
}
public partial class Product
{
#region private fields
private int _productid;
private string _productname = String.Empty;
private int? _categoryid;
private string _description = String.Empty;
#endregion
#region constructors
public Product() { }
public Product(int productid)
{
this._productid = productid;
}
#endregion
#region Public Properties
public int Productid
{
get { return _productid; }
set { _productid = value; }
}
public string Productname
{
get { return _productname; }
set { _productname = value; }
}
public int? CategoryId
{
get { return _categoryid; }
set { _categoryid = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
#endregion
}
注意:实际应用中可能不会使用CategoryId,而是使用Category对象作为属性。
[Serializable]
public partial class Product
{
#region private fields
private int _productid;
private string _productname = String.Empty;
private int _categoryid;
private string _description = String.Empty;
#endregion
#region constructors
public Product() { }
public Product(int productid)
{
this._productid = productid;
}
#endregion
#region Public Properties
public int Productid
{
get { return _productid; }
set { _productid = value; }
}
public string Productname
{
get { return _productname; }
set { _productname = value; }
}
public int CategoryId
{
get { return _categoryid; }
set { _categoryid = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
#endregion
}
public partial class Product
{
#region private fields
private int _productid;
private string _productname = String.Empty;
private int _categoryid;
private string _description = String.Empty;
#endregion
#region constructors
public Product() { }
public Product(int productid)
{
this._productid = productid;
}
#endregion
#region Public Properties
public int Productid
{
get { return _productid; }
set { _productid = value; }
}
public string Productname
{
get { return _productname; }
set { _productname = value; }
}
public int CategoryId
{
get { return _categoryid; }
set { _categoryid = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
#endregion
}
- iBATIS.NET Tips & Tricks(1) : 使用Nullable类型
- Hibernate Tips & Tricks 1
- iBATIS.NET Tips & Tricks(2) : 将image列设置为null值时遇到的Operand type clash异常。
- JavaScript tips and tricks - 1
- jQuery tips and tricks - 1
- Linux bash Tips & Tricks (1)
- Visual Studio .NET Tips and Tricks
- Tips Tricks
- .net 2.0中新增的nullable类型_asp.net使用技巧
- .net 2.0中新增的nullable类型
- .net 2.0中新增的nullable类型
- .net 2.0中新增的nullable类型
- nullable类型
- Nullable类型
- Nullable类型
- Nullable类型
- VB.NET中的值类型、引用类型与Nullable类型
- 在C#中使用Nullable类型
- js大全
- AOP框架汇总
- sphinx4
- Linux I2C核心、总线与设备驱动
- AOP技术研究
- iBATIS.NET Tips & Tricks(1) : 使用Nullable类型
- 学习SQL SERVER的存储过程----之一认识存储过程语法
- Linux登陆问题请教
- @@IDENTITY 是否总能保证插入语句执行后的ID是正确的?
- 带有返回值的oracle 存储过程 大 | 中 | 小
- 一个希腊富翁说他的成功之路
- 突破固有思维方式
- C#开发经验技巧宝典--免费试读
- .NET WinForm程序,用中文输入法输入字符时,重复触发OnKeyPress消息问题