/// <summary>
 /// Initializes a new instance of the <see cref="CompiledCollectionMaterializer{TCommand, TParameter, TObject, TCollection}"/> class.
 /// </summary>
 /// <param name="commandBuilder">The associated operation.</param>
 /// <param name="collectionOptions">The collection options.</param>
 /// <exception cref="NotSupportedException">Compiled materializers do not support non-default constructors</exception>
 public CompiledCollectionMaterializer(DbCommandBuilder <TCommand, TParameter> commandBuilder, CollectionOptions collectionOptions)
     : base(commandBuilder)
 {
     if (collectionOptions.HasFlag(CollectionOptions.InferConstructor))
     {
         throw new NotSupportedException("Compiled materializers do not support non-default constructors");
     }
 }
Exemplo n.º 2
0
    /// <summary>
    /// Initializes a new instance of the <see cref="CollectionMaterializer{TCommand,
    /// TParameter, TObject, TCollection}"/> class.
    /// </summary>
    /// <param name="commandBuilder">The associated operation.</param>
    /// <param name="collectionOptions">The collection options.</param>
    public ImmutableListMaterializer(DbCommandBuilder <TCommand, TParameter> commandBuilder, CollectionOptions collectionOptions)
        : base(commandBuilder)
    {
        m_CollectionOptions = collectionOptions;

        if (m_CollectionOptions.HasFlag(CollectionOptions.InferConstructor))
        {
            Constructor = InferConstructor();
        }
    }
Exemplo n.º 3
0
    public MasterDetailCollectionMaterializer(DbCommandBuilder <TCommand, TParameter> commandBuilder, string masterKeyColumn, Func <TMaster, ICollection <TDetail> > map, CollectionOptions masterOptions, CollectionOptions detailOptions) : base(commandBuilder)
    {
        m_MasterKeyColumn = masterKeyColumn;
        m_Map             = map;

        if (masterOptions.HasFlag(CollectionOptions.InferConstructor))
        {
            m_MasterConstructor = MaterializerUtilities.InferConstructor(s_MasterMetadata);
        }
        if (detailOptions.HasFlag(CollectionOptions.InferConstructor))
        {
            m_DetailConstructor = MaterializerUtilities.InferConstructor(s_DetailMetadata);
        }
    }
Exemplo n.º 4
0
        public void ContainsNullValue(
            CollectionOptions optionsWithNull, CollectionOptions optionsWithoutNull)
        {
            var withNullCount         = 10;
            var enumerableWithNull    = GetEnumerable <int?>(optionsWithNull, withNullCount);
            var enumerableWithNullArg = Guard.Argument(() => enumerableWithNull).ContainsNull();
            var nullIndex             = enumerableWithNull?.Items.TakeWhile(s => s.HasValue).Count() ?? RandomNumber;

            CheckAndReset(enumerableWithNull, containsCalled: true, enumerationCount: nullIndex + 1);

            var withoutNullCount         = optionsWithoutNull.HasFlag(CollectionOptions.Empty) ? 0 : withNullCount;
            var enumerableWithoutNull    = GetEnumerable <int?>(optionsWithoutNull, withoutNullCount);
            var enumerableWithoutNullArg = Guard.Argument(() => enumerableWithoutNull).DoesNotContainNull();

            CheckAndReset(enumerableWithoutNull, containsCalled: true, enumerationCount: withoutNullCount, enumerated: true);

            if (enumerableWithNull is null)
            {
                enumerableWithNullArg.DoesNotContainNull();
                enumerableWithoutNullArg.ContainsNull();
                return;
            }

            ThrowsArgumentException(
                enumerableWithoutNullArg,
                arg => arg.ContainsNull(),
                (arg, message) => arg.ContainsNull(e =>
            {
                Assert.Same(enumerableWithoutNull, e);
                return(message);
            }));

            CheckAndReset(enumerableWithoutNull, containsCalled: true, enumerationCount: withoutNullCount * 2, enumerated: true);

            ThrowsArgumentException(
                enumerableWithNullArg,
                arg => arg.DoesNotContainNull(),
                (arg, message) => arg.DoesNotContainNull(e =>
            {
                Assert.Same(enumerableWithNull, e);
                return(message);
            }));

            CheckAndReset(enumerableWithNull, containsCalled: true, enumerationCount: (nullIndex + 1) * 2);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionMaterializer{TCommand, TParameter, TObject, TCollection}"/> class.
        /// </summary>
        /// <param name="commandBuilder">The associated operation.</param>
        /// <param name="collectionOptions">The collection options.</param>
        public CollectionMaterializer(DbCommandBuilder <TCommand, TParameter> commandBuilder, CollectionOptions collectionOptions)
            : base(commandBuilder)
        {
            m_CollectionOptions = collectionOptions;

            if (m_CollectionOptions.HasFlag(CollectionOptions.InferConstructor))
            {
                var constructors = ObjectMetadata.Constructors.Where(x => x.Signature.Length > 0).ToList();
                if (constructors.Count == 0)
                {
                    throw new MappingException($"Type {typeof(TObject).Name} has does not have any non-default constructors.");
                }
                if (constructors.Count > 1)
                {
                    throw new MappingException($"Type {typeof(TObject).Name} has more than one non-default constructor. Please use the WithConstructor method to specify which one to use.");
                }
                ConstructorSignature = constructors[0].Signature;
            }
        }
Exemplo n.º 6
0
        private static ITestEnumerable <T> GetEnumerable <T>(CollectionOptions options, int maxCount = 10)
        {
            if (options == CollectionOptions.Null)
            {
                return(null);
            }

            IEnumerable <T> items;

            if (options.HasFlag(CollectionOptions.Empty))
            {
                items = Array.Empty <T>();
            }
            else
            {
                var type  = typeof(T);
                var range = Enumerable.Range(1, maxCount);
                if (type == typeof(int))
                {
                    items = range as IEnumerable <T>;
                }
                else if (type == typeof(int?))
                {
                    items = range.Select(i => i as int?) as IEnumerable <T>;
                }
                else if (type == typeof(char))
                {
                    items = range.Select(i => (char)i) as IEnumerable <T>;
                }
                else if (type == typeof(char?))
                {
                    items = range.Select(i => (char)i as char?) as IEnumerable <T>;
                }
                else if (type == typeof(string))
                {
                    items = range.Select(i => i.ToString()) as IEnumerable <T>;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            var list = items.ToList();

            if (options.HasFlag(CollectionOptions.HasNullElement))
            {
                list.RemoveAt(list.Count - 1);
                list.Insert(RandomUtils.Current.Next(list.Count), default);
            }

            var hasCount    = options.HasFlag(CollectionOptions.HasCount);
            var hasContains = options.HasFlag(CollectionOptions.HasContains);

            if (hasCount && hasContains)
            {
                return(new TestEnumerableWithCountAndContains <T>(list));
            }

            if (hasCount)
            {
                return(new TestEnumerableWithCount <T>(list));
            }

            if (hasContains)
            {
                return(new TestEnumerableWithContains <T>(list));
            }

            return(new TestEnumerable <T>(list));
        }