Author: gbayon
Date: Mon Jul  4 11:35:17 2005
New Revision: 209105

URL: http://svn.apache.org/viewcvs?rev=209105&view=rev
Log:
- Added a cache for proxy type

Added:
    ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Proxy/
    
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Proxy/CachedProxyGenerator.cs
Modified:
    ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs
    ibatis/trunk/cs/mapper/IBatisNet.DataMapper/LazyLoadList.cs

Modified: ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj?rev=209105&r1=209104&r2=209105&view=diff
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj (original)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common/IBatisNet.Common.csproj Mon Jul  4 
11:35:17 2005
@@ -304,6 +304,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "Utilities\Proxy\CachedProxyGenerator.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "Utilities\TypesResolver\CachedTypeResolver.cs"
                     SubType = "Code"
                     BuildAction = "Compile"

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Proxy/CachedProxyGenerator.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Proxy/CachedProxyGenerator.cs?rev=209105&view=auto
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Proxy/CachedProxyGenerator.cs 
(added)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/Proxy/CachedProxyGenerator.cs 
Mon Jul  4 11:35:17 2005
@@ -0,0 +1,110 @@
+
+#region Apache Notice
+/*****************************************************************************
+ * $Header: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2004 - Gilles Bayon
+ *  
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ 
********************************************************************************/
+#endregion
+
+#region Using
+
+using System;
+using System.Collections;
+using System.Collections.Specialized;
+using Castle.DynamicProxy;
+using IBatisNet.Common.Exceptions;
+using IBatisNet.Common.Logging;
+
+#endregion
+
+namespace IBatisNet.Common.Utilities.Proxy
+{
+       /// <summary>
+       /// An ProxyGenerator with cache that uses the Castle.DynamicProxy 
library.
+       /// </summary>
+       public class CachedProxyGenerator : ProxyGenerator
+       {
+               private static readonly ILog _log = LogManager.GetLogger( 
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
+
+               // key = mapped type
+               // value = proxy type
+               private IDictionary _cachedProxyTypes =null;
+
+               /// <summary>
+               /// Cosntructor
+               /// </summary>
+               public CachedProxyGenerator()
+               {
+                       _cachedProxyTypes = new HybridDictionary();
+               }
+
+               /// <summary>
+               /// Generates a proxy implementing all the specified interfaces 
and
+               /// redirecting method invocations to the specifed interceptor.
+               /// </summary>
+               /// <param name="theInterface">Interface to be 
implemented</param>
+               /// <param name="interceptor">instance of <see 
cref="IInterceptor"/></param>
+               /// <param name="target"></param>
+               /// <returns>Proxy instance</returns>
+               public override object CreateProxy(Type theInterface, 
IInterceptor interceptor, object target)
+               {
+                       return CreateProxy(new Type[] {theInterface}, 
interceptor, target);
+               }
+
+               /// <summary>
+               /// Generates a proxy implementing all the specified interfaces 
and
+               /// redirecting method invocations to the specifed interceptor.
+               /// </summary>
+               /// <param name="interfaces">Array of interfaces to be 
implemented</param>
+               /// <param name="interceptor">instance of <see 
cref="IInterceptor"/></param>
+               /// <param name="target"></param>
+               /// <returns>Proxy instance</returns>
+               public override object CreateProxy(Type[] interfaces, 
IInterceptor interceptor, object target)
+               {
+                       try
+                       {
+                               System.Type proxyType = null;
+                               System.Type targetType = target.GetType();
+
+                               lock( _cachedProxyTypes.SyncRoot )
+                               {
+                                       proxyType = _cachedProxyTypes[ 
targetType ] as System.Type;
+
+                                       if( proxyType == null )
+                                       {
+                                               proxyType = 
ProxyBuilder.CreateInterfaceProxy(interfaces, targetType );
+                                               _cachedProxyTypes[ targetType ] 
= proxyType;
+                                       }
+                               }
+                               return base.CreateProxyInstance( proxyType, 
interceptor, target );
+                       }
+                       catch( Exception e )
+                       {
+                               _log.Error( "Castle Dynamic Proxy Generator 
failed", e );
+                               throw new IBatisNetException( "Castle Proxy 
Generator failed", e );
+                       }
+
+
+               }
+
+
+       }
+}

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs?rev=209105&r1=209104&r2=209105&view=diff
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs 
(original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs 
Mon Jul  4 11:35:17 2005
@@ -22,7 +22,7 @@
        [TestFixture] 
        public class CacheTest : BaseTest
        {
-                       #region SetUp & TearDown
+               #region SetUp & TearDown
 
                /// <summary>
                /// SetUp

Modified: ibatis/trunk/cs/mapper/IBatisNet.DataMapper/LazyLoadList.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/LazyLoadList.cs?rev=209105&r1=209104&r2=209105&view=diff
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.DataMapper/LazyLoadList.cs (original)
+++ ibatis/trunk/cs/mapper/IBatisNet.DataMapper/LazyLoadList.cs Mon Jul  4 
11:35:17 2005
@@ -33,6 +33,7 @@
 using IBatisNet.Common;
 using IBatisNet.Common.Logging;
 using IBatisNet.Common.Utilities.Objects;
+using IBatisNet.Common.Utilities.Proxy;
 using IBatisNet.DataMapper.MappedStatements;
 
 #endregion
@@ -56,6 +57,7 @@
                private object _loadLock = new object();
                private static ArrayList _passthroughMethods = new ArrayList();
 
+               private static CachedProxyGenerator _proxyGenerator = new 
CachedProxyGenerator();
                private static readonly ILog _logger = LogManager.GetLogger( 
MethodBase.GetCurrentMethod().DeclaringType );
                #endregion
 
@@ -103,15 +105,13 @@
                        object proxList = null;
                        IInterceptor handler = new LazyLoadList(dataSource, 
mappedSatement, param, target, propertyName);
 
-                       ProxyGenerator proxyGenerator = new ProxyGenerator();
-
                        if (mappedSatement.Statement.ListClass != null)
                        {
-                               proxList = 
proxyGenerator.CreateProxy(typeof(IList), handler, 
mappedSatement.Statement.CreateInstanceOfListClass());
+                               proxList = 
_proxyGenerator.CreateProxy(typeof(IList), handler, 
mappedSatement.Statement.CreateInstanceOfListClass());
                        }
                        else
                        {
-                               proxList = 
proxyGenerator.CreateProxy(typeof(IList), handler, new ArrayList());
+                               proxList = 
_proxyGenerator.CreateProxy(typeof(IList), handler, new ArrayList());
                        }
 
                        return (IList) proxList;


Reply via email to