|
From: <fab...@us...> - 2009-06-09 22:06:40
|
Revision: 4436
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4436&view=rev
Author: fabiomaulo
Date: 2009-06-09 22:05:38 +0000 (Tue, 09 Jun 2009)
Log Message:
-----------
First step to have injectable ICollectionTypeFactory
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/NHibernate.csproj
trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs
trunk/nhibernate/src/NHibernate/Util/ReflectHelper.cs
trunk/nhibernate/src/NHibernate.Test/UtilityTest/ReflectHelperFixture.cs
Added Paths:
-----------
trunk/nhibernate/src/NHibernate/Type/CollectionTypeFactory.cs
trunk/nhibernate/src/NHibernate/Type/ICollectionTypeFactory.cs
Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-06-09 19:26:22 UTC (rev 4435)
+++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-06-09 22:05:38 UTC (rev 4436)
@@ -603,6 +603,8 @@
<Compile Include="Tool\hbm2ddl\SchemaMetadataUpdater.cs" />
<Compile Include="Transaction\AdoNetWithDistrubtedTransactionFactory.cs" />
<Compile Include="Transform\ToListResultTransformer.cs" />
+ <Compile Include="Type\CollectionTypeFactory.cs" />
+ <Compile Include="Type\ICollectionTypeFactory.cs" />
<Compile Include="Util\NullableDictionary.cs" />
<Compile Include="Hql\Ast\ANTLR\Util\PathHelper.cs" />
<Compile Include="Hql\Ast\ANTLR\Util\SyntheticAndFactory.cs" />
Added: trunk/nhibernate/src/NHibernate/Type/CollectionTypeFactory.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Type/CollectionTypeFactory.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate/Type/CollectionTypeFactory.cs 2009-06-09 22:05:38 UTC (rev 4436)
@@ -0,0 +1,103 @@
+using System.Collections;
+using System.Collections.Generic;
+
+namespace NHibernate.Type
+{
+ public class CollectionTypeFactory : ICollectionTypeFactory
+ {
+ public virtual CollectionType Array(string role, string propertyRef, bool embedded, System.Type elementClass)
+ {
+ return new ArrayType(role, propertyRef, elementClass, embedded);
+ }
+
+ public virtual CollectionType Bag(string role, string propertyRef, bool embedded)
+ {
+ return new BagType(role, propertyRef, embedded);
+ }
+
+ public virtual CollectionType Bag<T>(string role, string propertyRef, bool embedded)
+ {
+ return new GenericBagType<T>(role, propertyRef);
+ }
+
+ public virtual CollectionType List(string role, string propertyRef, bool embedded)
+ {
+ return new ListType(role, propertyRef, embedded);
+ }
+
+ public virtual CollectionType List<T>(string role, string propertyRef, bool embedded)
+ {
+ return new GenericListType<T>(role, propertyRef);
+ }
+
+ public virtual CollectionType IdBag(string role, string propertyRef, bool embedded)
+ {
+ return new IdentifierBagType(role, propertyRef, embedded);
+ }
+
+ public virtual CollectionType IdBag<T>(string role, string propertyRef, bool embedded)
+ {
+ return new GenericIdentifierBagType<T>(role, propertyRef);
+ }
+
+ public virtual CollectionType Set(string role, string propertyRef, bool embedded)
+ {
+ return new SetType(role, propertyRef, embedded);
+ }
+
+ public virtual CollectionType OrderedSet(string role, string propertyRef, bool embedded)
+ {
+ return new OrderedSetType(role, propertyRef, embedded);
+ }
+
+ public virtual CollectionType SortedSet(string role, string propertyRef, bool embedded, IComparer comparer)
+ {
+ return new SortedSetType(role, propertyRef, comparer, embedded);
+ }
+
+ public virtual CollectionType Set<T>(string role, string propertyRef, bool embedded)
+ {
+ return new GenericSetType<T>(role, propertyRef);
+ }
+
+ public virtual CollectionType SortedSet<T>(string role, string propertyRef, bool embedded, IComparer<T> comparer)
+ {
+ return new GenericSortedSetType<T>(role, propertyRef, comparer);
+ }
+
+ public virtual CollectionType OrderedSet<T>(string role, string propertyRef, bool embedded)
+ {
+ return new GenericOrderedSetType<T>(role, propertyRef);
+ }
+
+ public virtual CollectionType Map(string role, string propertyRef, bool embedded)
+ {
+ return new MapType(role, propertyRef, embedded);
+ }
+
+ public virtual CollectionType OrderedMap(string role, string propertyRef, bool embedded)
+ {
+ return new OrderedMapType(role, propertyRef, embedded);
+ }
+
+ public virtual CollectionType SortedMap(string role, string propertyRef, bool embedded, IComparer comparer)
+ {
+ return new SortedMapType(role, propertyRef, comparer, embedded);
+ }
+
+ public virtual CollectionType Map<TKey, TValue>(string role, string propertyRef, bool embedded)
+ {
+ return new GenericMapType<TKey, TValue>(role, propertyRef);
+ }
+
+ public virtual CollectionType SortedDictionary<TKey, TValue>(string role, string propertyRef, bool embedded, IComparer<TKey> comparer)
+ {
+ return new GenericSortedDictionaryType<TKey, TValue>(role, propertyRef, comparer);
+ }
+
+ public virtual CollectionType SortedList<TKey, TValue>(string role, string propertyRef, bool embedded, IComparer<TKey> comparer)
+ {
+ return new GenericSortedListType<TKey, TValue>(role, propertyRef, comparer);
+ }
+ }
+}
\ No newline at end of file
Added: trunk/nhibernate/src/NHibernate/Type/ICollectionTypeFactory.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Type/ICollectionTypeFactory.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate/Type/ICollectionTypeFactory.cs 2009-06-09 22:05:38 UTC (rev 4436)
@@ -0,0 +1,248 @@
+using System.Collections;
+using System.Collections.Generic;
+namespace NHibernate.Type
+{
+ /// <summary>
+ /// Type factory for collections types.
+ /// </summary>
+ public interface ICollectionTypeFactory
+ {
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="System.Array"/>.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="elementClass">The <see cref="System.Type"/> to use to create the array.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// An <see cref="ArrayType"/> for the specified role.
+ /// </returns>
+ CollectionType Array(string role, string propertyRef, bool embedded, System.Type elementClass);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="IList"/>
+ /// with bag semantics.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="BagType"/> for the specified role.
+ /// </returns>
+ CollectionType Bag(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an
+ /// <see cref="System.Collections.Generic.IList{T}"/> with bag semantics.
+ /// </summary>
+ /// <typeparam name="T">The type of elements in the list.</typeparam>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">
+ /// The name of the property in the owner object containing the collection ID,
+ /// or <see langword="null" /> if it is the primary key.
+ /// </param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="GenericBagType{T}"/> for the specified role.
+ /// </returns>
+ CollectionType Bag<T>(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="IList"/>.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="ListType"/> for the specified role.
+ /// </returns>
+ CollectionType List(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an
+ /// <see cref="System.Collections.Generic.IList<T>"/> with list
+ /// semantics.
+ /// </summary>
+ /// <typeparam name="T">The type of elements in the list.</typeparam>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">
+ /// The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.
+ /// </param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="ListType"/> for the specified role.
+ /// </returns>
+ CollectionType List<T>(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="IList"/>
+ /// with id-bag semantics.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="IdentifierBagType"/> for the specified role.
+ /// </returns>
+ CollectionType IdBag(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an
+ /// <see cref="System.Collections.Generic.IList{T}"/> with identifier
+ /// bag semantics.
+ /// </summary>
+ /// <typeparam name="T">The type of elements in the list.</typeparam>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.
+ /// </param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="GenericIdentifierBagType{T}"/> for the specified role.
+ /// </returns>
+ CollectionType IdBag<T>(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="Iesi.Collections.ISet"/>.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="SetType"/> for the specified role.
+ /// </returns>
+ CollectionType Set(string role, string propertyRef, bool embedded);
+
+ CollectionType OrderedSet(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="Iesi.Collections.ISet"/>
+ /// that is sorted by an <see cref="IComparer"/>.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="comparer">The <see cref="IComparer"/> that does the sorting.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="SortedSetType"/> for the specified role.
+ /// </returns>
+ CollectionType SortedSet(string role, string propertyRef, bool embedded, IComparer comparer);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="Iesi.Collections.Generic.ISet{T}" />.
+ /// </summary>
+ /// <typeparam name="T">The type of elements in the collection.</typeparam>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>A <see cref="GenericSetType{T}" /> for the specified role.</returns>
+ CollectionType Set<T>(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for a sorted <see cref="Iesi.Collections.Generic.ISet{T}" />.
+ /// </summary>
+ /// <typeparam name="T">The type of elements in the collection.</typeparam>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <param name="comparer">The <see cref="System.Collections.Generic.IComparer{T}" /> to use for the set.</param>
+ /// <returns>A <see cref="GenericSetType{T}" /> for the specified role.</returns>
+ CollectionType SortedSet<T>(string role, string propertyRef, bool embedded, IComparer<T> comparer);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an ordered <see cref="Iesi.Collections.Generic.ISet{T}" />.
+ /// </summary>
+ /// <typeparam name="T">The type of elements in the collection.</typeparam>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">
+ /// The name of the property in the owner object containing the collection ID,
+ /// or <see langword="null" /> if it is the primary key.
+ /// </param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>A <see cref="GenericSetType{T}" /> for the specified role.</returns>
+ CollectionType OrderedSet<T>(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="IDictionary"/>.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">
+ /// The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="MapType"/> for the specified role.
+ /// </returns>
+ CollectionType Map(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="IDictionary"/>
+ /// that maintains insertion order of elements.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="OrderedMapType"/> for the specified role.
+ /// </returns>
+ CollectionType OrderedMap(string role, string propertyRef, bool embedded);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an <see cref="IDictionary"/>
+ /// that is sorted by an <see cref="IComparer"/>.
+ /// </summary>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.</param>
+ /// <param name="comparer">The <see cref="IComparer"/> that does the sorting.</param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="SortedMapType"/> for the specified role.
+ /// </returns>
+ CollectionType SortedMap(string role, string propertyRef, bool embedded, IComparer comparer);
+
+ /// <summary>
+ /// Creates a new <see cref="CollectionType"/> for an
+ /// <see cref="System.Collections.Generic.IDictionary<TKey,TValue>"/>.
+ /// </summary>
+ /// <typeparam name="TKey">The type of keys in the dictionary.</typeparam>
+ /// <typeparam name="TValue">The type of values in the dictionary.</typeparam>
+ /// <param name="role">The role the collection is in.</param>
+ /// <param name="propertyRef">The name of the property in the
+ /// owner object containing the collection ID, or <see langword="null" /> if it is
+ /// the primary key.<
+ /// /param>
+ /// <param name="embedded">Is embedded in XML (not supported yet)</param>
+ /// <returns>
+ /// A <see cref="MapType"/> for the specified role.
+ /// </returns>
+ CollectionType Map<TKey, TValue>(string role, string propertyRef, bool embedded);
+
+
+ CollectionType SortedDictionary<TKey, TValue>(string role, string propertyRef, bool embedded, IComparer<TKey> comparer);
+ CollectionType SortedList<TKey, TValue>(string role, string propertyRef, bool embedded, IComparer<TKey> comparer);
+ }
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs 2009-06-09 19:26:22 UTC (rev 4435)
+++ trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs 2009-06-09 22:05:38 UTC (rev 4436)
@@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
+using System.Reflection;
using NHibernate.Classic;
using NHibernate.Engine;
using NHibernate.Intercept;
@@ -36,6 +37,9 @@
private static readonly char[] precisionScaleSplit = new char[] { '(', ')', ',' };
private static readonly char[] lengthSplit = new char[] { '(', ')' };
+ private static readonly ICollectionTypeFactory collectionTypeFactory;
+ private static readonly System.Type[] GenericCollectionSimpleSignature = new[] { typeof(string), typeof(string), typeof(bool) };
+
/*
* Maps the string representation of the type to the IType. The string
* representation is how the type will appear in the mapping file and in
@@ -92,6 +96,10 @@
/// <summary></summary>
static TypeFactory()
{
+ collectionTypeFactory =
+ (ICollectionTypeFactory)
+ Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(typeof (CollectionTypeFactory));
+
//basicTypes.Add(NHibernate.Blob.Name, NHibernate.Blob);
//basicTypes.Add(NHibernate.Clob.Name, NHibernate.Clob);
@@ -646,346 +654,134 @@
return new ManyToOneType(persistentClass, uniqueKeyPropertyName, lazy, unwrapProxy, isEmbeddedInXML, ignoreNotFound);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="System.Array"/>.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="elementClass">The <see cref="System.Type"/> to use to create the array.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// An <see cref="ArrayType"/> for the specified role.
- /// </returns>
public static CollectionType Array(string role, string propertyRef, bool embedded, System.Type elementClass)
{
- return new ArrayType(role, propertyRef, elementClass, embedded);
+ return collectionTypeFactory.Array(role, propertyRef, embedded, elementClass);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="IList"/>.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// A <see cref="ListType"/> for the specified role.
- /// </returns>
public static CollectionType List(string role, string propertyRef, bool embedded)
{
- return new ListType(role, propertyRef, embedded);
+ return collectionTypeFactory.List(role, propertyRef, embedded);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="IList"/>
- /// with bag semantics.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// A <see cref="BagType"/> for the specified role.
- /// </returns>
public static CollectionType Bag(string role, string propertyRef, bool embedded)
{
- return new BagType(role, propertyRef, embedded);
+ return collectionTypeFactory.Bag(role, propertyRef, embedded);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="IList"/>
- /// with id-bag semantics.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// A <see cref="IdentifierBagType"/> for the specified role.
- /// </returns>
public static CollectionType IdBag(string role, string propertyRef, bool embedded)
{
- return new IdentifierBagType(role, propertyRef, embedded);
+ return collectionTypeFactory.IdBag(role, propertyRef, embedded);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="IDictionary"/>.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// A <see cref="MapType"/> for the specified role.
- /// </returns>
public static CollectionType Map(string role, string propertyRef, bool embedded)
{
- return new MapType(role, propertyRef, embedded);
+ return collectionTypeFactory.Map(role, propertyRef, embedded);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="Iesi.Collections.ISet"/>.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// A <see cref="SetType"/> for the specified role.
- /// </returns>
public static CollectionType Set(string role, string propertyRef, bool embedded)
{
- return new SetType(role, propertyRef, embedded);
+ return collectionTypeFactory.Set(role, propertyRef, embedded);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="IDictionary"/>
- /// that is sorted by an <see cref="IComparer"/>.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="comparer">The <see cref="IComparer"/> that does the sorting.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// A <see cref="SortedMapType"/> for the specified role.
- /// </returns>
public static CollectionType SortedMap(string role, string propertyRef, bool embedded, IComparer comparer)
{
- return new SortedMapType(role, propertyRef, comparer, embedded);
+ return collectionTypeFactory.SortedMap(role, propertyRef, embedded, comparer);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="IDictionary"/>
- /// that maintains insertion order of elements.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// A <see cref="OrderedMapType"/> for the specified role.
- /// </returns>
public static CollectionType OrderedMap(string role, string propertyRef, bool embedded)
{
- return new OrderedMapType(role, propertyRef, embedded);
+ return collectionTypeFactory.OrderedMap(role, propertyRef, embedded);
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="Iesi.Collections.ISet"/>
- /// that is sorted by an <see cref="IComparer"/>.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="comparer">The <see cref="IComparer"/> that does the sorting.</param>
- /// <param name="embedded"></param>
- /// <returns>
- /// A <see cref="SortedSetType"/> for the specified role.
- /// </returns>
public static CollectionType SortedSet(string role, string propertyRef, bool embedded, IComparer comparer)
{
- return new SortedSetType(role, propertyRef, comparer, embedded);
+ return collectionTypeFactory.SortedSet(role, propertyRef, embedded, comparer);
}
public static CollectionType OrderedSet(string role, string propertyRef, bool embedded)
{
- return new OrderedSetType(role, propertyRef, embedded);
+ return collectionTypeFactory.OrderedSet(role, propertyRef, embedded);
}
-
-
- private static CollectionType CreateCollectionType(
- System.Type genericCollectionType,
- string role,
- string propertyRef,
- params System.Type[] typeArguments)
+ public static CollectionType GenericBag(string role, string propertyRef, System.Type elementClass)
{
- return
- (CollectionType)
- Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(
- genericCollectionType.MakeGenericType(typeArguments), role, propertyRef);
- }
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("Bag", new[] {elementClass},
+ GenericCollectionSimpleSignature);
- private static CollectionType CreateSortedCollectionType(
- System.Type genericCollectionType,
- string role,
- string propertyRef,
- object comparer,
- params System.Type[] typeArguments)
- {
- return
- (CollectionType)
- Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(
- genericCollectionType.MakeGenericType(typeArguments), role, propertyRef, comparer);
+ return (CollectionType) mi.Invoke(collectionTypeFactory, new object[] { role, propertyRef, false });
}
- private static CollectionType CreateOrderedCollectionType(System.Type genericCollectionType,
- string role,
- string propertyRef,
- params System.Type[] typeArguments)
+ public static CollectionType GenericIdBag(string role, string propertyRef, System.Type elementClass)
{
- return
- (CollectionType)
- Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(
- genericCollectionType.MakeGenericType(typeArguments), role, propertyRef);
- }
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("IdBag", new[] { elementClass },
+ GenericCollectionSimpleSignature);
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an
- /// <see cref="System.Collections.Generic.IList{T}"/> with bag semantics.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="elementClass">
- /// The <see cref="System.Type"/> to use to create the
- /// <see cref="System.Collections.Generic.IList{T}"/> with.
- /// </param>
- /// <returns>
- /// A <see cref="GenericBagType{T}"/> for the specified role.
- /// </returns>
- public static CollectionType GenericBag(string role, string propertyRef, System.Type elementClass)
- {
- return CreateCollectionType(typeof(GenericBagType<>), role, propertyRef, elementClass);
+ return (CollectionType)mi.Invoke(collectionTypeFactory, new object[] { role, propertyRef, false });
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an
- /// <see cref="System.Collections.Generic.IList{T}"/> with identifier
- /// bag semantics.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="elementClass">
- /// The <see cref="System.Type"/> to use to create the
- /// <see cref="System.Collections.Generic.IList{T}"/> with.
- /// </param>
- /// <returns>
- /// A <see cref="GenericIdentifierBagType{T}"/> for the specified role.
- /// </returns>
- public static CollectionType GenericIdBag(string role, string propertyRef, System.Type elementClass)
- {
- return CreateCollectionType(typeof(GenericIdentifierBagType<>), role, propertyRef, elementClass);
- }
-
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an
- /// <see cref="System.Collections.Generic.IList<T>"/> with list
- /// semantics.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="elementClass">
- /// The <see cref="System.Type"/> to use to create the
- /// <see cref="System.Collections.Generic.IList<T>"/> with.
- /// </param>
- /// <returns>
- /// A <see cref="ListType"/> for the specified role.
- /// </returns>
public static CollectionType GenericList(string role, string propertyRef, System.Type elementClass)
{
- return CreateCollectionType(typeof(GenericListType<>), role, propertyRef, elementClass);
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("List", new[] { elementClass },
+ GenericCollectionSimpleSignature);
+
+ return (CollectionType)mi.Invoke(collectionTypeFactory, new object[] { role, propertyRef, false });
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an
- /// <see cref="System.Collections.Generic.IDictionary<TKey,TValue>"/>.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="indexClass">
- /// The <see cref="System.Type"/> to use as the <c>TKey</c> to create the
- /// <see cref="System.Collections.Generic.IDictionary<TKey,TValue>"/> with.
- /// </param>
- /// <param name="elementClass">
- /// The <see cref="System.Type"/> to use as the <c>TValue</c> to create the
- /// <see cref="System.Collections.Generic.IDictionary<TKey,TValue>"/> with.
- /// </param>
- /// <returns>
- /// A <see cref="MapType"/> for the specified role.
- /// </returns>
public static CollectionType GenericMap(string role, string propertyRef, System.Type indexClass,
System.Type elementClass)
{
- return CreateCollectionType(typeof(GenericMapType<,>), role, propertyRef, indexClass, elementClass);
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("Map", new[] {indexClass, elementClass },
+ GenericCollectionSimpleSignature);
+
+ return (CollectionType)mi.Invoke(collectionTypeFactory, new object[] { role, propertyRef, false });
}
public static CollectionType GenericSortedList(string role, string propertyRef, object comparer,
System.Type indexClass, System.Type elementClass)
{
- return
- CreateSortedCollectionType(typeof(GenericSortedListType<,>), role, propertyRef, comparer, indexClass, elementClass);
+ var signature = new[] { typeof(string), typeof(string), typeof(bool), typeof(IComparer<>).MakeGenericType(indexClass) };
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("SortedList", new[] { indexClass, elementClass },
+ signature);
+
+ return (CollectionType)mi.Invoke(collectionTypeFactory, new[] { role, propertyRef, false, comparer });
}
public static CollectionType GenericSortedDictionary(string role, string propertyRef, object comparer,
System.Type indexClass, System.Type elementClass)
{
- return
- CreateSortedCollectionType(typeof(GenericSortedDictionaryType<,>), role, propertyRef, comparer, indexClass,
- elementClass);
+ var signature = new[] { typeof(string), typeof(string), typeof(bool), typeof(IComparer<>).MakeGenericType(indexClass) };
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("SortedDictionary", new[] { indexClass, elementClass },
+ signature);
+
+ return (CollectionType)mi.Invoke(collectionTypeFactory, new[] { role, propertyRef, false, comparer });
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an <see cref="Iesi.Collections.Generic.ISet{T}" />.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="elementClass">The type of the set elements.</param>
- /// <returns>A <see cref="GenericSetType{T}" /> for the specified role.</returns>
public static CollectionType GenericSet(string role, string propertyRef, System.Type elementClass)
{
- return CreateCollectionType(typeof(GenericSetType<>), role, propertyRef, elementClass);
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("Set", new[] { elementClass },
+ GenericCollectionSimpleSignature);
+
+ return (CollectionType)mi.Invoke(collectionTypeFactory, new object[] { role, propertyRef, false });
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for a sorted <see cref="Iesi.Collections.Generic.ISet{T}" />.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="comparer">The <see cref="System.Collections.Generic.IComparer{T}" /> to use for the set.</param>
- /// <param name="elementType">The type of the elements in the set.</param>
- /// <returns>A <see cref="GenericSetType{T}" /> for the specified role.</returns>
public static CollectionType GenericSortedSet(string role, string propertyRef, object comparer,
- System.Type elementType)
+ System.Type elementClass)
{
- return CreateSortedCollectionType(typeof(GenericSortedSetType<>), role, propertyRef, comparer, elementType);
+ var signature = new[] { typeof(string), typeof(string), typeof(bool), typeof(IComparer<>).MakeGenericType(elementClass) };
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("SortedSet", new[] { elementClass },
+ signature);
+
+ return (CollectionType)mi.Invoke(collectionTypeFactory, new[] { role, propertyRef, false, comparer });
}
- /// <summary>
- /// Creates a new <see cref="CollectionType"/> for an ordered <see cref="Iesi.Collections.Generic.ISet{T}" />.
- /// </summary>
- /// <param name="role">The role the collection is in.</param>
- /// <param name="propertyRef">The name of the property in the
- /// owner object containing the collection ID, or <see langword="null" /> if it is
- /// the primary key.</param>
- /// <param name="elementType">The type of the elements in the set.</param>
- /// <returns>A <see cref="GenericSetType{T}" /> for the specified role.</returns>
public static CollectionType GenericOrderedSet(string role, string propertyRef,
- System.Type elementType)
+ System.Type elementClass)
{
- return CreateOrderedCollectionType(typeof(GenericOrderedSetType<>), role, propertyRef, elementType);
+ MethodInfo mi = ReflectHelper.GetGenericMethodFrom<ICollectionTypeFactory>("OrderedSet", new[] { elementClass },
+ GenericCollectionSimpleSignature);
+
+ return (CollectionType)mi.Invoke(collectionTypeFactory, new object[] { role, propertyRef, false });
}
/// <summary> Deep copy a series of values from one array to another... </summary>
Modified: trunk/nhibernate/src/NHibernate/Util/ReflectHelper.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Util/ReflectHelper.cs 2009-06-09 19:26:22 UTC (rev 4435)
+++ trunk/nhibernate/src/NHibernate/Util/ReflectHelper.cs 2009-06-09 22:05:38 UTC (rev 4436)
@@ -563,5 +563,36 @@
return null;
}
+
+ public static MethodInfo GetGenericMethodFrom<T>(string methodName, System.Type[] genericArgs, System.Type[] signature)
+ {
+ MethodInfo result = null;
+ MethodInfo[] methods = typeof (T).GetMethods();
+ foreach (var method in methods)
+ {
+ if (method.Name.Equals(methodName) && method.IsGenericMethod
+ && signature.Length == method.GetParameters().Length
+ && method.GetGenericArguments().Length == genericArgs.Length)
+ {
+ bool foundCandidate = true;
+ result = method.MakeGenericMethod(genericArgs);
+
+ ParameterInfo[] ms = result.GetParameters();
+ for (int i = 0; i < signature.Length; i++)
+ {
+ if (ms[i].ParameterType != signature[i])
+ {
+ foundCandidate = false;
+ }
+ }
+
+ if (foundCandidate)
+ {
+ return result;
+ }
+ }
+ }
+ return result;
+ }
}
}
Modified: trunk/nhibernate/src/NHibernate.Test/UtilityTest/ReflectHelperFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/UtilityTest/ReflectHelperFixture.cs 2009-06-09 19:26:22 UTC (rev 4435)
+++ trunk/nhibernate/src/NHibernate.Test/UtilityTest/ReflectHelperFixture.cs 2009-06-09 22:05:38 UTC (rev 4436)
@@ -3,6 +3,7 @@
using NHibernate.DomainModel;
using NHibernate.Util;
using NUnit.Framework;
+using System.Collections.Generic;
namespace NHibernate.Test.UtilityTest
{
@@ -140,8 +141,26 @@
Assert.That(ReflectHelper.TryGetMethod(new[] { typeof(IMyBaseInterface), typeof(IMyInterface) }, mddg), Is.Not.Null);
Assert.That(ReflectHelper.TryGetMethod(new[] { typeof(IMyBaseInterface), typeof(IMyInterface) }, mdds), Is.Null);
}
+
+ [Test]
+ public void GetGenericMethodFrom()
+ {
+ var signature = new[] {typeof (string), typeof (string), typeof (bool)};
+ Assert.That(ReflectHelper.GetGenericMethodFrom<ISomething>("List", new[] {typeof (BRhf)}, signature), Is.Not.Null);
+ Assert.That(ReflectHelper.GetGenericMethodFrom<ISomething>("List", new[] { typeof(int), typeof(string) }, signature), Is.Not.Null);
+ Assert.That(ReflectHelper.GetGenericMethodFrom<ISomething>("List", new[] { typeof(int), typeof(string) }
+ , new[] { typeof(string), typeof(string), typeof(bool), typeof(IComparer<>).MakeGenericType(typeof(int)) }), Is.Not.Null);
+ }
}
+ public interface ISomething
+ {
+ int List(string role, string propertyRef, bool embedded);
+ int List<T>(string role, string propertyRef, bool embedded);
+ int List<TK, TV>(string role, string propertyRef, bool embedded);
+ int List<TK, TV>(string role, string propertyRef, bool embedded, IComparer<TK> comparer);
+ }
+
public class ARhf
{
public override bool Equals(object obj)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|