实践ORM,创建基于Grove的.NET应用程序(二)
撰稿 林学鹏 cnlxp@msn.com
7)在当前项目的EntityDB下添加CustomerDB类,该类负责对Customer的数据库操作,包括添加,删除,修改等操作。
8)重复7,完成AddressDB类。
CustomerDB.cs
using System;
using WebApp1.Entities;
using Grove;
using Grove.DataObject;
public class CustomerDB
{
public CustomerDB()
{
}
public void Insert(Customer c)
{
IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();
oo.BeginTranscation();
try
{
oo.InsertObject(c);
oo.Commit();
}
catch(System.Exception e)
{
oo.Rollback();
throw e;
}
oo.Dispose();
}
public void Delete(Customer c)
{
IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();
oo.BeginTranscation();
try
{
oo.RemoveObject(c);
oo.Commit();
}
catch(System.Exception e)
{
oo.Rollback();
throw e;
}
oo.Dispose();
}
public void Update(Customer c)
{
IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();
oo.BeginTranscation();
try
{
oo.UpdateObject(c);
oo.Commit();
}
catch(System.Exception e)
{
oo.Rollback();
throw e;
}
oo.Dispose();
}
public EntityData SelectAll()
{
IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();
IObjectQuery oq=oo.NewQuery(typeof(Customer));
EntityData customers=new EntityData();
oq.Execute(customers);
oo.Dispose();
return customers;
}
}
AddressDB.cs
using System;
using WebApp1.Entities;
using Grove;
using Grove.DataObject;
public class AddressDB
{
public AddressDB()
{
}
public void Insert(Address a)
{
IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();
oo.BeginTranscation();
try
{
oo.InsertObject(a);
oo.Commit();
}
catch(System.Exception e)
{
oo.Rollback();
throw e;
}
oo.Dispose();
}
public void Delete(Address a)
{
IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();
oo.BeginTranscation();
try
{
oo.RemoveObject(a);
oo.Commit();
}
catch(System.Exception e)
{
oo.Rollback();
throw e;
}
oo.Dispose();
}
public void Update(Address a)
{
IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();
oo.BeginTranscation();
try
{
oo.UpdateObject(a);
oo.Commit();
}
catch(System.Exception e)
{
oo.Rollback();
throw e;
}
oo.Dispose();
}
public EntityData SelectAll()
{
IObjectOperator oo=ObjectOperatorFactory.GetObjectOperator();
IObjectQuery oq=oo.NewQuery(typeof(Address));
EntityData addresses=new EntityData();
oq.Execute(addresses);
oo.Dispose();
return addresses;
}
}
代码2.数据库类