none
Event Handlers and Delegates in Entity Framework

    Question

  • We are currently using a custom Object Relational Mapping mechanism that we wrote for our data persistence layer.  One of the things that we wrote as part of our ORM persistence layer was the use of event handlers and delegates to allow overriding functionality such as Saving, Updating, Validating etc. on each of our ORM classes.  However, we would now like to migrate to Entity Framework v. 2.0 as our ORM solution, but we are unclear on how to manage and handle a persistence layer with event handler/delegates for Entities in Entity Framework.

    Is there a mechanism on how to implement this functionality on Entity Classes using Entity Framework v. 2.0 or do we need to explore other ORM solutions instead?

    Thanks.

    Friday, April 16, 2010 5:15 PM

Answers

All replies

  • EF4 does not have direct delegates / event handlers for this, but it does have a SavingChanges event, and the SaveChanges method is virtual so that you can override it.

    With this you can look in the ObjectStateManager for entiteis which are added, modified, deleted, etc.  Then you can run some business logic and then let the EF proceed to save changes, or if you completely handle the operations yourself, then you could call AcceptChanges on the ObjectStateEntries so the EF will know not to push changes to the database for them.

    I don't have a published sample on the web right now, but internally I've modified T4 templates to generate partial methods for each entity type and a SaveChanges override which queries the state manager and calls the partial methods as appropriate.  Then it's easy to implement the methods in partial classes and get behavior something like what you describe.

    - Danny


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, April 21, 2010 5:39 AM
  • If you could provide me with a code sample, that would be extremely helpful.

    Thanks.

    Wednesday, April 21, 2010 4:50 PM
  • The first example in the following topic shows how to override the SaveChanges method

    http://msdn.microsoft.com/en-us/library/cc716714.aspx

    public override int SaveChanges(SaveOptions options)
    {

        foreach (ObjectStateEntry entry in
            ObjectStateManager.GetObjectStateEntries(
            EntityState.Added | EntityState.Modified))
        {

             ...
         }
        return base.SaveChanges(options);
    }

    thanks,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, April 21, 2010 6:38 PM
  • I just put up a blog post with a simple VS extension that will make this process a lot easier.  Hopefully that helps.  Let me know if you have follow-up questions.

    http://blogs.msdn.com/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx

    - Danny


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, April 22, 2010 10:30 PM