欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.NET C#三层开发结构Demo详解及源码(二)

程序员文章站 2022-06-13 12:31:34
...

IBLL业务逻辑接口描述,需引用Model和IDAL,包含一个BLL工厂类,业务逻辑基础接口,两个模块逻辑接口

.NET C#三层开发结构Demo详解及源码(二)

BLLFactory.cs 业务逻辑工厂类代码

using Model;
using System.Configuration;
using System.Reflection;

namespace IBLL
{
    /// <summary>
    /// 业务逻辑工厂类
    /// </summary>
    /// <typeparam name="T">需要操作的业务逻辑类Model实体类型</typeparam>
    public class BLLFactory<T>
    {
        private static readonly string BLL_NameSpace = ConfigurationManager.AppSettings["BLL_NameSpace"];
        private static readonly string BLL_Users = ConfigurationManager.AppSettings["BLL_Users"];
        private static readonly string BLL_Nation = ConfigurationManager.AppSettings["BLL_Nation"];

        /// <summary>
        /// 返回此泛型Model对应的BLL 
        /// </summary>
        /// <returns></returns>
        public dynamic CreateBLL()
        {
            if (typeof(T) == typeof(Users))
                return Assembly.Load(BLL_NameSpace).CreateInstance(BLL_NameSpace + "." + BLL_Users) as IUsersIBLL;
            else if (typeof(T) == typeof(Nation))
                return Assembly.Load(BLL_NameSpace).CreateInstance(BLL_NameSpace + "." + BLL_Nation) as INationIBLL;

            return null;
        }
    }
}

IBaseIBLL.cs 业务逻辑基础接口描述

using System;
using System.Linq;
using System.Linq.Expressions;

namespace IBLL
{
    /// <summary>
    /// 业务逻辑基础描述接口
    /// </summary>
    public interface IBaseIBLL<T>
    {
        IQueryable<T> Get();

        IQueryable<T> Get(Expression<Func<T, bool>> exp);

        T Get(int id);

        int Add(T entity);

        int Change(T entity);

        int Remove(int id);
    }
}

IUsersIBLL.cs 和 INationIBLL.cs 业务模块接口

using System.Linq;
using Model;

namespace IBLL
{
    /// <summary>
    /// Users业务逻辑描述接口
    /// </summary>
    public interface IUsersIBLL : IBaseIBLL<Users>
    {
        /// <summary>
        /// 扩展分页方法描述
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        IQueryable<Users> Get(int pageSize, int pageIndex);
    }
}
using Model;

namespace IBLL
{
    /// <summary>
    /// Nation业务逻辑描述接口
    /// </summary>
    public interface INationIBLL : IBaseIBLL<Nation>
    {
    }
}

接下来就是TestBLL业务逻辑接口实现,里面会有TestDAL的调用,包含基础接口实现,模块功能实现

BaseBLL.cs 基础接口实现代码

using System;
using System.Linq;
using System.Linq.Expressions;
using IBLL;
using IDAL;

namespace TestBLL
{
    /// <summary>
    /// 业务逻辑基础接口实现
    /// </summary>
    public class BaseBLL<T> : IBaseIBLL<T> where T : class
    {
        /// 注意,这里需要子类来实例化所需要的数据访问类,构造函数中指定
        private IBaseIDAL<T> _Dal;

        //构造
        public BaseBLL(IBaseIDAL<T> dal) { _Dal = dal; }

        public int Add(T entity)
        {
            return _Dal.Insert(entity);
        }

        public int Change(T entity)
        {
            return _Dal.Update(entity);
        }

        public IQueryable<T> Get()
        {
            return _Dal.Select();
        }

        public T Get(int id)
        {
            return _Dal.Select(id);
        }

        public IQueryable<T> Get(Expression<Func<T, bool>> exp)
        {
            return _Dal.Select(exp);
        }

        public int Remove(int id)
        {
            T t = _Dal.Select(id);
            if (t != null) return _Dal.Delete(t);
            return 0;
        }
    }
}

UsersBLL.cs 和 NationBLL.cs两个模块实现类

using System.Linq;
using Model;
using IDAL;
using IBLL;

namespace TestBLL
{
    /// <summary>
    /// Users业务逻辑实现
    /// </summary>
    public class UsersBLL : BaseBLL<Users>, IUsersIBLL
    {
        private static IUsersIDAL dal = new DALFactory<Users>().CreateDAL();

        //构造函数中将具体的数据访问对象传到父级的构造函数中
        public UsersBLL() : base(dal)
        {
        }

        public IQueryable<Users> Get(int pageSize, int pageIndex)
        {
            return dal.Select(pageSize, pageIndex);
        }
    }
}
using Model;
using IDAL;
using IBLL;

namespace TestBLL
{
    /// <summary>
    /// Nation业务逻辑实现
    /// </summary>
    public class NationBLL : BaseBLL<Nation>, INationIBLL
    {
        private static INationIDAL dal = new DALFactory<Nation>().CreateDAL();
        public NationBLL() : base(dal)
        {
        }
    }
}

接下来就是WebUI表现层了,只需要引用IBLL和Model即可,通过BLLFactory工厂类创建相应的操作对象,大大简化UI层的代码,结果是这样的:

.NET C#三层开发结构Demo详解及源码(二)

前端就使用了一个Repeater,C#代码如下

using System;
using System.Linq;
using System.Web.UI.WebControls;
using IBLL;
using Model;

namespace WebUI
{
    public partial class page1 : System.Web.UI.Page
    {
        //通过业务逻辑层工厂创建相应的操作对象
        IUsersIBLL userBLL = new BLLFactory<Users>().CreateBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //调用对象方法
                Repeater1.DataSource = userBLL.Get().ToList();
                Repeater1.DataBind();
            }
        }
    }
}

结束!

语言组织能力有限,想的很多却写不出来,以后有机会慢慢完善吧。

相关标签: 三层开发