New Posts New Posts RSS Feed: Newbie Qstn, Adding entities to the tree vs EntityManager.AddEntity
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

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

 Post Reply Post Reply
Author
c63205 View Drop Down
Newbie
Newbie


Joined: 28-Dec-2010
Location: Houston, TX
Posts: 10
Post Options Post Options   Quote c63205 Quote  Post ReplyReply Direct Link To This Post Topic: Newbie Qstn, Adding entities to the tree vs EntityManager.AddEntity
    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,
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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...
Back to Top
c63205 View Drop Down
Newbie
Newbie


Joined: 28-Dec-2010
Location: Houston, TX
Posts: 10
Post Options Post Options   Quote c63205 Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
c63205 View Drop Down
Newbie
Newbie


Joined: 28-Dec-2010
Location: Houston, TX
Posts: 10
Post Options Post Options   Quote c63205 Quote  Post ReplyReply Direct Link To This Post Posted: 05-Jan-2011 at 11:03am
Thank you smi-mark,
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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/ for some examples (such as BadGolf) which show more robust patterns.
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down