Snake.Net 框架中的ORM(三) (Version 0.2 Beta)

5.CRUD操作
 CRUD(即Create, Retrieve, Update, Delete方法)是数据库的基本操作,很多程序员早期可能一直重复着这些简单后又枯草的工作。ORM最大的贡献就是把程序员从这种状态中解脱出来,而把精力集中于业务实体对象的设计。
 (1)实体类的创建、更新和删除
 Snake.Net通过ObjectManager对象实现与数据库的持久工作, ObjectManager使用Save和Delete方法实现业务实体对象的创建、更新和删除。请看下列代码:

// declare
Customer customer;

customer 
=   new  Customer( " ZZCFR " );
customer.CompanyName 
=   " Ana Trujillo Emparedados y helados " ;
customer.ContactName 
=   " Antonio Moreno " ;
customer.ContactTitle 
=   " Owner " ;
customer.Address 
=   " Forsterstr. 57 " ;
customer.City 
=   " Berlin " ;
customer.PostalCode 
=   " T2F 8M4 " ;
customer.Country 
=   " Canada " ;
customer.Phone 
=   " (604) 555-4729 " ;

using (IObjectManager om
    
=  ObjectManager.Create( typeof (Customer))) {
    om.Save(customer);
}


using (IObjectManager om 
=  ObjectManager.Create( typeof (Customer))) {
    customer 
= om.Retrieve(1as Customer;
    om.Delete(customer);
}


(2)使用存储过程来实现创建、更新和删除
默认情况下,Snake.Net根据业务实体对象与数据表结构的映射,自动生成SQL语句,并进行操作。但是根据需要,用户可以通过配置文件利用存储过程,实现创建、更新和删除操作。参见下面的配置节点,Snake.Net可以通过设置insertProcedure, updateProcedure和deleteProcedure三个配置节点,用于执行指定的存储过程操作。
< object  name ="Eastasp.Enterprise.Security.Authorization.Session"  groupName ="enterprise.main" >
    
< insertProcedure  name ="usp_SessionInsert"  params ="-all-" />
</ object >

图 5-2-1 配置存储过程名称

(3)事务处理
Snake.Net有两种方法实现事务处理,一种是使用ObjectManager的事务处理方法:BeginTransaction、Commit和 Abort,请参见以下代码:

// declare
Category c1, c2;

c1 
=   new  Category();
c1.CategoryName 
=   " Seafood1 " ;
c1.Description 
=   " Seaweed and fish " ;

c2 
=   new  Category();
c2.CategoryName 
=   " Seafood2 " ;
c2.Description 
=   " Seaweed and fish " ;

using (IObjectManager om
            
=  ObjectManager.Create( typeof (Category))) {

    Try
{
        om.BeginTransaction();
        om.Save(c1);
        om.Save(c2);
        om.Commit();
    }

    
catch(Exception ex)
{
        om.Abort();
        
throw ex;
    }

}


另一种方法是使用
TransactionContext 类,调用事务环境进行事务处理,请参见以下代码:
// declare
Shipper shipper;
Customer customer;
IObjectManager om1, om2;

shipper 
=   new  Shipper();
shipper.CompanyName 
=   " Speedy Express " ;
shipper.Phone 
=   " (503) 555-9831 " ;

customer 
=   new  Customer( " ZDEFR " );
customer.CompanyName 
=   " B¨®lido Comidas preparadas " ;
customer.ContactName 
=   " na Trujillo " ;
customer.ContactTitle 
=   " Owner " ;
customer.Address 
=   " Forsterstr. 57 " ;
customer.City 
=   " Berlin " ;
customer.PostalCode 
=   " T2F 8M4 " ;
customer.Country 
=   " Canada " ;
customer.Phone 
=   " (604) 555-4729 " ;

using (TransactionContext tc  =
            TransactionContext.Create())
{
    om1 
= ObjectManager.Create(typeof(Shipper));
    om2 
= ObjectManager.Create(typeof(Customer));
    
    om1.Save(shipper);
    om2.Save(customer);

    tc.Consistent 
= true;

(4)接受业务实体
Snake.Net可以通过ObjectManager的Retrieve和List方法用于接受业务实体对象,前者可根据关键字接受单个的业务实体,后者可以根据一组关键字接受一组业务实体。请参看以下代码:
Customer customer;

using (IObjectManager om 
            
=  ObjectManager.Create( typeof (Customer))) {
    customer 
= om.Retrieve(1as Customer;
}

你可能感兴趣的:(version)