예제 #1
0
        /// <summary>
        /// By default attempts to load entities from internal type with static Id properties
        /// </summary>
        protected virtual void InitializeEntityIds()
        {
            // Find all nested Ids
            var nestedIds = new Dictionary <string, List <Id> >();

            foreach (var id in GetType().GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic).SelectMany(Extensions.GetIds))
            {
                nestedIds.AddOrAppend(id, id);
            }

            // Add the nested Ids' Logical Names to the Mapper
            foreach (var key in nestedIds.Keys)
            {
                EntityDependency.Mapper.Add(key);
            }

            // Add the nested Ids in the Deletion Order of the Mapper
            foreach (var entity in EntityDependency.Mapper.EntityDeletionOrder)
            {
                if (nestedIds.TryGetValue(entity, out List <Id> ids))
                {
                    EntityIdsByLogicalName.AddOrAppend(entity, ids.ToArray());
                }
            }
        }
        /// <summary>
        /// Data is assumed to be there, so use TryDelete
        /// </summary>
        /// <param name="service"></param>
        protected virtual void CleanupDataPostInitialization(IOrganizationService service)
        {
            var totalWatch = new Stopwatch();

            totalWatch.Start();

            var requests = new OrganizationRequestCollection();

            foreach (var id in EntityIdsByLogicalName.Where(e => e.Key != "businessunit").SelectMany(entityType => entityType.Value))
            {
                requests.Add(new DeleteRequest {
                    Target = EntityIds[id]
                });
            }

            if (requests.Any())
            {
                var response = (ExecuteMultipleResponse)service.Execute(
                    new ExecuteMultipleRequest
                {
                    Settings = new ExecuteMultipleSettings
                    {
                        ContinueOnError = true,
                        ReturnResponses = false
                    },
                    Requests = requests,
                });

                ThrowExceptionForFaults(response, requests);

                totalWatch.Stop();
                Logger.WriteLine("Total Time to delete {0} entities of types {1} (ms): {2}",
                                 requests.Count,
                                 requests.Select(s => ((DeleteRequest)s).Target.LogicalName).Distinct().ToCsv(),
                                 totalWatch.ElapsedMilliseconds);
            }

            List <Id> businessIds;

            if (!EntityIdsByLogicalName.TryGetValue("businessunit", out businessIds))
            {
                return;
            }

            foreach (var id in businessIds)
            {
                service.DeleteBusinessUnit(id);
            }
        }