Print Page | Close Window

Deleting child entities from generic entity. How?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3388
Printed Date: 13-May-2026 at 6:26am


Topic: Deleting child entities from generic entity. How?
Posted By: katit
Subject: Deleting child entities from generic entity. How?
Date Posted: 11-Apr-2012 at 11:59am
I have base repository that does most entity work on Silverlight client.
 
Consider this:
 
public void DeleteEntityFromManager(T entity)
        {
            entity.EntityAspect.Delete();
            if (this.EntitiesToDeleteWhenDeleting != null && this.EntitiesToDeleteWhenDeleting.Any())
            {
               
                foreach (var entitiesToEnclude in this.EntitiesToIncludeWhenLoading)
                    query = query.Include(entitiesToEnclude);
            }
        }
 
"If" statement inside function doesn't work right now, I just copied it from my generic "Query". When I do generic query - I append includes like this:
 
var query = EntityQuery.Create(typeof(T), this.EntityManager).OrderBySelector(sortSelector);
            if (this.EntitiesToIncludeWhenLoading != null && this.EntitiesToIncludeWhenLoading.Any())
            {
                foreach (var entitiesToEnclude in this.EntitiesToIncludeWhenLoading) query = query.Include(entitiesToEnclude);
            }
 
So, going back to my example on top - I'd like to somehow delete child entities according to EntitiesToDeleteWhenDeleting content which is just IEnumerable<string> and it can be something like "Users".



Replies:
Posted By: katit
Date Posted: 11-Apr-2012 at 1:19pm
Actually, I made it work with reflection like this:
 
public void DeleteEntityFromManager(T entity)
        {
            entity.EntityAspect.Delete();
            //
            if (this.EntitiesToDeleteWhenDeleting == null || !this.EntitiesToDeleteWhenDeleting.Any()) return;
            foreach (var entitiesToDelete in this.EntitiesToDeleteWhenDeleting)
            {
                var property = entity.GetType().GetProperty(entitiesToDelete);
                var childEntities = property.GetValue(entity, null) as IEnumerable<Entity>;
                if (childEntities != null)
                {
                    foreach (var childEntity in childEntities)
                    {
                        childEntity.EntityAspect.Delete();
                    }
                }
            }
        }
 
Does it look like a good solution or it can be done better?


Posted By: DenisK
Date Posted: 12-Apr-2012 at 1:11am
Hi katit,

Instead of using reflection, consider using DevForce entity metadata. For example,

    private void DeleteEntityFromEntityManager(Entity entity) {
      foreach (var listNavigationProperty in entity.EntityAspect.EntityMetadata.ListNavigationProperties) {
        var childrens = listNavigationProperty.GetValue(entity) as IEnumerable<Entity>;
        childrens.ToList().ForEach(c => c.EntityAspect.Delete());
      }
    }

Hope this helps.


Posted By: katit
Date Posted: 12-Apr-2012 at 6:23am
That works. Thank you!



Print Page | Close Window