• 自己动手写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框架。

  • 相关阅读:
    django静态资源转移
    QT5 内置Multimedia开发音乐播放器
    Qt Creator 设置编码格式为 UTF-8
    QT 出错 moc_mainwindow.obj:-1: error: LNK2019: 无法解析的外部符号 " 中被引用...
    linux 安装node, 添加软链接,更改npm安装源
    django.template.exceptions.TemplateDoesNotExist: index.html
    centos下使用virtualenv建立python虚拟环境
    win7上 nginx 出现 403 Forbidden
    django安装xadmin中出现的报错汇总
    centos安装mysql57
  • 原文地址:https://www.cnblogs.com/wangwei123/p/1735257.html
Copyright © 2020-2023  润新知