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

    最终实现后达到的效果,只需写少量代码就可实现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框架。

你可能感兴趣的:(orm)