Geeks With Blogs
Jamie Kurtz Promoting architectural simplicty

I've been looking quite a bit at the new ADO.NET Entity Framework. I'm really hoping to be able to implement the technology to replace our data access layer with all of its generated database and .NET code.

One of the first things I found myself trying to figure out was updating a DateLastMaint field whenever a change is persisted by the ObjectContext. Our applications use this field religiously, and its update needs to be baked down into the architecture so the application developers don't need to mess with it.

In addition to updating fields on entities, we also need to be support auditing. I'm not sure if this little trick would solve that problem, but it's a start. So here's how I did...

I created a ModelFactory class that serves up EDM models that are pre-hooked to update this DateLastMaint field upon saving. Here's the code for the class:

public static class ModelFactory
{
    public static T CreateModel<T>() where T: ObjectContext
    {
        T model = Activator.CreateInstance<T>();
        model.SavingChanges += new EventHandler(model_SavingChanges);
        return model;
    }

    static void model_SavingChanges(object sender, EventArgs e)
    {
        ObjectContext context = (ObjectContext)sender;

        IEnumerable<ObjectStateEntry> changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);

        foreach (ObjectStateEntry entry in changes)
        {
            DateTime dateLastMaint = DateTime.Now;
            int ordinal = entry.CurrentValues.GetOrdinal("DateLastMaint"); 
            entry.CurrentValues.SetDateTime(ordinal, dateLastMaint);
        }
    }
}

Then to use the ModelFactory class, just do the following:

MyEntities context = ModelFactory.CreateModel<MyEntities>()

One thing to work out yet: The ObjectContext / model should be instantiated within a using block. But doing so caused the underlying connection to get disposed, so my little test app couldn't persist the changes back to the database. Not sure yet how to recreate an instance of the model and have existing changes merged with it???

Posted on Monday, February 25, 2008 9:37 PM | Back to top


Comments on this post: ADO.NET Entity Framework (BETA 3) - Hooking SaveChanges()

# re: ADO.NET Entity Framework (BETA 3) - Hooking SaveChanges()
Requesting Gravatar...
Hey Jamie
Is this so that you don't have to add this same code into the ObjectContext.SavingChanges event for each or your models?
Left by Julie Lerman on Jul 17, 2008 9:06 PM

# re: ADO.NET Entity Framework (BETA 3) - Hooking SaveChanges()
Requesting Gravatar...
Correct. Using the approach described above we can pack that event hook full of various business rules and validation, and it is all transparent to our developers . Further, if we need to alter any of these rules we only need to alter them in one place.

I can see scenarios where you'd want both: the business global rules but also rules that are specific to a certain model. In this case you can simply hook a 2nd event to SavingChanges, and both would be fired.
Left by Jamie on Jul 18, 2008 7:45 AM

# re: ADO.NET Entity Framework (BETA 3) - Hooking SaveChanges()
Requesting Gravatar...
Very Cool.

So here's an idea for a next step...

http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3635154&SiteID=1&mode=1

Left by Julie on Jul 18, 2008 8:56 AM

# re: ADO.NET Entity Framework (BETA 3) - Hooking SaveChanges()
Requesting Gravatar...
I have been attempting something similar for auditing purposes, but I have not wired into the SavingChanges event because I am concerned about the possibility that the commit could fail and wreck my audit trail. I'm looking for something like an ObjectContext.SavedChanges event or some other way to audit a change after it has been successful.

I even tried overriding SaveChanges, but it is not overridable.

Have you ever run into (and solved) this problem?
Left by Rob on Jul 28, 2008 4:56 PM

# re: ADO.NET Entity Framework (BETA 3) - Hooking SaveChanges()
Requesting Gravatar...
where can i download ADO.NET Entity Framework (BETA 3) from? any links?
Left by omar on Nov 16, 2009 4:43 AM

Your comment:
 (will show your gravatar)


Copyright © Jamie Kurtz | Powered by: GeeksWithBlogs.net