Author: gbayon
Date: Sun Nov 12 09:00:49 2006
New Revision: 473989

URL: http://svn.apache.org/viewvc?view=rev&rev=473989
Log:
- Internal refactoring

Added:
    ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/DocumentCollection.cs
    
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/GenericDocumentCollection.cs
    
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/TypeUtilsTest.cs
Modified:
    
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.2005.csproj
    ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/TypeUtils.cs
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/ResultMapping/ResultProperty.cs
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/DataExchange/DataExchangeFactory.cs
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/PropertStrategy/GroupByStrategy.cs
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/ResultStrategy/GroupByStrategy.cs

Added: ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/DocumentCollection.cs
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/DocumentCollection.cs?view=auto&rev=473989
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/DocumentCollection.cs 
(added)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/DocumentCollection.cs 
Sun Nov 12 09:00:49 2006
@@ -0,0 +1,61 @@
+
+using System.Collections;
+
+namespace IBatisNet.Common.Test.Domain
+{
+    public class DocumentCollection : CollectionBase
+    {
+
+        public Document this[int index]
+        {
+            get { return (Document)List[index]; }
+            set { List[index] = value; }
+        }
+
+        public int Add(Document value)
+        {
+            return List.Add(value);
+        }
+
+        public void AddRange(Document[] value)
+        {
+            for (int i = 0; i < value.Length; i++)
+            {
+                Add(value[i]);
+            }
+        }
+
+        public void AddRange(DocumentCollection value)
+        {
+            for (int i = 0; i < value.Count; i++)
+            {
+                Add(value[i]);
+            }
+        }
+
+        public bool Contains(Document value)
+        {
+            return List.Contains(value);
+        }
+
+        public void CopyTo(Document[] array, int index)
+        {
+            List.CopyTo(array, index);
+        }
+
+        public int IndexOf(Document value)
+        {
+            return List.IndexOf(value);
+        }
+
+        public void Insert(int index, Document value)
+        {
+            List.Insert(index, value);
+        }
+
+        public void Remove(Document value)
+        {
+            List.Remove(value);
+        }
+    }
+}

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/GenericDocumentCollection.cs
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/GenericDocumentCollection.cs?view=auto&rev=473989
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/GenericDocumentCollection.cs
 (added)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/Domain/GenericDocumentCollection.cs
 Sun Nov 12 09:00:49 2006
@@ -0,0 +1,10 @@
+#if dotnet2
+using System.Collections.Generic;
+
+namespace IBatisNet.Common.Test.Domain
+{
+    public class GenericDocumentCollection : List<Document>
+    {
+    }
+}
+#endif

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.2005.csproj
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.2005.csproj?view=diff&rev=473989&r1=473988&r2=473989
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.2005.csproj 
(original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/IBatisNet.Common.Test.2005.csproj 
Sun Nov 12 09:00:49 2006
@@ -102,6 +102,8 @@
     </Compile>
     <Compile Include="Domain\Book.cs" />
     <Compile Include="Domain\Document.cs" />
+    <Compile Include="Domain\DocumentCollection.cs" />
+    <Compile Include="Domain\GenericDocumentCollection.cs" />
     <Compile Include="Domain\Item.cs" />
     <Compile Include="Domain\Order.cs" />
     <Compile Include="Domain\Property.cs" />
@@ -146,6 +148,7 @@
     </Compile>
     <Compile Include="NUnit\CommonTests\Utilities\Timer.cs" />
     <Compile Include="NUnit\CommonTests\Utilities\TypeResolverTest.cs" />
+    <Compile Include="NUnit\CommonTests\Utilities\TypeUtilsTest.cs" />
     <None Include="bin\Debug\dao_Access_OleDb.config" />
     <None Include="bin\Debug\dao_MSSQL_Odbc.config" />
     <None Include="bin\Debug\dao_MSSQL_OleDb.config" />

Added: 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/TypeUtilsTest.cs
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/TypeUtilsTest.cs?view=auto&rev=473989
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/TypeUtilsTest.cs
 (added)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.Common.Test/NUnit/CommonTests/Utilities/TypeUtilsTest.cs
 Sun Nov 12 09:00:49 2006
@@ -0,0 +1,88 @@
+
+#if dotnet2
+using System;
+using System.Collections;
+using System.Collections.Generic;
+#endif
+using IBatisNet.Common.Test.Domain;
+using IBatisNet.Common.Utilities;
+using NUnit.Framework;
+
+namespace IBatisNet.Common.Test.NUnit.CommonTests.Utilities
+{
+    [TestFixture]
+    public class TypeUtilsTest
+    {
+        
+#if dotnet2        
+
+
+        /// <summary>
+        /// Test IsImplementGenericIListInterface.
+        /// </summary>
+        [Test]
+        public void IsImplementGenericIListInterfaceOnIList()
+        {
+            Type memberType = typeof(IList<Document>);
+
+            bool isGenericIList = 
TypeUtils.IsImplementGenericIListInterface(memberType);
+            Assert.IsTrue(isGenericIList);
+
+            //Console.Write(typeof(IList).IsAssignableFrom(typeof(Array)));
+        }
+
+        /// <summary>
+        /// Test IsImplementGenericIListInterface.
+        /// </summary>
+        [Test]
+        public void IsImplementGenericIListInterfaceOnList()
+        {
+            //GenericDocumentCollection documents = new 
GenericDocumentCollection();
+            Type memberType = typeof(List<Document>);
+
+            bool isGenericIList = 
TypeUtils.IsImplementGenericIListInterface(memberType);
+
+            Assert.IsTrue(isGenericIList);
+        }
+
+        /// <summary>
+        /// Test IsImplementGenericIListInterface.
+        /// </summary>
+        [Test]
+        public void IsImplementGenericIListInterfaceOnCustomList()
+        {
+            Type memberType = typeof(GenericDocumentCollection);
+
+            bool isGenericIList = 
TypeUtils.IsImplementGenericIListInterface(memberType);
+
+            Assert.IsTrue(isGenericIList);
+        }
+
+        /// <summary>
+        /// Test IsImplementGenericIListInterface.
+        /// </summary>
+        [Test]
+        public void IsImplementGenericIListInterfaceOnArray()
+        {
+            Type memberType = typeof(Document[]);
+
+            bool isGenericIList = 
TypeUtils.IsImplementGenericIListInterface(memberType);
+
+            Assert.IsTrue(isGenericIList);
+        }
+
+        /// <summary>
+        /// Test IsImplementGenericIListInterface.
+        /// </summary>
+        [Test]
+        public void IsImplementGenericIListInterfaceOnInt()
+        {
+            Type memberType = typeof(int);
+
+            bool isGenericIList = 
TypeUtils.IsImplementGenericIListInterface(memberType);
+
+            Assert.IsFalse(isGenericIList);
+        }
+#endif
+    }
+}

Modified: ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/TypeUtils.cs
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/TypeUtils.cs?view=diff&rev=473989&r1=473988&r2=473989
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/TypeUtils.cs (original)
+++ ibatis/trunk/cs/mapper/IBatisNet.Common/Utilities/TypeUtils.cs Sun Nov 12 
09:00:49 2006
@@ -24,7 +24,9 @@
 #endregion
 
 using System;
-
+#if dotnet2
+using System.Collections.Generic;
+#endif
 using IBatisNet.Common.Utilities.TypesResolver;
 
 namespace IBatisNet.Common.Utilities
@@ -214,6 +216,49 @@
 
             return resultObject;
         }
+
+        /// <summary>
+        /// Determines whether the specified type is implement generic Ilist 
interface.
+        /// </summary>
+        /// <param name="type">The type.</param>
+        /// <returns>
+        ///    <c>true</c> if the specified type is implement generic Ilist 
interface; otherwise, <c>false</c>.
+        /// </returns>
+        public static bool IsImplementGenericIListInterface(Type type)
+        {
+            bool ret = false;
+            
+            if (!type.IsGenericType)
+            {
+                ret = false;  
+            }
+
+            Type genericTypeDef = null;
+            try
+            {
+                genericTypeDef = type.GetGenericTypeDefinition();
+            }
+            catch {}
+
+            // Check if it is IList<T>
+            if (genericTypeDef!=null && 
typeof(IList<>).IsAssignableFrom(genericTypeDef))
+            {
+                ret = true;
+            }
+            else // check if one of the derived interfaces is IList<>
+            {
+                Type[] interfaceTypes = type.GetInterfaces();
+                foreach (Type interfaceType in interfaceTypes)
+                {
+                    if (interfaceType.IsGenericType &&
+                      interfaceType.GetGenericTypeDefinition() == 
typeof(IList<>))
+                    {
+                        ret = true;
+                    }
+                }
+            }                
+            return ret;
+        } 
 #endif
     }
 }

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/ResultMapping/ResultProperty.cs
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/ResultMapping/ResultProperty.cs?view=diff&rev=473989&r1=473988&r2=473989
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/ResultMapping/ResultProperty.cs
 (original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/ResultMapping/ResultProperty.cs
 Sun Nov 12 09:00:49 2006
@@ -28,10 +28,14 @@
 
 using System;
 using System.Collections;
+#if dotnet2
+using System.Collections.Generic;
+#endif
 using System.Data;
 using System.Reflection;
 using System.Xml.Serialization;
 using IBatisNet.Common.Exceptions;
+using IBatisNet.Common.Utilities;
 using IBatisNet.Common.Utilities.Objects;
 using IBatisNet.Common.Utilities.Objects.Members;
 using IBatisNet.DataMapper.Exceptions;
@@ -94,11 +98,47 @@
                private IPropertyStrategy _propertyStrategy = null;
         [NonSerialized]
         private ILazyFactory _lazyFactory = null;
+        [NonSerialized]
+        private bool _isIList = false;
+        [NonSerialized]    
+           private bool _isGenericIList = false;
+        [NonSerialized]
+        private IFactory _listFactory = null;
+           [NonSerialized]
+        private static IFactory _arrayListFactory = new ArrayListFactory();
+           
                #endregion
 
                #region Properties
 
         /// <summary>
+        /// Tell us if the member type implement generic Ilist interface.
+        /// </summary>
+        [XmlIgnore]
+        public bool IsGenericIList
+        {
+            get { return _isGenericIList; }
+        }
+
+        /// <summary>
+        /// Tell us if the member type implement Ilist interface.
+        /// </summary>
+        [XmlIgnore]
+        public bool IsIList
+        {
+            get { return _isIList; }
+        }
+        /// <summary>
+        /// List factory for <see cref="IList"/> property
+        /// </summary>
+        /// <remarks>Used by N+1 Select solution</remarks>
+        [XmlIgnore]
+        public IFactory ListFactory
+        {
+            get { return _listFactory; }
+        }
+           
+        /// <summary>
         /// The lazy loader factory
         /// </summary>
         [XmlIgnore]
@@ -349,6 +389,70 @@
                                        string memberName = 
_propertyName.Substring( _propertyName.LastIndexOf('.')+1);
                     _setAccessor = 
configScope.DataExchangeFactory.AccessorFactory.SetAccessorFactory.CreateSetAccessor(propertyInfo.ReflectedType,
 memberName);
                                }
+
+#if dotnet2
+                _isGenericIList = 
TypeUtils.IsImplementGenericIListInterface(this.MemberType);
+#endif                     
+                _isIList = typeof (IList).IsAssignableFrom(this.MemberType);
+                           
+                           // set the list factory
+#if dotnet2                       
+                           if (_isGenericIList)
+                           {
+                               if (this.MemberType.IsArray)
+                               {
+                        _listFactory = _arrayListFactory;
+                               }
+                               else
+                               {
+                        Type[] typeArgs = 
this.MemberType.GetGenericArguments();
+
+                        if (typeArgs.Length == 0)// Custom collection which 
derive from List<T>
+                                   {
+                            _listFactory = 
configScope.DataExchangeFactory.ObjectFactory.CreateFactory(this.MemberType, 
Type.EmptyTypes);
+                                   }
+                                   else
+                                   {
+                            Type genericIList = typeof(IList<>);
+                            Type interfaceListType = 
genericIList.MakeGenericType(typeArgs);
+
+                            Type genericList = typeof(List<>);
+                            Type listType = 
genericList.MakeGenericType(typeArgs);
+
+                            if ((interfaceListType == this.MemberType) || 
(listType == this.MemberType))
+                            {
+                                Type constructedType = 
genericList.MakeGenericType(typeArgs);
+                                _listFactory = 
configScope.DataExchangeFactory.ObjectFactory.CreateFactory(
+                                    constructedType,
+                                    Type.EmptyTypes);
+                            }
+                            else // Custom collection which derive from List<T>
+                            {
+                                _listFactory = 
configScope.DataExchangeFactory.ObjectFactory.CreateFactory(this.MemberType, 
Type.EmptyTypes);
+                            }  
+                                   }                               
+                               }
+                           }
+                           else 
+#endif                         
+                           if (typeof(IList).IsAssignableFrom(this.MemberType))
+                           {
+                    if (this.MemberType.IsArray)
+                    {
+                        _listFactory = _arrayListFactory;
+                    }
+                               else
+                    {
+                        if (this.MemberType == typeof(IList))
+                        {
+                            _listFactory = _arrayListFactory;
+                        }
+                        else // custom collection
+                        {
+                            _listFactory = 
configScope.DataExchangeFactory.ObjectFactory.CreateFactory(this.MemberType,    
                                                                                
               Type.EmptyTypes);
+                        }                        
+                    }
+                           }
                        }
 
                        if (this.CallBackName!=null && this.CallBackName.Length 
>0)
@@ -474,6 +578,32 @@
         }
 
         #endregion
+           
+           /// <summary>
+        /// <see cref="IFactory"/> that constracut <see cref="ArrayList"/> 
instance
+           /// </summary>
+           private class ArrayListFactory : IFactory
+           {
+
+            #region IFactory Members
+
+            /// <summary>
+            /// Create a new instance with the specified parameters
+            /// </summary>
+            /// <param name="parameters">An array of values that matches the 
number, order and type
+            /// of the parameters for this constructor.</param>
+            /// <returns>A new instance</returns>
+            /// <remarks>
+            /// If you call a constructor with no parameters, pass null.
+            /// Anyway, what you pass will be ignore.
+            /// </remarks>
+            public object CreateInstance(object[] parameters)
+            {
+              return new ArrayList();
+            }
+
+            #endregion
+        }
     }
 
 }

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/DataExchange/DataExchangeFactory.cs
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/DataExchange/DataExchangeFactory.cs?view=diff&rev=473989&r1=473988&r2=473989
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/DataExchange/DataExchangeFactory.cs 
(original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/DataExchange/DataExchangeFactory.cs 
Sun Nov 12 09:00:49 2006
@@ -126,27 +126,5 @@
                        return dataExchange;
                }
 
-#if dotnet2
-        /// <summary>
-        /// Determines whether [is implement generic Ilist interface] [the 
specified type].
-        /// </summary>
-        /// <param name="type">The type.</param>
-        /// <returns>
-        ///    <c>true</c> if [is implement generic I list interface] [the 
specified type]; otherwise, <c>false</c>.
-        /// </returns>
-        public bool IsImplementGenericIListInterface(Type type)
-        {
-            Type[] interfaceTypes = type.GetInterfaces();
-            foreach (Type interfaceType in interfaceTypes)
-            {
-                if (interfaceType.IsGenericType &&
-                  interfaceType.GetGenericTypeDefinition() == typeof(IList<>))
-                {
-                    return true;
-                }
-            }
-            return false;
-        } 
-#endif
        }
 }

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/PropertStrategy/GroupByStrategy.cs
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/PropertStrategy/GroupByStrategy.cs?view=diff&rev=473989&r1=473988&r2=473989
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/PropertStrategy/GroupByStrategy.cs
 (original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/PropertStrategy/GroupByStrategy.cs
 Sun Nov 12 09:00:49 2006
@@ -90,35 +90,8 @@
             
             if (property == null)// Create the list if need
             {
-#if dotnet2
-                if (mapping.MemberType.IsGenericType &&
-                    
typeof(IList<>).IsAssignableFrom(mapping.MemberType.GetGenericTypeDefinition()))
-                {
-                    Type genericList = typeof(List<>);
-                    Type[] typeArgs = mapping.MemberType.GetGenericArguments();
-                    Type constructedType = 
genericList.MakeGenericType(typeArgs);
-
-                    IFactory factory = 
request.DataExchangeFactory.ObjectFactory.CreateFactory(constructedType,
-                                                                               
                Type.EmptyTypes);
-                    property = factory.CreateInstance(null);
-                    mapping.SetAccessor.Set(target, property);
-                }
-                else
-#endif
-                    if (typeof(IList).IsAssignableFrom(mapping.MemberType))
-                    {
-                        if (mapping.MemberType == typeof(IList))
-                        {
-                            property = new ArrayList();
-                        }
-                        else // custom collection
-                        {
-                            IFactory factory = 
request.DataExchangeFactory.ObjectFactory.CreateFactory(mapping.MemberType,
-                                                                               
                        Type.EmptyTypes);
-                            property = factory.CreateInstance(null);
-                        }
-                        mapping.SetAccessor.Set(target, property);
-                    }
+                property = mapping.ListFactory.CreateInstance(null);
+                mapping.SetAccessor.Set(target, property);
             }
             list = (IList)property;
 

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/ResultStrategy/GroupByStrategy.cs
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/ResultStrategy/GroupByStrategy.cs?view=diff&rev=473989&r1=473988&r2=473989
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/ResultStrategy/GroupByStrategy.cs
 (original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/ResultStrategy/GroupByStrategy.cs
 Sun Nov 12 09:00:49 2006
@@ -85,22 +85,7 @@
                 for (int index = 0; index < resultMap.Properties.Count; 
index++)
                 {
                     ResultProperty resultProperty = 
resultMap.Properties[index];
-#if dotnet2
-                    if (resultProperty.MemberType.IsGenericType &&
-                        
typeof(IList<>).IsAssignableFrom(resultProperty.MemberType.GetGenericTypeDefinition()))
-                    {
-                        resultProperty.PropertyStrategy.Set(request, 
resultMap, resultProperty, ref outObject, reader, null);
-                    }
-                    else
-#endif
-                        if 
(typeof(IList).IsAssignableFrom(resultProperty.MemberType))
-                        {
-                            resultProperty.PropertyStrategy.Set(request, 
resultMap, resultProperty, ref outObject, reader, null);
-                        }
-                        else
-                        {
-                            resultProperty.PropertyStrategy.Set(request, 
resultMap, resultProperty, ref outObject, reader, null);
-                        }
+                    resultProperty.PropertyStrategy.Set(request, resultMap, 
resultProperty, ref outObject, reader, null);                   
                 }
 
                 if (buildObjects == null)


Reply via email to