面向接口(抽象)编程,是为了降低程序的耦合度,符合依赖倒置原则。因此典型的三层架构UI、BLL、DAL基于接口设计后,会拓展成UI、IBLL、BLL、IDAL、DAL。在上一篇《C# IoC学习笔记》中我们可以了解到,控制反转(IoC)使对象的创建交给了第三方IoC容器如Unity。利用Unity的IoC功能,可以对基于接口设计的三层架构做进一步的升级,搭建一个更容易实现的可配置可扩展的三层架构;利用Unity的AOP功能,使框架更加的简洁,因为它抽出了框架的公共逻辑部分。
依次新建项目UI(Client)、IBLL、BLL、IDAL、DAL、Model、Common。
对Unity容器的IoC调用进行封装,Container应封装成单例模式以提高效率,此处使用MemoryCache。
///
/// 内存缓存帮助类,支持绝对过期时间、滑动过期时间、文件依赖三种缓存方式。
///
class MemoryCacheHelper
{
private static readonly object _locker1 = new object(), _locker2 = new object();
///
/// 取缓存项,如果不存在则返回空。
///
///
///
///
public static T GetCacheItem<T>(string key)
{
try
{
return (T)MemoryCache.Default[key];
}
catch
{
return default(T);
}
}
///
/// 是否包含指定键的缓存项
///
///
///
public static bool Contains(string key)
{
return MemoryCache.Default.Contains(key);
}
///
/// 取缓存项,如果不存在则新增缓存项。
///
///
///
///
///
///
///
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];
}
///
/// 取缓存项,如果不存在则新增缓存项。
///
///
///
///
///
///
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];
}
///
/// 指定缓存项的一组逐出和过期详细信息
///
///
///
///
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;
}
///
/// 指定缓存项的一组逐出和过期详细信息
///
///
///
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;
}
///
/// 移除指定键的缓存项
///
///
public static void RemoveCacheItem(string key)
{
if (Contains(key))
{
MemoryCache.Default.Remove(key);
}
}
}
///
/// Unity容器帮助类
///
public static class UnityHelper
{
///
/// Unity容器创建对象(不含别名)
///
/// 容器名称
///
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>();
}
///
/// Unity容器创建对象(含别名)
///
///
/// 容器名称
/// 对象别名
///
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);
}
}
转载自:https://www.cnblogs.com/atomy/p/12516304.html