使用反射--完成三层架构

本文介绍了一个基于C#的ORM框架实现,通过抽象基类和接口定义了通用的数据访问层,包括增删改查等基本操作,并展示了如何为特定实体如用户表创建专门的数据访问对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目结构图:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XU.Model
{
     public abstract class BaseModel
    {
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XU.Model
{
    public class User :BaseModel
    {
        public string UserName { get; set; }
        public string PassWord { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XU.Model;

namespace XU.IDAL
{
    public interface IBaseDAL<T> where T : XU.Model.BaseModel, new()
      {
          bool Add(T model);
          bool Delete(int ID);
          bool Update(T model);
         T GetModel(int ID);
     }

    public interface IUserDAL : IBaseDAL<XU.Model.User>
    {
        /// <summary>
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        bool Exists(string userName);
    }
}
using System;
using XU.IDAL;
using XU.Model;

namespace XU.MSSQLDAL
{
    public class UserDAL : XU.IDAL.IUserDAL
    {
        public bool Exists(string userName)
        {
            return true;
        }

        public bool Add(Model.User model)
        {
            return true;
        }

        public bool Delete(int ID)
        {
            return true;
        }

        public bool Update(Model.User model)
        {
            return true;
        }

        public Model.User GetModel(int ID)
        {
            return new XU.Model.User() { UserName = "MSSQL", PassWord = "123456" };
        }
    }
}
using System;

namespace XU.FactoryDAL
{
    public class DataAccess<T> where T : class
    {
        //获取配置路径
        private static readonly string path = "XU.MSSQLDAL";//System.Configuration.ConfigurationManager.AppSettings["DAL"];
        private DataAccess() { }

        /// <summary>
        /// 创建实例
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T CreateDAL(string type)
        {
            string className = string.Format(path + ".{0}", type);
            try
            {
                return (T)System.Reflection.Assembly.Load(path).CreateInstance(className);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
    }
}

using System;
using XU.FactoryDAL;

namespace XU.BLL
{
    public class BaseBLL<T> where T : XU.Model.BaseModel, new()
    {
        protected XU.IDAL.IBaseDAL<T> Dal;
        public BaseBLL(string type)
        {
            Dal = XU.FactoryDAL.DataAccess<XU.IDAL.IBaseDAL<T>>.CreateDAL(type);
        }
        public virtual bool Add(T model)
        {
            return Dal.Add(model);
        }
        public virtual bool Delete(int ID)
        {
            return Dal.Delete(ID);
        }
        public virtual bool Update(T model)
        {
            return Dal.Update(model);
        }
        public virtual T GetModel(int ID)
        {
            return Dal.GetModel(ID);
        }
    }
}


using System;
using XU.Model;

namespace XU.BLL
{
    public class UserBLL : BaseBLL<XU.Model.User>
    {
        private const string _Type = "UserDAL";
        private XU.IDAL.IUserDAL _Dal;

        public UserBLL()
            : base(_Type)
        {
            _Dal = base.Dal as XU.IDAL.IUserDAL;
            if (_Dal == null)
            {
                throw new NullReferenceException(_Type);
            }
        }

        public bool Exists(string userName)
        {
            return _Dal.Exists(userName);
        }
    }
}

            XU.BLL.UserBLL userBLL = new XU.BLL.UserBLL();
             XU.Model.User user=new XU.Model.User();
            int id = 1;
             Console.WriteLine("添加------{0}", userBLL.Add(user));




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值