Print Page | Close Window

Newbie Qstn, Adding entities to the tree vs EntityManager.AddEntity

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=2420
Printed Date: 29-Jul-2026 at 12:18pm


Topic: Newbie Qstn, Adding entities to the tree vs EntityManager.AddEntity
Posted By: c63205
Subject: Newbie Qstn, Adding entities to the tree vs EntityManager.AddEntity
Date Posted: 05-Jan-2011 at 8:45am
I'm just starting with DevForce, I'm assuming that the functionality of Manager.AddEntity() will also be accomplished by creating a 'child' enitity to an existing entity that is already in the EnityManager.

so, this code should correctly create a new 'PersonalDemographic' entity to the EntityManager as the object "existingEntityInstance" has already been explicitly added to the EntityManager.  I'm testing this out now, but wanted to check to see if this was 'kosher' just in case.

   existingEnitityInstance.QuestionnaireInstanceMeta.PersonalDemographic = new PersonalDemographic();
   existingEnitityInstance.QuestionnaireInstanceMeta.PersonalDemographic.ID = Guid.NewGuid();

Thanks,



Replies:
Posted By: smi-mark
Date Posted: 05-Jan-2011 at 8:56am
I believe you will still have to attach it to the EntityManager.

PersonalDemographic.cs:

public PersonalDemographic Create(EntityManager manager)
{
   var pd = new PersonalDemographic() { ID = Guid.NewGuid() };
   manager.AddEntity(pd);
   return pd;
}


existingEnitityInstance.QuestionnaireInstanceMeta.PersonalDemographic = PersonalDemographic.Create(yourEntityManager);

alternatively you can create it using the entityManager of the existingEntityInstance by using the EntityManager property of the EntityAspect.


Posted By: kimj
Date Posted: 05-Jan-2011 at 9:55am
We haven't done a very good job of documenting how entities are added/attached to the EntityManager, but it does happen implicitly.  In this case, if existingEntityInstance is already in an EM, then the new PersonalDemographic will automatically be added to that EM as well, with the EntityState of Added.
 
In general, when a navigation property setter is called the entity will be added/attached to the EM.  The obverse is true too, if the source entity (say existingEntityInstance here) is not in the EM but the navigation  property entity is, then the source entity will be added/attached to that EM.  The "goal" is that both sides of the navigation end up in the same EM.
 
As smi-mark mentions, it may still be a good idea to rely on explicit add logic, especially so that any PK values can be set prior to the add.
 
We'll get some documentation and samples of this up on the DevForce Resource Center soon...


Posted By: c63205
Date Posted: 05-Jan-2011 at 10:59am
Great, thank you for the reply.  Additional documentation on building the object tree would certainly be useful.  Overall, I've been quite impressed with the style, tone, and content of the documentation.  I like how the technicalities are documented along with examples and the guidance-type dialog touching on when and why a feature would be employed.

So, would calling .EntityAspect.AddToManager() be a solid way to explicitly add the new entity to the manager? 


 existingEnitityInstance.QuestionnaireInstanceMeta.PersonalDemographic = new PersonalDemographic();
 existingEnitityInstance.QuestionnaireInstanceMeta.PersonalDemographic.ID = Guid.NewGuid();
 existingEnitityInstance.QuestionnaireInstanceMeta.PersonalDemographic.EntityAspect.AddToManager();


Also, I've been assuming that the EntityManager is a global , singleton object and I don't have to worry about managing instances of it.  Being unsure, I've been using it from my own singleton method.

Thank you


Posted By: c63205
Date Posted: 05-Jan-2011 at 11:03am
Thank you smi-mark,


Posted By: kimj
Date Posted: 05-Jan-2011 at 11:43am
As soon as you do:
  existingEnitityInstance.QuestionnaireInstanceMeta.PersonalDemographic = new PersonalDemographic();
 
you've added the new entity to an EM so there's no need to then call AddToManager.   To first create the entity (detached) and then implicitly add it you can do the following:
 
   var pd = new PersonalDemographic();
   pd.ID = Guid.NewGuid();
   existingEnitityInstance.QuestionnaireInstanceMeta.PersonalDemographic = pd;
 
AddToManager() is somewhat confusing - what manager?  If you construct an entity using CreateEntity<T> then you've tagged the entity to an EM, so the AddToManager() will add the detached entity to that EM.  If you construct an entity using a standard new constructor, the EM used will be the DefaultManager, which may not be what you want.
 
The method shown by smi-mark to create the entity and then call manager.AddEntity(entity) gives you better control over what's happening.
 
The EntityManager is not a global singleton.  The EM.DefaultManager is a global singleton but we caution against using it, and we'll likely deprecate it over time.  If you need a singleton it's best to implement your own.   See the Resource Center at http://drc.ideablade.com/xwiki/bin/view/Documentation/ - http://drc.ideablade.com/xwiki/bin/view/Documentation/  for some examples (such as BadGolf) which show more robust patterns.
 



Print Page | Close Window