Overloading Entity Framework Methods: More GetObjectStateEntries

I have been writing gobs of Extension methods for entity Framework through the course of writing my book.

I was inspired by this forum thread to create extension methods to overload existing methods in the APIs.

One pattern that was annoying me was that frequently when I'm using the GetObjectStateEntries method of the ObjectStateManager, I want to filter on a type. It only meant building a linq expression with the method in it

Dim CustomerEntries= From entry _
                                    In objStateMgr.GetObjectStateEntries(EntityState.Added Or EntityState.Deleted _
                                          Or EntityState.Modified Or EntityState.Unchanged) _
                                    Where entry.Entity.GetType Is Customer

So two things about this were bugging me.

The first is I always have to list out all of those entries. Sometimes I only want one or two, so it's good to build the list.

The second is that I always have to write a query.

So, I created a few overloads to the method.

The first takes no parameters, and returns all of the entries, regardless of EntityState

<Extension()> _
Public Function GetObjectStateEntries(ByVal osm As Objects.ObjectStateManager) _
   
As IEnumerable(Of Objects.ObjectStateEntry)
  Dim typeEntries = From entry As Objects.ObjectStateEntry _
                  
In osm.GetObjectStateEntries(EntityState.Added Or EntityState.Deleted _
                      Or EntityState.Modified Or EntityState.Unchanged)
  Return typeEntries
End Function

Now I can just call GetObjectStateEntries and get all of them without having to specify the four entity state enums.

The second returns all objects of a particular type. Having the overload for the generic type is more discoverable for people like me.

<Extension()> _
Public Function GetObjectStateEntries(Of TEntity)(ByVal osm As Objects.ObjectStateManager) _
    
As IEnumerable(Of Objects.ObjectStateEntry)  
  Dim typeEntries = From entry As Objects.ObjectStateEntry _
                    In osm.GetObjectStateEntries(EntityState.Added Or EntityState.Deleted _
                      Or EntityState.Modified Or EntityState.Unchanged) _
                    Where entry.Entity Is TEntity

  Return typeEntries 
End Function

Now I can get all entities of a particular type without having to build a query.

GetObjectStateEntries(Of Customer)

The last takes the EntityState parameters and filters on a particular type.

<Extension()> _
Public Function GetObjectStateEntries(Of TEntity)(ByVal osm As Objects.ObjectStateManager, _
         ByVal state As EntityState) As IEnumerable(Of Objects.ObjectStateEntry) 
  Dim typeEntries = From entry As Objects.ObjectStateEntry _
                    In osm.GetObjectStateEntries(state) _
                    Where entry.Entity Is TEntity
  Return typeEntries
End Function

So I can use GetObjectStateEntries it's "normal" way, except I can include the type as well as filtering on state.

GetObjectStateEntries(Of Customer)(EntityState.Unchanged)

(These are in Chapter 14 along with their C# versions. :-))

#1 Abhijeet P on 2.27.2009 at 8:37 PM

I believe EntityState uses a [Flags] enumeration, so you can use bitwise logic to identify the state perhaps?

#2 Julia Lerman on 2.27.2009 at 10:16 PM

I'm pretty sure I tried that quite a while ago and it doesn't work. But I'd have to go dig through the forums or my email to find the thread. Feel free to try it out yourself and report back!