自己动手写ORM框架(九):实现删除功能Remove方法

本文分享了一种ORM操作思路,包括实体类的增删改查等基本功能的实现方式,并提供了相关类的代码示例。

    前面简单的修改功能已经实现了,下面是实现删除功能,代码1-1如下:

 删除实体对应数据库中的数据
public int Remove<T>(T entity)
{
 tableInfo = .GetTableInfo(entity, .DELETE);
 strSql = .GetDeleteByIdSql(tableInfo);
[] parms = .CreateDbParameters(1);
    
    parms[0].ParameterName = tableInfo.Id.Key;
    
    parms[0].Value = tableInfo.Id.Value;
    
    object val = .ExecuteNonQuery(transaction, .Text, strSql, parms);
    
    return .ToInt32(val);
}

 
 根据主键id删除实体对应数据库中的数据
public int Remove<T>(object id)  T : new()
{
 tableInfo = .GetTableInfo(new T(), .DELETE);
 strSql = .GetDeleteByIdSql(tableInfo);
[] parms = .CreateDbParameters(1);
    
    parms[0].ParameterName = tableInfo.Id.Key;
    
    parms[0].Value = id;
    
    object val = .ExecuteNonQuery(transaction, .Text, strSql, parms);
    
    return .ToInt32(val);
}
 

    DbEntityUtils.GetDeleteByIdSql方法代码如下1-2:

public static string GetDeleteByIdSql( tableInfo)
{
    string strSql = "delete {0} where {1} =" + .DbParmChar + tableInfo.Id.Key;
    strSql = string.Format(strSql, tableInfo.TableName, tableInfo.Id.Key);
    return strSql;
}

    到这里为止简单的增、删、改、查功能都已完成,还有许多复杂的操作需要完成,但需要大量的时间和精力,这里只是分享一种思路或是作技术交流,所以不再继续深入的研究下去了,如有兴趣的也可以继续研究和分享经验。

    下面是几个类的代码,EntityManagerImpl类:

EntityManagerImplusing System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Orm.CustomAttributes;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using System.Orm.DBUtility;
using System.Orm.Common;

namespace System.Orm.EntityManager
{
    public class EntityManagerImpl : EntityManager
    {
        IDbTransaction transaction = null;

        #region 将实体数据保存到数据库
        public int Save<T>(T entity)
        {
            TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.INSERT);

            String strSql = DbEntityUtils.GetInsertSql(tableInfo);

            IDbDataParameter[] parms = DbFactory.CreateDbParameters(tableInfo.Columns.Count);

            DbEntityUtils.SetParameters(tableInfo.Columns, parms);

            object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);

            return Convert.ToInt32(val);
        }
        #endregion

        #region 将实体数据修改到数据库
        public int Update<T>(T entity)
        {
            TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.UPDATE);

            String strSql = DbEntityUtils.GetUpdateSql(tableInfo);

            IDbDataParameter[] parms = DbFactory.CreateDbParameters(tableInfo.Columns.Count);

            DbEntityUtils.SetParameters(tableInfo.Columns, parms);

            object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);

            return Convert.ToInt32(val);
        }
        #endregion

        #region 删除实体对应数据库中的数据
        public int Remove<T>(T entity)
        {
            TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.DELETE);

            String strSql = DbEntityUtils.GetDeleteByIdSql(tableInfo);

            IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
            parms[0].ParameterName = tableInfo.Id.Key;
            parms[0].Value = tableInfo.Id.Value;

            object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);

            return Convert.ToInt32(val);
        }
        #endregion

        #region 根据主键id删除实体对应数据库中的数据
        public int Remove<T>(object id) where T : new()
        {
            TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.DELETE);

            String strSql = DbEntityUtils.GetDeleteByIdSql(tableInfo);

            IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
            parms[0].ParameterName = tableInfo.Id.Key;
            parms[0].Value = id;

            object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);

            return Convert.ToInt32(val);
        }
        #endregion

        #region 查询实体对应表的所有数据
        public List<T> FindAll<T>() where T : new()
        {
            List<T> listArr = new List<T>();
            PropertyInfo[] properties = ReflectionUtils.GetProperties(new T().GetType());

            TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.SELECT);

            String strSql = DbEntityUtils.GetFindAllSql(tableInfo);

            IDataReader sdr = null;
            try
            {
                sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, strSql);
                while (sdr.Read())
                {
                    T entity = new T();
                    foreach (PropertyInfo property in properties)
                    {
                        String name = tableInfo.PropToColumn[property.Name].ToString();
                        ReflectionUtils.SetPropertyValue(entity, property, sdr[name]);
                    }

                    listArr.Add(entity);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sdr != null) sdr.Close();
            }

            return listArr;
        }
        #endregion

        #region 通过自定义SQL语句查询数据
        public List<T> FindBySql<T>(string strSql) where T : new()
        {
            List<T> listArr = new List<T>();
            Type classType = new T().GetType();
            PropertyInfo[] properties = ReflectionUtils.GetProperties(classType);

            IDataReader sdr = null;
            try
            {
                sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, strSql);
                while (sdr.Read())
                {
                    T entity = new T();
                    foreach (PropertyInfo property in properties)
                    {
                        ReflectionUtils.SetPropertyValue(entity, property, sdr[property.Name]);
                    }

                    listArr.Add(entity);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sdr != null) sdr.Close();
            }

            return listArr;
        }
        #endregion

        #region 通过主键ID查询数据
        public List<T> FindById<T>(object id) where T : new()
        {
            List<T> listArr = new List<T>();
            PropertyInfo[] properties = ReflectionUtils.GetProperties(new T().GetType());

            TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.SELECT);

            String strSql = DbEntityUtils.GetFindByIdSql(tableInfo);

            IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
            parms[0].ParameterName = tableInfo.Id.Key;
            parms[0].Value = id;

            IDataReader sdr = null;
            try
            {
                sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, strSql, parms);
                while (sdr.Read())
                {
                    T entity = new T();
                    foreach (PropertyInfo property in properties)
                    {
                        String name = tableInfo.PropToColumn[property.Name].ToString();
                        ReflectionUtils.SetPropertyValue(entity, property, sdr[name]);
                    }

                    listArr.Add(entity);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sdr != null) sdr.Close();
            }

            return listArr;
        }
        #endregion

        #region Transaction 注入事物对象属性
        public IDbTransaction Transaction
        {
            get
            {
                return transaction;
            }
            set
            {
                transaction = value;
            }
        }
        #endregion
    }
}


EntityManagerFactory类:

EntityManagerFactoryusing System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace System.Orm.EntityManager
{
    public class EntityManagerFactory
    {
        public static EntityManager CreateEntityManager()
        {
            return new EntityManagerImpl();
        }
    }
}


TransactionManager类:

TransactionManagerusing System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Data.Common;
using System.Orm.DBUtility;

namespace System.Orm.DBTransaction
{
    public class TransactionManager
    {   
        public static IDbTransaction CreateTransaction()
        {
            return DbFactory.CreateDbTransaction();
        }
    }
}


StudentDAL调用类:

StudentDALusing System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Entity;
using System.Orm.EntityManager;
using System.Orm.DBTransaction;

namespace DAL
{
    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);
        }        
    }
}

 

 

有一些园友希望能提供源码,这里将提供源码下载地址OrmTest.rar下载

转载于:https://www.cnblogs.com/wangwei123/archive/2010/07/03/1770598.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值