• 三层架构


    一、架构图

    二、解决方案中的项目设计 

    AbstractFactory(抽象工厂) 

    IBLL(业务逻辑层) 

    BLL

    IDAL(数据访问层) 

    SqlServerDAL(SqlServer数据访问) 

    ```` 

    Model(数据模型) 

    ToolsLibrary(工具类) 

    WebMVCApplication(Web MVC应用程序) 

    三、具体的实现

    1、AbstractFactory 

    (1)Cache.cs 反射出实例很耗费性能,这里使用缓存来减轻负担 

    /// <summary>
    /// 抽象工厂的实现,因为反射出实例很耗费性能,所以运用了缓存来减轻负担
    /// </summary>
    public class Cache
    {
        public Cache()
        {

        }
        /// <summary>
        /// 获取缓存数据
        /// </summary>
        /// <param name="key">key</param>
        /// <returns></returns>
        public static object Get(string key)
        {
            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            return cache.Get(key);
        }
        /// <summary>
        /// 添加缓存数据
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public static void InsertCache(string key, object value)
        {
            if (Get(key) == null)
            {
                System.Web.Caching.Cache cache = HttpRuntime.Cache;
                cache.Insert(key, value);
            }
        }
    }

    (2)DALFactory.cs 反射DAL,使用IOC依赖注入

    public class DALFactory
    {
        /// <summary>
        /// DLL全路径
        /// </summary>
        public static readonly string DALPath = System.Configuration.ConfigurationManager.AppSettings["DALPath"];
        public static object CreateDAL(string assemblyPath, string objType)
        {
            var cacheDAL = Cache.Get(objType);
            if (cacheDAL == null)
            {
                cacheDAL = Assembly.Load(assemblyPath).CreateInstance(objType);
                Cache.InsertCache(objType, cacheDAL);
            }
            return cacheDAL;
        }
        public static IDAL.IExample GetExampleDAL()
        {
            return CreateDAL(DALPath, string.Format("{0}.Example", DALPath)) as IDAL.IExample;
        }
    }

    2、IBLL 

    public interface IExampleService
    {
        int Add(Model.Example model);

    } 

    3、BLL 

    public class ExampleService : IBLL.IExampleService
    {
        public ExampleService()
        {
            this.exampleDAL = AbstractFactory.DALFactory.GetExampleDAL();
        }
        /// <summary>
        /// 数据表数据访问层接口
        /// </summary>
        private IDAL.IExample exampleDAL;
        public IDAL.IExample ExampleDAL
        {
            set { exampleDAL = value; }
            get { return exampleDAL; }
        }
        public int Add(Model.Example model)
        {
            return exampleDAL.Add(model);
        }

    } 

    4、IDAL

    /// <summary>
    /// 接口层 IExample
    /// </summary>
    public interface IExample
    {
        #region 成员方法
        /// <summary>
        /// 增加一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        int Add(Model.Example model);
        #endregion

    } 

    5、SqlServerDAL 

    public class Example:IDAL.IExample
    {
        public int Add(Model.Example model)
        {
            return 0;
        }
    }

    6、Model 

    /// <summary>
    /// Example:实体类
    /// </summary>
    [Serializable]
    public class Example
    {
        private string _id = "";
        private string _name = "";
        /// <summary>
        /// ID
        /// </summary>
        public string ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// Name
        /// </summary>
        public string Name
        {
            set { _name = value; }
            get { return _name; }
        }

    } 

    7、Web.config 

    <appSettings>
      <!--DAL反射配置-->
      <add key="DALPath" value="SqlServerDAL" />
    </appSettings>
  • 相关阅读:
    echarts 点击事件 点击一次触发多次
    echarts 没有数据时,无数据展示
    递归 出现的一些问题
    echarts 下钻 返回设成按钮的样式
    阻止冒泡 阻止默认事件
    前端如何绘制svg格式图片
    html头部的标签有哪些?
    ant design 封装可填写Tag标签的input框
    React:通过Vite集成Tailwind CSS
    React:通过vite批量导入图片
  • 原文地址:https://www.cnblogs.com/sydeveloper/p/3017814.html
Copyright © 2020-2023  润新知