一、引言

    面向接口(抽象)编程,是为了降低程序的耦合度,符合依赖倒置原则。因此典型的三层架构UI、BLL、DAL基于接口设计后,会拓展成UI、IBLL、BLL、IDAL、DAL。在上一篇《C# IoC学习笔记》中我们可以了解到,控制反转(IoC)使对象的创建交给了第三方IoC容器如Unity。利用Unity的IoC功能,可以对基于接口设计的三层架构做进一步的升级,搭建一个更容易实现的可配置可扩展的三层架构;利用Unity的AOP功能,使框架更加的简洁,因为它抽出了框架的公共逻辑部分。

    二、项目建立

    2.1、项目建立

    依次新建项目UI(Client)、IBLL、BLL、IDAL、DAL、Model、Common。

C#基于接口设计三层架构Unity篇

    2.2项目说明

C#基于接口设计三层架构Unity篇

    三、项目之间的引用关系

    3.1、对着项目名称右键->添加->引用->项目,添加各个项目的引用。

    3.2、引用说明

C#基于接口设计三层架构Unity篇

C#基于接口设计三层架构Unity篇

    四、项目需安装的NuGet包

 C#基于接口设计三层架构Unity篇

     五、注意事项

    对Unity容器的IoC调用进行封装,Container应封装成单例模式以提高效率,此处使用MemoryCache。

    5.1、在Common项目引用System.Runtime.Caching.dll,并在Helper文件夹下新建一个缓存帮助类:MemoryCacheHelper.cs。

    /// <summary>
    /// 内存缓存帮助类,支持绝对过期时间、滑动过期时间、文件依赖三种缓存方式。
    /// </summary>
    class MemoryCacheHelper
    {
        private static readonly object _locker1 = new object(), _locker2 = new object();

        /// <summary>
        /// 取缓存项,如果不存在则返回空。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetCacheItem<T>(string key)
        {
            try
            {
                return (T)MemoryCache.Default[key];
            }
            catch
            {
                return default(T);
            }
        }

        /// <summary>
        /// 是否包含指定键的缓存项
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Contains(string key)
        {
            return MemoryCache.Default.Contains(key);
        }

        /// <summary>
        /// 取缓存项,如果不存在则新增缓存项。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="cachePopulate"></param>
        /// <param name="slidingExpiration"></param>
        /// <param name="absoluteExpiration"></param>
        /// <returns></returns>
        public static T GetOrAddCacheItem<T>(string key, Func<T> cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
        {
            if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key");
            if (cachePopulate == null) throw new ArgumentNullException("cachePopulate");
            if (slidingExpiration == null && absoluteExpiration == null) throw new ArgumentException("Either a sliding expiration or absolute must be provided");

            if (MemoryCache.Default[key] == null)
            {
                lock (_locker1)
                {
                    if (MemoryCache.Default[key] == null)
                    {
                        T cacheValue = cachePopulate();
                        if (!typeof(T).IsValueType && cacheValue == null)   //如果是引用类型且为NULL则不存缓存
                        {
                            return cacheValue;
                        }

                        var item = new CacheItem(key, cacheValue);
                        var policy = CreatePolicy(slidingExpiration, absoluteExpiration);

                        MemoryCache.Default.Add(item, policy);
                    }
                }
            }

            return (T)MemoryCache.Default[key];
        }

        /// <summary>
        /// 取缓存项,如果不存在则新增缓存项。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="cachePopulate"></param>
        /// <param name="dependencyFilePath"></param>
        /// <returns></returns>
        public static T GetOrAddCacheItem<T>(string key, Func<T> cachePopulate, string dependencyFilePath)
        {
            if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key");
            if (cachePopulate == null) throw new ArgumentNullException("cachePopulate");

            if (MemoryCache.Default[key] == null)
            {
                lock (_locker2)
                {
                    if (MemoryCache.Default[key] == null)
                    {
                        T cacheValue = cachePopulate();
                        if (!typeof(T).IsValueType && cacheValue == null)   //如果是引用类型且为NULL则不存缓存
                        {
                            return cacheValue;
                        }

                        var item = new CacheItem(key, cacheValue);
                        var policy = CreatePolicy(dependencyFilePath);

                        MemoryCache.Default.Add(item, policy);
                    }
                }
            }

            return (T)MemoryCache.Default[key];
        }

        /// <summary>
        /// 指定缓存项的一组逐出和过期详细信息
        /// </summary>
        /// <param name="slidingExpiration"></param>
        /// <param name="absoluteExpiration"></param>
        /// <returns></returns>
        private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration)
        {
            var policy = new CacheItemPolicy();

            if (absoluteExpiration.HasValue)
            {
                policy.AbsoluteExpiration = absoluteExpiration.Value;
            }
            else if (slidingExpiration.HasValue)
            {
                policy.SlidingExpiration = slidingExpiration.Value;
            }

            policy.Priority = CacheItemPriority.Default;

            return policy;
        }

        /// <summary>
        /// 指定缓存项的一组逐出和过期详细信息
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static CacheItemPolicy CreatePolicy(string filePath)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string>() { filePath }));
            policy.Priority = CacheItemPriority.Default;
            return policy;
        }

        /// <summary>
        /// 移除指定键的缓存项
        /// </summary>
        /// <param name="key"></param>
        public static void RemoveCacheItem(string key)
        {
            if (Contains(key))
            {
                MemoryCache.Default.Remove(key);
            }
        }
    }
View Code

相关文章:

  • 2021-08-15
  • 2021-07-31
  • 2021-09-11
  • 2021-05-22
  • 2021-12-25
猜你喜欢
  • 2021-08-06
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2021-11-13
相关资源
相似解决方案