New Posts New Posts RSS Feed: Any impact of instantiating new EntityManager multiple times
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Any impact of instantiating new EntityManager multiple times

 Post Reply Post Reply
Author
sebma View Drop Down
Groupie
Groupie
Avatar

Joined: 19-Aug-2008
Location: Singapore
Posts: 66
Post Options Post Options   Quote sebma Quote  Post ReplyReply Direct Link To This Post Topic: Any impact of instantiating new EntityManager multiple times
    Posted: 28-Aug-2009 at 1:41am
Hi all,
 
Audit log tables may be in another database altogether, and in another EDMX. I am also thinking that this EDMX is only referenced in another DomainModelAuditTrail project, away from the main DomainModel.
 
I am creating a backend audit trail via IEntityServerSaving. Though I can get the entities' EntityManager via EventArgs.GetEntityManager(), I am thinking of instantiating new EntityManager to create audit log entity.
This implies that each client transaction will result in new "DomainModelAuditTrailEntityManager" create just to create audit entries.
 
Of course, there must be try...catch in this new EntityManager's SaveChanges, any exceptions caught must be re-thrown to client to ensure that the transaction remains atomic.
 
There is no Dispose on entity manager to be called, and I am wondering if there is any impact on resource usage (besides clearing its cache).
 
Any advice will be great, thanks!
Sebastian
 
 
 
 
 
Back to Top
sebma View Drop Down
Groupie
Groupie
Avatar

Joined: 19-Aug-2008
Location: Singapore
Posts: 66
Post Options Post Options   Quote sebma Quote  Post ReplyReply Direct Link To This Post Posted: 29-Aug-2009 at 3:43am

Again, a picture paints a thousand words, perhaps pseudocode below can help explain:

void IEntityServerSaving.OnSaving(EntityServerSavingEventArgs args)
{
   EntityManager entEm = args.GetEntityManager();
   IEnumerable<Entity> ents = entEm.FindEntities(EntityState.Added | EntityState.Deleted | EntityState.Modified);
   if (ents.FirstOrDefault() == null)
      return;
   List<Entity> entList = ents.ToList();
  
   // now do own audit stuffs with own new instance of EntityManager
   DomainModelAuditTrailEntityManager audEm = new DomainModelAuditTrailEntityManager(entEm); // always new instance, any impact?
   ...
}
 
 
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: 29-Aug-2009 at 7:45am
What you're doing should be fine.  On the server, DevForce actually instantiates new EntityManagers too whenever needed - the one you retrieve via args.GetEntityManager() is new (note it's not the EM you called SaveChanges on).  Using the EM constructor which accepts another EM, as you're doing here, is a really good idea - that way your new EM is identified as a "server side" EM and DevForce won't try to log it in or connect it to the server, since it's both already connected and logged in. 
Back to Top
sebma View Drop Down
Groupie
Groupie
Avatar

Joined: 19-Aug-2008
Location: Singapore
Posts: 66
Post Options Post Options   Quote sebma Quote  Post ReplyReply Direct Link To This Post Posted: 30-Aug-2009 at 2:33am
Great, thanks!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down