/// <summary> /// Case where B navigates to A, A is mandatory but A cant navigate to B is not handled. /// </summary> public void CleanOrphans(Type pocoType, IAgrideaService service, bool cleanNonDependentPocos = false) { var instances = service.All(pocoType).ToList(); var cleantCount = 0; foreach (var instance in instances) { var classHasRelationships = false; var instanceHasLinks = false; foreach (var referenceProperty in instance.GetType().GetReferenceProperties()) { classHasRelationships = true; var reference = referenceProperty.GetValue(instance) as IPocoBase; if (reference != null) { instanceHasLinks = true; break; } } if (instanceHasLinks) { continue; } foreach (var collectionProperty in instance.GetType().GetCollectionProperties()) { classHasRelationships = true; var inputCollection = collectionProperty.GetValue(instance) as IList; if (inputCollection.Count != 0) { instanceHasLinks = true; break; } } if (classHasRelationships && instanceHasLinks) { continue; } if (!classHasRelationships && !cleanNonDependentPocos) { continue; } service.RemoveItem(instance); cleantCount++; } service.Save().Reset(); if (cleantCount > 0) { Log.Info("CloningHelper.CleanOrphans ({0}, cleanNonDependentPocos:{1} Total:{2} / Cleant:{3})", pocoType.Name, cleanNonDependentPocos, instances.Count, cleantCount); } }
/// <summary> /// Removes a whole tree under input, this is the 'inverse' of Clone but this is too slow for a whole Canton... /// </summary> public void Remove(IPocoBase input, IAgrideaService service) { if (input == null) { return; } var pocoType = ObjectContext.GetObjectType(input.GetType()); var pocoTypeName = pocoType.Name; if (excludedPocos_.Contains(pocoType)) { return; } if (input.GetType().IsReferenceEntity()) { return; } if (deleted_.ContainsKey(input)) { return; } Log.Verbose("CloningHelper.Remove ({0})", input); deleted_.Add(input, true); foreach (var referenceProperty in input.GetType().GetReferenceProperties()) { var reference = referenceProperty.GetValue(input) as IPocoBase; if (reference == null) { continue; } Remove(reference, service); } foreach (var collectionProperty in input.GetType().GetCollectionProperties()) { var inputCollection = collectionProperty.GetValue(input) as IList; if (inputCollection.Count == 0) { continue; } foreach (IPocoBase inputItem in new ArrayList(inputCollection)) //to avoid collection change during iteration { Remove(inputItem, service); } } service.RemoveItem(input); //Must be at the end otherwise all relationships are cleared }