您的位置:首页 > 其它

Castle AR 使用 Nullables 支持初态为 null 的数值属性

2006-09-25 16:19 148 查看
NullableModel.cs

namespace Castle.ActiveRecord.Tests.Model
{
using System;

using Nullables;

[ActiveRecord]
public class NullableModel : ActiveRecordBase
{
private int id;
private NullableInt32 age;
private NullableDateTime completion;
private NullableBoolean accepted;

public NullableModel()
{
}

[PrimaryKey]
public int Id
{
get { return id; }
set { id = value; }
}

[Property]
public NullableInt32 Age
{
get { return age; }
set { age = value; }
}

[Property]
public NullableDateTime Completion
{
get { return completion; }
set { completion = value; }
}

[Property]
public NullableBoolean Accepted
{
get { return accepted; }
set { accepted = value; }
}

public static void DeleteAll()
{
ActiveRecordBase.DeleteAll( typeof(NullableModel) );
}

public static NullableModel[] FindAll()
{
return (NullableModel[]) ActiveRecordBase.FindAll( typeof(NullableModel) );
}

public static NullableModel Find(int id)
{
return (NullableModel) ActiveRecordBase.FindByPrimaryKey( typeof(NullableModel), id );
}
}
}

NullableTestCase.cs

namespace Castle.ActiveRecord.Tests
{
using System;

using NUnit.Framework;

using Castle.ActiveRecord.Tests.Model;

[TestFixture]
public class NullablesTestCase : AbstractActiveRecordTest
{
[SetUp]
public void Init()
{
ActiveRecordStarter.Initialize( GetConfigSource(), typeof(NullableModel) );

Recreate();
}

[Test]
public void Usage()
{
NullableModel model = new NullableModel();
model.Save();

Assert.AreEqual(1, NullableModel.FindAll().Length);

model = NullableModel.FindAll()[0];

Assert.AreEqual(Nullables.NullableInt32.Default, model.Age);
Assert.AreEqual(Nullables.NullableDateTime.Default, model.Completion);
Assert.AreEqual(Nullables.NullableBoolean.Default, model.Accepted);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐