自己动手写ORM框架(一):目标效果预览

以前还在大学中学习java的时候,就学着使用Hibernate进行持久化层的操作。当时只是觉得这框架很神奇,能够不写SQL语句就能操作数据库。当时也不知道其内部原来是什么,就只知道怎么去用,怎么去写XML配置文件。毕竟当时来讲,代码量有限,接触的编程思想也不多,也没有办法去做深入的研究。最近一段时间以来一直在做.net的开发,其ORM框架有微软自带的Framework,当然也有从Hibernate演化而来的.net平台下的NHibernate. 最近学东西,就很好奇其内部的转换过程。恰巧在博客园中,很幸运到读到了“奋斗”前辈关于自己动手写ORM的一系列文章,用C#完成。特此转载,希望大家一起学习。以下是转载内容,这一篇主要是描述最终的框架效果,以后会一步一步的去用代码实现这个框架。

 最终实现后达到的效果,只需写少量代码就可实现CURD操作。

DAL层代码:

StudentDAL代码
复制代码
public   class  StudentDAL
    {
        EntityManager entityManager 
=  EntityManagerFactory.CreateEntityManager();

        
public  StudentDAL() { }
        
public  StudentDAL(IDbTransaction transaction)
        {
            entityManager.Transaction 
=  transaction;
        }

        
public  List < StudentEntity >  FindAll()
        {
            
return  entityManager.FindAll < StudentEntity > ();
        }
        
        
public   int  Save(StudentEntity entity)
        {
            
return  entityManager.Save(entity);
        }

        
public   int  Update(StudentEntity entity)
        {
            
return  entityManager.Update(entity);
        }

        
public   int  Remove(StudentEntity entity)
        {
            
return  entityManager.Remove(entity);
        }

        
public   int  Remove( object  id)
        {
            
return  entityManager.Remove < StudentEntity > (id);
        }               

        
public   List < StudentEntity >  FindById( object  id)
        {
            
return  entityManager.FindById < StudentEntity > (id);
        }

        
public   List < StudentEntity >  FindByProperty( string  propertyName, object  value)
        {
            
return  entityManager.FindByProperty < StudentEntity > (propertyName, value);
        }
    }
复制代码

实体类与数据库表的映射关系配置:

StudentEntity代码
复制代码
using  System;
using  System.Data;
using  System.Collections.Generic;
using  System.Text;
using  System.Orm.CustomAttributes;

namespace  Entity
{
    [Serializable]
    [Table(name
= " Student " )]
    
public   class  StudentEntity
    {
        
private   string  stuid;
        
private   string  stuno;
        
private   string  name;
        
private   int  sex;
        
private   int  age;
        
private   string  address;
        
private   string  telphone;

        [Id(Name=”stuid”,Strategy = GenerationType.SEQUENCE)]

        
public   string  Stuid
        {
            
get  {  return  stuid; }
            
set  { stuid  =  value; }
        }

        [Column(Name 
=   " studentno " )]
        
public   string  Stuno
        {
            
get  {  return  stuno; }
            
set  { stuno  =  value; }
        }

        [Column(IsInsert 
=   true )]
        
public   string  Name
        {
            
get  {  return  name; }
            
set  { name  =  value; }
        }

        [Column(IsUpdate 
=   true )]
        
public   int  Sex
        {
            
get  {  return  sex; }
            
set  { sex  =  value; }
        }

        
public   int  Age
        {
            
get  {  return  age; }
            
set  { age  =  value; }
        }
        
        
public   string  Address
        {
            
get  {  return  address; }
            
set  { address  =  value; }
        }
        
        
public   string  Telphone
        {
            
get  {  return  telphone; }
            
set  { telphone  =  value; }
        }
    }
}
复制代码

BLL层代码:

StudentBLL代码
复制代码
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Data;
using  DAL;
using  Entity;
using  System.Orm.DBTransaction;

namespace  BLL
{
    
public   class  StudentBP
    {
        
public  List < StudentEntity >  FindAll()
        {
            StudentDAL dal 
=   new  StudentDAL();
            
return  dal.FindAll();
        }

        
public   void  Save(StudentEntity entity)
        {
            IDbTransaction trans 
=   null ;
            
try
            {
                trans 
=  TransactionManager.CreateTransaction();
                StudentDAL dal 
=   new  StudentDAL(trans);
                dal.Save(entity);
                trans.Commit();

            }
            
catch  (Exception ex)
            {
                trans.Rollback();
                
throw  ex;
            }
            
finally
            {
                trans.Dispose();
            }
        }

        
public   void  Remove( object  id)
        {
            IDbTransaction trans 
=   null ;
            
try
            {
                trans 
=  TransactionManager.CreateTransaction();
                StudentDAL dal 
=   new  StudentDAL(trans);
                dal.Remove(id);
                trans.Commit();

            }
            
catch  (Exception ex)
            {
                trans.Rollback();
                
throw  ex;
            }
            
finally  {
                trans.Dispose();
            }
        }        
    }
}
复制代码

     在实体类中配置[Table(Name="Student")],对应数据库中的表名:Student

     在实体类中配置[Id(Name=”studentid”,Strategy = GenerationType.SEQUENCE)],表示当前属性是Student表中的主键ID,Name=”studentid”表示该属性Stuid对应Student表列studentid,Strategy表示主键生成策略,这里是自动增长。

     在实体类中配置[Column(Name="studentno")],表示当前属性Stuno对应Student表中的列名:studentno(默认属性名=列名)

在实体类中配置[Column(IsInsert=false)],表示当前列值不插入到数据库(默认插入)

在实体类中配置[Column(IsUpdate=false)],表示当前列值不更新到数据库(默认更新)

(实体类映射配置和一些命名参考了JAVA中的JPA)

在下一篇中将开始研究如何一步一步的构建一个简单的ORM框架。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值