New Posts New Posts RSS Feed: Best way to implement a "IsDefault" field on an entity ?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Best way to implement a "IsDefault" field on an entity ?

 Post Reply Post Reply
Author
nazbrok View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Jan-2011
Location: Paris
Posts: 21
Post Options Post Options   Quote nazbrok Quote  Post ReplyReply Direct Link To This Post Topic: Best way to implement a "IsDefault" field on an entity ?
    Posted: 06-Mar-2011 at 12:51pm
Hi,


I have an entity which has a field named "IsDefault". I want to add a method to this entity named SetDefault to set this flag at true. I can only have one entity with this flag at true in my model.

What is the best way to implement such logic ? Should I have this logic in the entity or in my ViewModel ?


The way I did it I have a problem when I manipulate many entities in memory. 
Exemple : I create one entity and call my SetDefault method. Then create another one entity and also call SetDefault on it. At last I call SaveChanges.

I would expect to have only the last entity with the IsDefault to true but both are true because I used a query in my method to find the current entity which has IsDefault=true. Obviously the query doesn't know yet about the entity I created in memory.

In my solution I have a problem, if I create many RefNomenclature in the same time before to do a SaveChanges, the query I use to find the previous default entity is always empty. 

here is my code :

    public partial class RefNomenclature
    {
        public static RefNomenclature Create(MyEntitiesManager mgr)
        {
            var refNomenclature = mgr.CreateEntity<RefNomenclature>();
            refNomenclature.EntityAspect.AddToManager();
            refNomenclature.IsDefault = false;
            return refNomenclature;
        }

        public void SetParDefaut()
        {
            if (this.IsDefault) return;

            var oldDefault = (EntityAspect.EntityManager as MyEntitiesManager).RefNomenclatures.Where(d => d.IsDefault).FirstOrDefault();
            if (oldDefault != null)
                oldDefault.IsDefault = false;
            this.IsDefault = true;
        }
    }
}
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: 07-Mar-2011 at 5:15pm
Hi nazbrok;

Your implementation actually looks reasonable except for one thing.

var oldDefault = (EntityAspect.EntityManager as MyEntitiesManager).RefNomenclatures.Where(d => d.IsDefault).FirstOrDefault();

if (oldDefault != null)

should be changed to

var oldDefault = (EntityAspect.EntityManager as MyEntitiesManager).RefNomenclatures.Where(d => d.IsDefault).FirstOrNullEntity();

if (!oldDefault.EntityAspect.IsNullEntity)

In addition, if you only expect to work with "in-memory" entities or entities inside the cache, you can modify your query as follows so it's more optimized.

var oldDefault = (EntityAspect.EntityManager as MyEntitiesManager).RefNomenclatures.With(QueryStrategy.CacheOnly).Where(d => d.IsDefault).FirstOrNullEntity();

Hope this helps.


Edited by DenisK - 07-Mar-2011 at 5:21pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down