Exemplo n.º 1
0
        /// <summary>
        /// Sets all updateable values for the update set.
        /// </summary>
        /// <param name="entity">
        /// Get
        /// The entity containing the target values.
        /// </param>
        /// <param name="definitionProvider">
        /// The definition provider for the target repository.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="entity"/> or <paramref name="definitionProvider"/> is null.
        /// </exception>
        /// <returns>
        /// The current <see cref="UpdateSet{T}"/> with all updateable attribute values set.
        /// </returns>
        public UpdateSet <T> Set([NotNull] T entity, [NotNull] IEntityDefinitionProvider definitionProvider)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            if (definitionProvider == null)
            {
                throw new ArgumentNullException(nameof(definitionProvider));
            }

            this.attributesToSet.Clear();
            var entityDefinition = definitionProvider.Resolve <T>();

            foreach (var attributeDefinition in entityDefinition.UpdateableAttributes)
            {
                this.attributesToSet.Add(
                    new ValueState(
                        new AttributeLocation(attributeDefinition.PropertyInfo, EntityReference),
                        attributeDefinition.GetValueDelegate.Method.Invoke(entity, null)));
            }

            return(this);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the default relations for the entity set using the specified <paramref name="definitionProvider"/>.
        /// </summary>
        /// <param name="definitionProvider">
        /// The definition provider to get the default relations from.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="definitionProvider"/> is null.
        /// </exception>
        public void SetDefaultRelations([NotNull] IEntityDefinitionProvider definitionProvider)
        {
            if (definitionProvider == null)
            {
                throw new ArgumentNullException(nameof(definitionProvider));
            }

            this.relations.Clear();
            this.relations.AddRange(definitionProvider.Resolve <T>().DefaultRelations);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataTableLoader{T}"/> class.
        /// </summary>
        /// <param name="definitionProvider">
        /// The definition provider.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="definitionProvider"/> is null.
        /// </exception>
        public DataTableLoader([NotNull] IEntityDefinitionProvider definitionProvider)
        {
            if (definitionProvider == null)
            {
                throw new ArgumentNullException(nameof(definitionProvider));
            }

            this.propertyDictionary = new Lazy <Dictionary <string, PropertyInfo> >(
                () => definitionProvider.Resolve <T>()
                .AllAttributes.OrderBy(definition => definition.Ordinal)
                .ToDictionary(definition => definition.PropertyInfo.Name, definition => definition.PropertyInfo));
        }
Exemplo n.º 4
0
        /// <summary>
        /// The mock command provider.
        /// </summary>
        /// <param name="items">
        /// The merge items.
        /// </param>
        /// <param name="definitionProvider">
        /// The definition provider.
        /// </param>
        /// <param name="nameQualifier">
        /// The name qualifier.
        /// </param>
        /// <typeparam name="T">
        /// The type of item in the list to create an <see cref="IDbTableCommandFactory"/> mock for.
        /// </typeparam>
        /// <returns>
        /// The <see cref="Mock"/>.
        /// </returns>
        public static Mock <IDbTableCommandFactory> MockCommandFactory <T>(
            [NotNull] this IReadOnlyCollection <T> items,
            [NotNull] IEntityDefinitionProvider definitionProvider,
            [NotNull] INameQualifier nameQualifier)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (definitionProvider == null)
            {
                throw new ArgumentNullException(nameof(definitionProvider));
            }

            if (nameQualifier == null)
            {
                throw new ArgumentNullException(nameof(nameQualifier));
            }

            var structureDefinition = definitionProvider.Resolve <T>();
            var orderedAttributes   = structureDefinition.ReturnableAttributes.OrderBy(definition => definition.Ordinal).ToList();

            var reader = items.MockDataReaderForList(orderedAttributes);

            var command = new Mock <IDbCommand>();

            command.Setup(dbCommand => dbCommand.ExecuteReader()).Returns(reader.Object);

            var commandProvider   = new Mock <IDbTableCommandFactory>();
            var databaseContext   = new Mock <IDatabaseContext>();
            var repositoryAdapter = new Mock <IRepositoryAdapter>();

            repositoryAdapter.Setup(adapter => adapter.NameQualifier).Returns(nameQualifier);
            repositoryAdapter.Setup(adapter => adapter.DefinitionProvider).Returns(definitionProvider);
            databaseContext.Setup(context => context.RepositoryAdapter).Returns(repositoryAdapter.Object);
            ////commandProvider.Setup(provider => provider.DatabaseContext).Returns(databaseContext.Object);
            ////commandProvider
            ////    .Setup(provider => provider.Create(It.IsAny<ITableCommand>(), It.IsAny<DataTable>(), It.IsAny<IDbTransaction>()))
            ////    .Returns(command.Object);

            commandProvider
            .Setup(provider => provider.Create(It.IsAny <IDatabaseContext>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <T> >()))
            .Returns(command.Object);

            return(commandProvider);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Matches the primary key of the specified <paramref name="entity"/>.
        /// </summary>
        /// <param name="entity">
        /// The entity to match the key for.
        /// </param>
        /// <param name="definitionProvider">
        /// The definition provider for the entity.
        /// </param>
        /// <param name="explicitKeyAttributes">
        /// The explicit key attributes for this match.
        /// </param>
        /// <returns>
        /// The current <see cref="ValueFilterSet{T}"/>.
        /// </returns>
        /// <remarks>
        /// This operation will clear any existing filters.
        /// </remarks>
        public ValueFilterSet <T> MatchKey(
            [NotNull] T entity,
            [NotNull] IEntityDefinitionProvider definitionProvider,
            [NotNull] params Expression <Func <T, object> >[] explicitKeyAttributes)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            if (definitionProvider == null)
            {
                throw new ArgumentNullException(nameof(definitionProvider));
            }

            if (explicitKeyAttributes == null)
            {
                throw new ArgumentNullException(nameof(explicitKeyAttributes));
            }

            this.valueFilters.Clear();
            var entityDefinition = definitionProvider.Resolve <T>();
            var keyAttributes    = explicitKeyAttributes.Any()
                                    ? explicitKeyAttributes.Select(entityDefinition.Find)
                                    : entityDefinition.PrimaryKeyAttributes;

            foreach (var keyAttribute in keyAttributes)
            {
                var entityReference = new EntityReference
                {
                    EntityAlias = keyAttribute.Entity.Alias,
                    EntityType  = keyAttribute.Entity.EntityType
                };
                var attributeLocation = new AttributeLocation(keyAttribute.PropertyInfo, entityReference);
                var valueFilter       = new ValueFilter(attributeLocation, FilterType.Equality, keyAttribute.GetValueDelegate.DynamicInvoke(entity));

                this.valueFilters.Add(valueFilter);
            }

            // Use all available values if no keys are defined.
            if (this.valueFilters.Any())
            {
                return(this);
            }

            Trace.TraceWarning($"{typeof(T).FullName} does not have any key attributes defined.");

            foreach (var attribute in entityDefinition.DirectAttributes)
            {
                var entityReference = new EntityReference
                {
                    EntityAlias = attribute.Entity.Alias,
                    EntityType  = attribute.Entity.EntityType
                };
                var attributeLocation = new AttributeLocation(attribute.PropertyInfo, entityReference);
                var valueFilter       = new ValueFilter(attributeLocation, FilterType.Equality, attribute.GetValueDelegate.DynamicInvoke(entity));
                this.valueFilters.Add(valueFilter);
            }

            return(this);
        }