protected virtual IOrganizationService GetIOrganizationService()
 {
     return(new OrganizationServiceBuilder(GetInternalOrganizationServiceProxy()).
            WithEntityNameDefaulted((e, i) => GetUnitTestName(i.MaximumLength)).
            AssertIdNonEmptyOnCreate().
            WithIdsDefaultedForCreate(NewEntityDefaultIds.ToArray()).
            WithDefaultParentBu().Build());
 }
Пример #2
0
        /// <summary>
        /// When an entity is attempted to be created without an id, and an Id was passed in for that particular entity type, the Guid of the Id will be used to populate the entity
        /// </summary>
        /// <param name="ids">The ids.</param>
        /// <returns></returns>
        public TDerived WithIdsDefaultedForCreate(IEnumerable <Id> ids)
        {
            foreach (var id in ids)
            {
                NewEntityDefaultIds.AddOrEnqueue(id);
            }

            return(This);
        }
        /// <summary>
        /// When an entity is attempted to be created without an id, and an Id was passed in for that particular entity type, the Guid of the Id will be used to populate the entity
        /// </summary>
        /// <param name="ids">The ids.</param>
        /// <param name="ignoreContextCreation">Since OrganizationServiceContext.SaveChanges will populate ids if they aren't already populated, controls whether an error should be thrown when this occurs.</param>
        /// <returns></returns>
        public TDerived WithIdsDefaultedForCreate(IEnumerable <Id> ids, bool ignoreContextCreation = false)
        {
            if (ignoreContextCreation)
            {
                NewEntityThrowErrorOnContextCreation = false;
            }
            foreach (var id in ids)
            {
                NewEntityDefaultIds.AddOrEnqueue(id);
            }

            return(This);
        }
Пример #4
0
        /// <summary>
        /// Defaults the id an any entity created using the Dictionary string, without an already defined Guid, to the default Id.  Doesn't actually create the Entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        private void DefaultIdForEntity(Entity entity)
        {
            Queue <Guid> ids;

            if (entity.Id != Guid.Empty || !NewEntityDefaultIds.TryGetValue(entity.LogicalName, out ids))
            {
                return;
            }
            if (ids.Count == 0)
            {
                throw new Exception(
                          $"An attempt was made to create an entity of type {entity.LogicalName}, but no id exists in the NewEntityDefaultIds Collection for it.{Environment.NewLine}" +
                          "Either the entity's Id was not populated as a part of initialization, or a call is needs to be added to to OrganizationServiceBuilder.WithIdsDefaultedForCreate(id)");
            }
            entity.Id = ids.Dequeue();
        }
        /// <summary>
        /// Defaults the id an any entity created using the Dictionary string, without an already defined Guid, to the default Id.  Doesn't actually create the Entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        private void DefaultIdForEntity(Entity entity)
        {
            // throw an error if from context, and NewEntityThrowErrorOnContextCreation is true, and not in the ids collection
            if (entity.Id != Guid.Empty)
            {
                if (entity.EntityState != EntityState.Created)
                {
                    return;
                }

                var hasIds = NewEntityDefaultIds.TryGetValue(entity.LogicalName, out Queue <Guid> ids);

                if (NewEntityThrowErrorOnContextCreation)
                {
                    if (!hasIds || !ContainsAndRemoved(ids, entity.Id))
                    {
                        throw new Exception(
                                  $"An attempt was made to create an entity of type {entity.LogicalName} with the EntityState set to created which normally means it comes from an OrganizationServiceContext.SaveChanges call.{Environment.NewLine}"
                                  +
                                  "Either set ignoreContextCreation to true on the WithIdsDefaultedForCreate call, or define the id before calling SaveChanges, and add the id with the WithIdsDefaultedForCreate method.");
                    }
                }
                else if (hasIds)
                {
                    ContainsAndRemoved(ids, entity.Id);
                }
            }
            else
            {
                if (!NewEntityDefaultIds.TryGetValue(entity.LogicalName, out Queue <Guid> ids))
                {
                    return;
                }
                if (ids.Count == 0)
                {
                    throw new Exception(
                              $"An attempt was made to create an entity of type {entity.LogicalName}, but no id exists in the NewEntityDefaultIds Collection for it.{Environment.NewLine}" +
                              "Either the entity's Id was not populated as a part of initialization, or a call is needs to be added to to OrganizationServiceBuilder.WithIdsDefaultedForCreate(id)");
                }
                entity.Id = ids.Dequeue();
            }
        }
        private void ApplyNewEntityDefaultIds()
        {
            if (!NewEntityDefaultIds.Any())
            {
                return;
            }

            CreateFuncs.Add((s, e) =>
            {
                DefaultIdForEntity(e);
                return(s.Create(e));
            });
            ExecuteFuncs.Add((s, r) =>
            {
                if (r is SendEmailFromTemplateRequest email)
                {
                    DefaultIdForEntity(email.Target);
                }
                return(s.Execute(r));
            });
        }