开发者

Entity Framework - Advice and best practice

I've recently started with Entity Framework on an MVC project I'm doing to get the grips with the technologies and I've got a few questions : ..

My first one is this, this is my Save code for an entity - Is this the best way.. it looks a little suspect to me..

public bool SavePrank(PrankDefinition prank)
    {

        if (prank == null)
            throw new ArgumentNullException("prank");

        if (prank.ID == 0)
        {
            DataBase.Pranks.Add(prank);
            DataBase.SaveChanges();
        }
        else
        {
            DataBase.Pranks.Attach(prank);
            DataBase.Entry(prank).State = EntityState.Modified;
            DataBase.SaveChanges();
        }

        return true;

    }

Ive also got this code for getting the latest version of the entitys..

public List<PrankDefinition> GetPranks()
    {
        List<PrankDefinition> pranks = DataBase.Pranks.Where(p => p != null).ToList();

        foreach (PrankDefinition prankDef in pranks)
        {
            DataBase.Entry(prankDef).Reload();
        }

        return pranks;
    }

开发者_如何学Cthe reason ive had to call .reload on the entity's is because when another client is using the project - there changes to entity's are not reflected instantly (which is critical). My question for this - is there a better way to do this? is there something I can attach to the Where method to get the latest versions?

my context - if it helps..

    public static DataContext DataBase
    {
        get
        {
            if (HttpContext.Current != null &&            HttpContext.Current.Session["DataBase"]== null)
            {
                HttpContext.Current.Session["DataBase"] = new DataContext();
            }

            return HttpContext.Current.Session["DataBase"] as DataContext;
        }
        set
        {
            if (HttpContext.Current != null)
                HttpContext.Current.Session["DataBase"] = value;
        }
    }

any help would be fantastic!

EDIT : UPDATE TO DATA CONTEXT

Would this be a better implementation of the DataContext?

public static DataContext DataBase
    {
        get { return new DataContext(); }
    }

cheers. ste.


Just a few remarks:

  • In my opinion SaveChanges finishes a unit of work and doesn't belong to single repository methods like SavePrank. I would probably prefer a pattern like this:

    InsertOrUpdatePrank(prank); // = SavePrank without SaveChanges
    ModifyPerhapsSomeOtherEntity(otherEntity);
    SetPerhapsRelationshipBetweenPrankAndOtherEntity(prank, otherEntity);
    // ... more ...
    DataBase.SaveChanges();
    

    This way all changes are written to the database in a single transaction.

  • Where do you dispose your context? If you instantiate and store it in a session but don't dispose the context in your code explicitely the context lives across multiple requests. This is a potential source of big trouble because your context might still contain entities from older requests when you process a new request. For example: If the prank with ID = 123 gets updated twice in two subsequent requests from the same user (in the same session) DataBase.Pranks.Attach(prank) would throw an exception because on old prank from a former requests with same ID is already attached to the context. In a web application a context should never live longer than a single request to avoid problems like this and many more.

  • If you dispose the context per request I don't see the need anymore to "reload" entities. You have to load the entities from the database anyway because each context is a new one and empty when you enter an action. So, there is nothing to reload. You'll get the latest version of your entites from the database anyway when you run your queries.

  • Where(p => p != null) makes no sense. To fetch all rows you can just write:

    List<PrankDefinition> pranks = DataBase.Pranks.ToList();
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜