VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#基于接口设计三层架构Unity篇

    一、引言

    面向接口(抽象)编程,是为了降低程序的耦合度,符合依赖倒置原则。因此典型的三层架构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。

    2.2项目说明

    三、项目之间的引用关系

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

    3.2、引用说明

    四、项目需安装的NuGet包

 

     五、注意事项

    对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);
            }
        }
    }
复制代码

    5.2、在Common项目Helper文件夹下面新建一个Unity帮助类:UnityHelper.cs,并实现Container的缓存。

复制代码
    /// <summary>
    /// Unity容器帮助类
    /// </summary>
    public static class UnityHelper
    {
        /// <summary>
        /// Unity容器创建对象(不含别名)
        /// </summary>
        /// <param name="containerName">容器名称</param>
        /// <returns></returns>
        public static T GetObject<T>(string containerName)
        {
            IUnityContainer container = MemoryCacheHelper.GetOrAddCacheItem(containerName, () =>
            {
                IUnityContainer unityContainer = new UnityContainer();
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
                {
                    ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"ConfigFiles\Unity.config")
                };
                Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                configSection.Configure(unityContainer, containerName);
                return unityContainer;
            },TimeSpan.FromMinutes(30),null);
            
            return container.Resolve<T>();
        }

        /// <summary>
        /// Unity容器创建对象(含别名)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="containerName">容器名称</param>
        /// <param name="alisName">对象别名</param>
        /// <returns></returns>
        public static T GetObject<T>(string containerName, string alisName)
        {
            IUnityContainer container = MemoryCacheHelper.GetOrAddCacheItem(containerName, () =>
            {
                IUnityContainer unityContainer = new UnityContainer();
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
                {
                    ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"ConfigFiles\Unity.config")
                };
                Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                configSection.Configure(unityContainer, containerName);
                return unityContainer;
            }, TimeSpan.FromMinutes(30), null);

            return container.Resolve<T>(alisName);
        }
    }
复制代码


相关教程