New Posts New Posts RSS Feed: Deleting child entities from generic entity. How?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Deleting child entities from generic entity. How?

 Post Reply Post Reply
Author
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Topic: Deleting child entities from generic entity. How?
    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".


Edited by katit - 11-Apr-2012 at 12:00pm
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 146
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Posted: 12-Apr-2012 at 6:23am
That works. Thank you!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down