Commit 37aeff5

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-03-08 03:30:14
updated the proxy factory to lazy load the target.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@58 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent 558c110
trunk/product/MyMoney/Infrastructure/proxies/IProxyFactory.cs
@@ -1,33 +1,13 @@
+using System;
 using System.Collections.Generic;
-using System.Linq;
 using Castle.Core.Interceptor;
-using Castle.DynamicProxy;
 
-namespace Ec.AuditTool.Infrastructure.Proxies
+namespace MoMoney.Infrastructure.proxies
 {
     public interface IProxyFactory
     {
-        TypeToProxy CreateProxyFor<TypeToProxy>(TypeToProxy implementation, IEnumerable<IInterceptor> interceptors);
-    }
-
-    public class ProxyFactory : IProxyFactory
-    {
-        private readonly ProxyGenerator generator;
-
-        public ProxyFactory() : this(new ProxyGenerator())
-        {
-        }
-
-        public ProxyFactory(ProxyGenerator generator)
-        {
-            this.generator = generator;
-        }
+        TypeToProxy create_proxy_for<TypeToProxy>(TypeToProxy implementation, IEnumerable<IInterceptor> interceptors);
 
-        public TypeToProxy CreateProxyFor<TypeToProxy>(TypeToProxy implementation,
-                                                       IEnumerable<IInterceptor> interceptors)
-        {
-            return generator
-                .CreateInterfaceProxyWithTarget<TypeToProxy>(implementation, interceptors.ToArray());
-        }
+        TypeToProxy create_proxy_for<TypeToProxy>(Func<TypeToProxy> implementation, IEnumerable<IInterceptor> interceptors);
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/proxies/ProxyBuilder.cs
@@ -1,15 +1,14 @@
 using System.Collections.Generic;
 using System.Linq;
 using Castle.Core.Interceptor;
-using Ec.AuditTool.Infrastructure.Proxies;
 using Ec.AuditTool.Infrastructure.Proxies.Interceptors;
 
 namespace MoMoney.Infrastructure.proxies
 {
     public interface IProxyBuilder<TypeToProxy>
     {
-        IConstraintSelector<TypeToProxy> AddInterceptor<Interceptor>() where Interceptor : IInterceptor, new();
-        TypeToProxy CreateProxyFor(TypeToProxy an_implementation_of_the_interface);
+        IConstraintSelector<TypeToProxy> add_interceptor<Interceptor>() where Interceptor : IInterceptor, new();
+        TypeToProxy create_proxy_for(TypeToProxy target);
     }
 
     public class ProxyBuilder<TypeToProxy> : IProxyBuilder<TypeToProxy>
@@ -30,20 +29,19 @@ namespace MoMoney.Infrastructure.proxies
         }
 
 
-        public IConstraintSelector<TypeToProxy> AddInterceptor<Interceptor>() where Interceptor : IInterceptor, new()
+        public IConstraintSelector<TypeToProxy> add_interceptor<Interceptor>() where Interceptor : IInterceptor, new()
         {
             var constraint = constraint_factory.CreateFor<TypeToProxy>();
             constraints.Add(new Interceptor(), constraint);
             return constraint;
         }
 
-        public TypeToProxy CreateProxyFor(TypeToProxy an_implementation_of_the_interface)
+        public TypeToProxy create_proxy_for(TypeToProxy target)
         {
-            return proxy_factory.CreateProxyFor(an_implementation_of_the_interface,
-                                                AllInterceptorsWithTheirConstraints());
+            return proxy_factory.create_proxy_for(()=>target, all_interceptors_with_their_constraints());
         }
 
-        IEnumerable<IInterceptor> AllInterceptorsWithTheirConstraints()
+        IEnumerable<IInterceptor> all_interceptors_with_their_constraints()
         {
             foreach (var pair in constraints)
             {
trunk/product/MyMoney/Infrastructure/proxies/ProxyBuilderSpecs.cs
@@ -40,9 +40,9 @@ namespace MoMoney.Infrastructure.proxies
 
         because b = () =>
                         {
-                            sut.AddInterceptor<SomeInterceptor>();
-                            sut.AddInterceptor<AnotherInterceptor>();
-                            var proxy = sut.CreateProxyFor(an_implementation_of_the_interface);
+                            sut.add_interceptor<SomeInterceptor>();
+                            sut.add_interceptor<AnotherInterceptor>();
+                            var proxy = sut.create_proxy_for(an_implementation_of_the_interface);
                             proxy.OneMethod();
                             proxy.SecondMethod();
                         };
@@ -70,10 +70,10 @@ namespace MoMoney.Infrastructure.proxies
 
         because b = () =>
                         {
-                            var constraint = sut.AddInterceptor<SomeInterceptor>();
+                            var constraint = sut.add_interceptor<SomeInterceptor>();
                             constraint.InterceptOn.OneMethod();
 
-                            var proxy = sut.CreateProxyFor(an_implementation);
+                            var proxy = sut.create_proxy_for(an_implementation);
                             proxy.OneMethod();
                             proxy.SecondMethod();
                         };
trunk/product/MyMoney/Infrastructure/proxies/ProxyFactory.cs
@@ -0,0 +1,40 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Castle.Core.Interceptor;
+using Castle.DynamicProxy;
+using MoMoney.Infrastructure.interceptors;
+
+namespace MoMoney.Infrastructure.proxies
+{
+    public class ProxyFactory : IProxyFactory
+    {
+        readonly ProxyGenerator generator;
+
+        public ProxyFactory() : this(new ProxyGenerator())
+        {
+        }
+
+        public ProxyFactory(ProxyGenerator generator)
+        {
+            this.generator = generator;
+        }
+
+        public TypeToProxy create_proxy_for<TypeToProxy>(TypeToProxy implementation,
+                                                         IEnumerable<IInterceptor> interceptors)
+        {
+            return generator.CreateInterfaceProxyWithTarget<TypeToProxy>(implementation, interceptors.ToArray());
+        }
+
+        public TypeToProxy create_proxy_for<TypeToProxy>(Func<TypeToProxy> implementation, IEnumerable<IInterceptor> interceptors)
+        {
+            var proxy = create_proxy_for<TypeToProxy>(() => new LazyLoadedInterceptor<TypeToProxy>(implementation));
+            return create_proxy_for(proxy, interceptors);
+        }
+
+        static T create_proxy_for<T>(Func<IInterceptor> interceptor)
+        {
+            return new ProxyGenerator().CreateInterfaceProxyWithoutTarget<T>(interceptor());
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -236,6 +236,7 @@
     <Compile Include="Infrastructure\proxies\IProxyFactory.cs" />
     <Compile Include="Infrastructure\proxies\ProxyBuilder.cs" />
     <Compile Include="Infrastructure\proxies\ProxyBuilderSpecs.cs" />
+    <Compile Include="Infrastructure\proxies\ProxyFactory.cs" />
     <Compile Include="Infrastructure\registries\default_registry.cs" />
     <Compile Include="Infrastructure\registries\default_registry_specs.cs" />
     <Compile Include="Domain\accounting\billing\Bill.cs" />