comparison Extensions/JointureAddOn/Mapping/LazyValueLoadInterceptor.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using Castle.DynamicProxy;
3 using IInterceptor = Castle.DynamicProxy.IInterceptor;
4
5 namespace BLToolkit.Mapping
6 {
7 public class LazyValueLoadInterceptor : IInterceptor
8 {
9 #region Fields
10
11 private readonly IObjectMapper _mapper;
12 private readonly Func<IMapper, object, Type, object> _lazyLoader;
13 private bool _intercepted;
14
15 #endregion
16
17 public LazyValueLoadInterceptor(IObjectMapper mapper, Func<IMapper, object, Type, object> lazyLoader)
18 {
19 _mapper = mapper;
20 _lazyLoader = lazyLoader;
21 }
22
23 public void Intercept(IInvocation invocation)
24 {
25 if (invocation.Method.Name.StartsWith("get_"))
26 {
27 string propertyName = invocation.Method.Name.Substring(4);
28
29 foreach (IMapper map in _mapper.PropertiesMapping)
30 {
31 if (map.PropertyName == propertyName)
32 {
33 if (!_intercepted)
34 {
35 _intercepted = true;
36 map.Setter(invocation.Proxy, _lazyLoader(map, invocation.Proxy, invocation.TargetType));
37 }
38 break;
39 }
40 }
41 }
42
43 // let the original call go through first, so we can notify *after*
44 invocation.Proceed();
45 }
46 }
47 }