What Design Patterns are present in the Entity Framework?

Looking at http://en.wikipedia.org/wiki/Design_Patterns_(book) and the GoF-patterns listed there, I came up with this list:

  • Factory Method, an Entity Framework ObjectContext provides a CreateObjectSet<T> method, which creates an ObjectSet<T> for the given type. Since those object-sets are the main approach to access the entities within a context, I would say it is also a very important pattern used in EF.
  • Proxy, obviously EF dynamically creates proxy-types for your entities (unless you tell it not to, or it can’t create a proxy because a class is sealed or does not qualify as a “proxyable” type). https://www.tutorialspoint.com/design_pattern/proxy_pattern.htm
  • Decorator, this might be a bit far fetched, but to implement lazy-loading in proxy-types, the property-getters of navigation-properties are overloaded, to perform the lazy-loading until requested. This is handled in the proxy type, but also depends on whether lazy-loading is enabled and the navigation-property itself.
  • Interpreter, EF introduced Entity-SQL which is a somewhat generalized form of SQL that also has knowledge of the object-orientied entities. You can define a those queries as strings which are then interpreted by EF and turned into provider-dependend SQL.
  • Strategy, looking at the various overloads of the ObjectContext.SaveChanges method, you can select from different strategies that are used to determine changes made on known entities.
  • Memento, the ObjectStateManager keeps track of changes made to the entities and can be used to access those information including the original-values.
  • Facade, exposing entity-sets as IQueryable should qualify as “a simplified interface to a large body of code”, since turning such expressions into Entity SQL and than provider-specific SQL is a big deal.
  • Observer, ObjectContext provides two eventy ObjectMaterialized and SavingChanges. Since .NET events are an implementation of the observer-pattern, EF also qualifies for this one.

I may have been very generous with naming patterns here, but with some digging you can find a lot of places where EF actually implements those patterns. But since the book is out for nearly 20 years, you can expect that some of these patterns are considered a standard, which isn’t worth mentioning. For example the Observer pattern or the Iterator pattern (which I haven’t mentioned, just because IQueryable implements IEnumerable).

 

http://stackoverflow.com/questions/8132133/what-design-patterns-are-present-in-the-net-entity-framework

Advertisements