-
什么是MEF
Git:https://github.com/MicrosoftArchive/mef
MEF也是一款ioc框架,貌似历史比较悠久了。
这里有一篇.net阵容里面主流ioc比较。
https://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html
-
AOP
引用百度。
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
-
背景
线上有个报表项目,最近监控发现有个报表查询比较频繁,导致db压力大,现需要对查询频次较多的报表进行缓存。
-
代码实现
Nugget 引用Castle.Core
1自定义ExportProved
public { private
public AOPExportProvider(Func<ComposablePartCatalog> catalogResolver) { _exportProvider = new
//support recomposition _exportProvider.ExportsChanged += (s, e) => OnExportsChanged(e); _exportProvider.ExportsChanging += (s, e) => OnExportsChanging(e); }
public { get { return _exportProvider.SourceProvider; } set { _exportProvider.SourceProvider = value; } }
protected ImportDefinition definition, AtomicComposition atomicComposition) { IEnumerable<Export> exports = _exportProvider.GetExports(definition, atomicComposition); return exports.Select(export => new }
private { var value = innerExport.Value; IInterceptor[] attribs = value.GetType().GetCustomAttributes(typeof(IInterceptor), true).Cast<IInterceptor>().ToArray(); if (attribs.Length == 0) return value; ProxyGenerator generator = new object proxy = generator.CreateClassProxy(value.GetType(), attribs);
PropertyInfo[] propertyInfo= value.GetType().GetProperties(); Type proxyType = proxy.GetType().BaseType; foreach (var item in propertyInfo) { PropertyInfo property = proxyType.GetProperty(item.Name); if (property == null) continue; property.SetValue(proxy, item.GetValue(value,null),null); } return proxy; }
public { _exportProvider.Dispose(); } } |
2 在web启动的时候 指定自定义exportprovide
public { public { var container = ConfigureContainer(); ControllerBuilder.Current.SetControllerFactory(new var dependencyResolver = System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver; }
private { Func<ComposablePartCatalog> catalogResolver = () => {
AggregateCatalog catalog = new catalog.Catalogs.Add(new return catalog; }; AOPExportProvider provider = new CompositionContainer container = new provider.SourceProvider = container; AppDomain.CurrentDomain.SetData("Container", container); return container; } } |
3 实现Interceptor
[Export(typeof(IInterceptor))] public { private public CacheInterceptor(){ _cacheProvider = ((CompositionContainer)AppDomain.CurrentDomain.GetData("Container")).GetExportedValue<ICache>(); } private public { var qCachingAttribute = this.GetQCachingAttributeInfo(invocation.MethodInvocationTarget ?? invocation.Method); if (qCachingAttribute != null) { ProceedCaching(invocation, qCachingAttribute); } else { invocation.Proceed(); } } private { return method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(QCachingAttribute)) as } private { var cacheKey = attribute.Key; if(string.IsNullOrEmpty(cacheKey)) cacheKey= GenerateCacheKey(invocation);
var cacheValue = _cacheProvider.Get(cacheKey, x => { return if (cacheValue != null) { invocation.ReturnValue = cacheValue; return; }
invocation.Proceed();
if (!string.IsNullOrWhiteSpace(cacheKey)) { _cacheProvider.Set(cacheKey, invocation.ReturnValue, TimeSpan.FromSeconds(attribute.AbsoluteExpiration)); } } private { var typeName = invocation.TargetType.Name; var methodName = invocation.Method.Name; var methodArguments = this.FormatArgumentsToPartOfCacheKey(invocation.Arguments);
return } //拼接缓存的键 private { var builder = new
builder.Append(typeName); builder.Append(_linkChar);
builder.Append(methodName); builder.Append(_linkChar);
foreach (var param in parameters) { builder.Append(param); builder.Append(_linkChar); }
return builder.ToString().TrimEnd(_linkChar); }
private { return methodArguments.Select(this.GetArgumentValue).Take(maxCount).ToList(); } //处理方法的参数,可根据情况自行调整 private { if (arg is return arg.ToString();
if (arg is return ((DateTime)arg).ToString("yyyyMMddHHmmss");
if (arg is return ((IQCachable)arg).CacheKey;
return } } |
4 定义拦截Attribute
[AttributeUsage(AttributeTargets.Method, Inherited = true)] public { public public } } |
5配置使用Interceptor
[Export(typeof(ICompanyDaily))] [CacheInterceptor] public { [Import] public [QCaching(AbsoluteExpiration =60*60*8)] public { string beginDate = sellDateBegin.ToString("yyyy-MM-dd"); string endDate = sellDateEnd.ToString("yyyy-MM-dd"); try { //dosomething return dtChart; } catch (Exception exp) { throw exp; } } |
Ps:拦截的方法必须是virtual