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

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

不多废话,用ORM的好处很多:(1)简化代码,(2)减少代码中的错误,(3)批量生成Model和DAL、BLL层,实敏捷快速开发。
虽然网上有提及ORM的效率问题,但该问题后面再详细说明和解决方法。
最终实现后达到的效果,只需写少量代码就可实现CURD操作。

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

1.1 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; }
        }
    }
}

2. DAL层代码

2.1 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);
        }
    }

3. BLL层代码:

3.1 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)

猜你喜欢

转载自blog.csdn.net/coolhe21cn/article/details/79075713