src/Cassandra/Mapping/MapperFactory.cs
/// <summary>
/// Tries to get an Expression that will create an empty collection compatible with the POCO column's type if the type coming from
/// the database is a collection type.  Returns true if successful, along with the Expression in an out parameter.
/// </summary>
Tries to get an Expression that will create an empty collection compatible with the POCO column's type if the type coming from
the database is a collection type.  Returns true if successful, along with the Expression in an out parameter.
Tries to get an Expression that will create an empty collection compatible with the POCO column ' s type if the type coming from the database is a collection type . Returns true if successful along with the Expression in an out parameter .
private static bool TryGetCreateEmptyCollectionExpression(CqlColumn dbColumn, Type pocoDestType, out Expression createEmptyCollection)
        {
            createEmptyCollection = null;
            if (IsCassandraCollection(dbColumn) == false)
                return false;
            if (!pocoDestType.GetTypeInfo().IsInterface)
            {
                if (pocoDestType.IsArray)
                {
                    createEmptyCollection = Expression.NewArrayInit(pocoDestType.GetElementType());
                    return true;
                }
                if (ImplementsCollectionInterface(pocoDestType))
                {
                    try
                    {
                        createEmptyCollection = Expression.New(pocoDestType);
                        return true;
                    }
                    catch (ArgumentException)
                    {
                        return false;
                    }
                }
            }
            else
            {
                if (!pocoDestType.GetTypeInfo().IsGenericType)
                {
                    return false;
                }
                Type openGenericType = pocoDestType.GetGenericTypeDefinition();
                if (openGenericType == typeof (IDictionary<,>))
                {
                    Type dictionaryType = typeof (SortedDictionary<,>).MakeGenericType(pocoDestType.GetTypeInfo().GetGenericArguments());
                    createEmptyCollection = Expression.Convert(Expression.New(dictionaryType), pocoDestType);
                    return true;
                }
                if (openGenericType == typeof (ISet<>))
                {
                    Type setType = typeof (SortedSet<>).MakeGenericType(pocoDestType.GetTypeInfo().GetGenericArguments());
                    createEmptyCollection = Expression.Convert(Expression.New(setType), pocoDestType);
                    return true;
                }
                if (openGenericType == typeof (ICollection<>) || openGenericType == typeof (IList<>) || openGenericType == typeof (IEnumerable<>))
                {
                    Type listType = typeof (List<>).MakeGenericType(pocoDestType.GetTypeInfo().GetGenericArguments());
                    createEmptyCollection = Expression.Convert(Expression.New(listType), pocoDestType);
                    return true;
                }
            }
            return false;
        }
private static bool TryGetCreateEmptyCollectionExpression ( CqlColumn dbColumn , Type pocoDestType , out Expression createEmptyCollection ) { createEmptyCollection = null ; if ( IsCassandraCollection ( dbColumn ) == false ) return false ; if ( ! pocoDestType . GetTypeInfo ( ) . IsInterface ) { if ( pocoDestType . IsArray ) { createEmptyCollection = Expression . NewArrayInit ( pocoDestType . GetElementType ( ) ) ; return true ; } if ( ImplementsCollectionInterface ( pocoDestType ) ) { try { createEmptyCollection = Expression . New ( pocoDestType ) ; return true ; } catch ( ArgumentException ) { return false ; } } } else { if ( ! pocoDestType . GetTypeInfo ( ) . IsGenericType ) { return false ; } Type openGenericType = pocoDestType . GetGenericTypeDefinition ( ) ; if ( openGenericType == typeof ( IDictionary < , > ) ) { Type dictionaryType = typeof ( SortedDictionary < , > ) . MakeGenericType ( pocoDestType . GetTypeInfo ( ) . GetGenericArguments ( ) ) ; createEmptyCollection = Expression . Convert ( Expression . New ( dictionaryType ) , pocoDestType ) ; return true ; } if ( openGenericType == typeof ( ISet < > ) ) { Type setType = typeof ( SortedSet < > ) . MakeGenericType ( pocoDestType . GetTypeInfo ( ) . GetGenericArguments ( ) ) ; createEmptyCollection = Expression . Convert ( Expression . New ( setType ) , pocoDestType ) ; return true ; } if ( openGenericType == typeof ( ICollection < > ) || openGenericType == typeof ( IList < > ) || openGenericType == typeof ( IEnumerable < > ) ) { Type listType = typeof ( List < > ) . MakeGenericType ( pocoDestType . GetTypeInfo ( ) . GetGenericArguments ( ) ) ; createEmptyCollection = Expression . Convert ( Expression . New ( listType ) , pocoDestType ) ; return true ; } } return false ; }
Tries to get an Expression that will create an empty collection compatible with the POCO column's type if the type coming from
the database is a collection type.
Tries to get an Expression that will create an empty collection compatible with the POCO column ' s type if the type coming from the database is a collection type .
// If the DB column isn't a collection type, just bail // See if the POCO's type if something we can create an empty collection for // If an array, we know we have a constructor available // new T[] { } // Is a type implementing ICollection<T>? (this covers types implementing ISet<T>, IDictionary<T> as well) // new T(); // Type does not have an empty constructor, so just bail // See if destination type interface on the POCO is one we can create an empty object for // Handle IDictionary<T, U> // The driver currently uses SortedDictionary so we will too // (IDictionary<T, U>) new SortedDictionary<T, U>(); // Handle ISet<T> // The driver uses List (?!) but we'll use a sorted set since that's the CQL semantics // (ISet<T>) new SortedSet<T>(); // Handle ICollection<T>, IList<T>, and IEnumerable<T> // The driver uses List so we'll use that as well // (... IList<T> or ICollection<T> or IEnumerable<T> ...) new List<T>(); // We don't know what to do to create an empty collection or we don't know it's a collection
dbColumn
CqlColumn
pocoDestType
Type
Expression
out