Write your own ORM framework (1): Preview of target effect

Write your own ORM framework (1): Preview of target effect

No nonsense, there are many benefits of using ORM: (1) simplify the code, (2) reduce errors in the code, (3) generate Model, DAL, and BLL layers in batches, realizing agile and rapid development.
Although the efficiency of ORM is mentioned on the Internet, the problem will be explained and solved in detail later.
The effect achieved after the final realization, only need to write a small amount of code to achieve CURD operation.

1. The mapping relationship configuration between entity classes and database tables:

1.1 StudentEntity code

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 layer code

2.1 StudentDAL code

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 layer code:

3.1 StudentBLL code

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

Configure in the entity class: Table(Name=”Student”)], corresponding to the table name in the database: Student

Configure in the entity class:
[Id(Name=”studentid”,Strategy = GenerationType.SEQUENCE)], which
means that the current attribute is the primary key ID in the Student table, Name=”studentid” means that the attribute Stuid corresponds to the Student table column student, Strategy Represents the primary key generation strategy, here is automatic growth.

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

Configure [Column(IsInsert=false)] in the entity class to indicate that the current column value is not inserted into the database (default insertion)

Configure [Column(IsUpdate=false)] in the entity class to indicate that the current column value is not updated to the database (updated by default)

(Entity class mapping configuration and some naming refer to JPA in JAVA)

Guess you like

Origin blog.csdn.net/coolhe21cn/article/details/79075713