zoukankan      html  css  js  c++  java
  • 精简BLL业务层,使用范型复用常用方法


    首先DALFactory,需要增加一个类DALFactory2.cs

    //*--------------------------------------
    //*  Create By Yesun .Net Tool V1.1
    //*  CopyRight (C) yesun
    //*  Email:edzh@tom.com QQ:363980
    //*  Msn:dyesur@hotmail.com
    //*  Web http://edzh.com
    //*  DateTime 2006-6-2
    //*--------------------------------------
    using System;
    using System.Reflection;
    using System.Collections.Specialized;
    using System.Configuration;
    using Yesun.Edzh.IDAL;
    namespace Yesun.Edzh.DALFactory
    {
        /// <summary>
        /// 工厂类Admin 的摘要说明。
        /// web.config 需要加入配置:(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)
        /// DataCache类在导出代码的文件夹里
        /// 可以把所有DAL类的创建放在这个DataAccess类里
        /// <appSettings>
        /// <add key="DAL" value="CMS.DAL" /> DAL路径,(这里的命名空间根据实际情况更改为自己项目的命名空间)
        /// </appSettings>
        /// </summary>
        public sealed class DataAccess2<T>
        {
            private static readonly string path = ConfigurationManager.AppSettings["DAL"];

            /// <summary>
            /// 创建对象或从缓存获取
            /// </summary>
            public static object CreateObject(string path, string CacheKey)
            {
                System.Web.Caching.Cache objCache = System.Web.HttpRuntime.Cache;
                object objType = objCache[CacheKey];
                if (objType == null)
                {
                    try
                    {
                        objType = Assembly.Load(path).CreateInstance(CacheKey);
                        objCache.Insert(CacheKey, objType);// 写入缓存
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                return objType;
            }
            /// <summary>
            /// 创建数据层接口
            /// </summary>
            public static IDAL<T> CreateDAL()
            {
                string className = typeof(T).ToString();
                className = className.Substring(className.LastIndexOf(".") + 1);
                string CacheKey = path + "." + className;
                object objType = CreateObject(path, CacheKey);
                return (IDAL<T>)objType;
            }
        }
    }

     然后再BLL层,需要增加一个基类BaseBLL.cs

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

    namespace Yesun.Edzh.BLL
    {
        /// <summary>
        /// 业务层常用方法
        /// </summary>
        public class BaseBLL<T>
        {
            private static readonly Yesun.Edzh.IDAL.IDAL<T> dal = Yesun.Edzh.DALFactory.DataAccess2<T>.CreateDAL();

            /// <summary>
            /// 增加一条数据
            /// </summary>
            public bool Save(T t)
            {
                return dal.Save(t);
            }

            /// <summary>
            /// 修改一条数据
            /// </summary>
            public bool Update(T t)
            {
                return dal.Update(t);
            }

            /// <summary>
            /// 取得对象实体
            /// </summary>
            public T GetModel(Guid id)
            {
                return dal.GetModel(id);
            }

            /// <summary>
            /// 取得对象实体
            /// </summary>
            public T GetModel(string id)
            {
                return dal.GetModel(new Guid(id));
            }

            /// <summary>
            /// 取得数据列表
            /// </summary>
            public Yesun.Edzh.Model.SqlPage<T> GetList()
            {
                return this.GetList(1, -1);
            }

            /// <summary>
            /// 取得数据列表
            /// </summary>
            public Yesun.Edzh.Model.SqlPage<T> GetList(int pageid, int pagesize)
            {
                return this.GetList(pageid, pagesize, "");
            }

            /// <summary>
            /// 取得数据列表
            /// </summary>
            public Yesun.Edzh.Model.SqlPage<T> GetList(int pageid, int pagesize, string where)
            {
                return this.GetList(pageid, pagesize, where, "");
            }

            /// <summary>
            /// 取得数据列表
            /// </summary>
            public Yesun.Edzh.Model.SqlPage<T> GetList(int pageid, int pagesize, string where, string sort)
            {
                return this.GetList(pageid, pagesize, where, sort, "");
            }

            /// <summary>
            /// 取得数据列表
            /// </summary>
            public Yesun.Edzh.Model.SqlPage<T> GetList(int pageid, int pagesize, string where, string sort, string fields)
            {
                return this.GetList(pageid, pagesize, where, sort, fields, "");
            }

            /// <summary>
            /// 取得数据列表
            /// </summary>
            public Yesun.Edzh.Model.SqlPage<T> GetList(int pageid, int pagesize, string where, string sort, string fields, string pk)
            {
                return dal.GetList(pk, fields, sort, where, pageid, pagesize);
            }

            /// <summary>
            /// 删除N条数据
            /// </summary>
            public bool Delete(string delid)
            {
                return dal.Delete(delid);
            }

            /// <summary>
            /// 按条件统计
            /// </summary>
            public long GetCount(string where)
            {
                return dal.GetCount(where);
            }

            /// <summary>
            /// 按条件判断是否重复
            /// </summary>
            public bool IsExsit(string where)
            {
                return dal.IsExsit(where);
            }

        }
    }

     接下来BLL层的普通类只需要基层BaseBLL即可,支持扩充方法

    //*--------------------------------------
    //*  Create By Yesun .Net Tool V1.1
    //*  CopyRight (C) yesun
    //*  Email:edzh@tom.com QQ:363980
    //*  Msn:dyesur@hotmail.com
    //*  Web http://edzh.com
    //*  DateTime 2006-10-11
    //*--------------------------------------
    using System;
    using System.Data;
    using System.Collections;
    using System.Text;
    using System.Data.SqlClient;
    using Yesun.Edzh.Model;
    using Yesun.Edzh.IDAL;
    using Yesun.Edzh.DALFactory;

    namespace Yesun.Edzh.BLL
    {
     /// <summary>
     /// 逻辑业务层 Admin
     /// </summary>
     public class AdminService : BaseBLL<Yesun.Edzh.Model.Admin>
     {
            private static readonly IAdmin dal = DataAccess.CreateAdmin();

            #region 业务层扩充方法

            /// <summary>
            /// 得到一个对象实体
            /// </summary>
            /// <param name="username"></param>
            /// <returns></returns>
            public Yesun.Edzh.Model.Admin GetModelByUserName(string username)
            {
                return dal.GetModelByUserName(username);
            }

            /// <summary>
            /// 通过用户id取得属于该用户的权限
            /// </summary>
            /// <param name="userid"></param>
            /// <returns></returns>
            public Hashtable GetPermissionByUserId(Guid userid)
            {
                Hashtable htPermisstion = new Hashtable();
                SqlPage<Yesun.Edzh.Model.Permission> sqlpage = new PermissionService().GetList(1,-1,"id IN(select permissionid from cms_role_permission where roleid IN(select roleid from cms_user_role where userid='" + userid + "'))");
                foreach (Yesun.Edzh.Model.Permission permission in sqlpage)
                {
                    if(permission == null) continue;
                    if (permission.PermissionName != "")
                    {
                        htPermisstion.Add((object)(permission.PermissionName + "" + permission.Operate), (object)(permission.PermissionName + "" + permission.Operate));
                    }
                }
                return htPermisstion;
            }

            #endregion
        }
    }

    思路基本上和上次DAL,IDAL这两个层差不多,稍作变化而已!

  • 相关阅读:
    golang框架对比Revel and Beego
    Gin框架系列02:路由与参数
    用cp命令拷贝文件,源目录后带不带斜杠的区别
    Isilon Gen6的换盘步骤
    如何在同一行里执行多个linux命令?
    KB,MB,GB,TB,PB,EB,ZB,YB,BB
    Isilon的WebUI上指定跨时区时间的小问题
    用Powershell强制同步Windows主机与Internet time server的时间
    Remote Desktop突然不能用了 “This could be due to CredSSP encryption oracle remediation”
    打开KVM Console的一些注意事项
  • 原文地址:https://www.cnblogs.com/yesun/p/767118.html
Copyright © 2011-2022 走看看