Mercurial > pub > bltoolkit
diff Extensions/JointureAddOn/Mapping/LazyValueLoadInterceptor.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Extensions/JointureAddOn/Mapping/LazyValueLoadInterceptor.cs Thu Mar 27 21:46:09 2014 +0400 @@ -0,0 +1,47 @@ +using System; +using Castle.DynamicProxy; +using IInterceptor = Castle.DynamicProxy.IInterceptor; + +namespace BLToolkit.Mapping +{ + public class LazyValueLoadInterceptor : IInterceptor + { + #region Fields + + private readonly IObjectMapper _mapper; + private readonly Func<IMapper, object, Type, object> _lazyLoader; + private bool _intercepted; + + #endregion + + public LazyValueLoadInterceptor(IObjectMapper mapper, Func<IMapper, object, Type, object> lazyLoader) + { + _mapper = mapper; + _lazyLoader = lazyLoader; + } + + public void Intercept(IInvocation invocation) + { + if (invocation.Method.Name.StartsWith("get_")) + { + string propertyName = invocation.Method.Name.Substring(4); + + foreach (IMapper map in _mapper.PropertiesMapping) + { + if (map.PropertyName == propertyName) + { + if (!_intercepted) + { + _intercepted = true; + map.Setter(invocation.Proxy, _lazyLoader(map, invocation.Proxy, invocation.TargetType)); + } + break; + } + } + } + + // let the original call go through first, so we can notify *after* + invocation.Proceed(); + } + } +} \ No newline at end of file