BT

Abstract Interfaces vs Abstract Base Classes in Entity Framework 6

by Jonathan Allen on Jun 18, 2013 |

As part of the Beta 1 release, Entity Framework 6 has added support for mass inserts and deletes using the new DbSet AddRange and RemoveRange methods. These methods accept an IEnumerable and are processed when SaveChanges is called. Another DbSet methods, FindAsync was also added.

Adding these methods caused a design problem for the Entity Framework team. In order to make DbSet testable they had included a matching interface called IDbSet. In theory any mocks would implement this interface and all would be well. Unfortunately adding new methods to abstract interfaces is a breaking change, so what do you do?

Some of the ideas considered by the Entity Framework team included:

  • Make the breaking change. This is the route that Sun choose when it added new method to the JDBC interfaces in Java 7.
  • Make DbSet “more mockable”. In this context that means giving it a protected constructor and marking its methods as virtual. If the protected constructor is used, all the methods are treated as no-ops unless overridden. IDbSet could be marked as obsolete, as it would nothing over the real DbSet.
  • Use extension methods with delegation. If a real DbSet is available the extension method will forward the call to it. If only an IDbSet is available, reflection will be used to locate a matching method to invoke. Needless to say, this is going to make tests slower and static type safety will be lost.
  • Create a new interface called IDbSet2. (And in the future IDbSet3, IDbSet4, etc.)

The decision was recorded in the May 16 design meeting notes,

The decision was to make DbSet more mockable. However, we will not obsolete IDbSet because this would create work for those currently using IDbSet who don’t need to use the new members. We will add guidance to IDbSet indicating that using DbSet is the way to go for new code and, depending on feedback, we may choose to obsolete IDbSet in a future release.

In you are interested in the history of other design decisions made in Entity Framework, the design meeting notes are available as far back as May 31, 2012.

Hello stranger!

You need to Register an InfoQ account or to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Abstract Interfaces?! by Paulo Pinto

Since when does .NET offer such a concept?

Re: Abstract Interfaces?! by Jonathan Allen

You are defining an abstract interface whenever you use the 'interface' keyword.

Over the years that keyword has led to a lot of confusion amongst C# and Java developers. They often forget that objects tend to have many interfaces. You've got the "public interface", which consists of all of the public members. Add to that the public interface of each base class working your way back to object. This can be a really important distinction when dealing with shadowed members.

There may also be a "protected interface", which is the public interface plus the members only available to subclasses. And of course the "internal interface" for .NET or the "package private interface" for Java.

When you hear expressions like "Program to the interface, not the implementation" they weren't talking about the interface keyword. What they were saying was that you should create functions to manipulate the data structure instead of directly mucking with internal memory using pointer offsets.

Re: Abstract Interfaces?! by Paulo Pinto

I have been using OO languages since early 90's and that is the first time I ever heard such term, hence my question.

Re: Abstract Interfaces?! by Jonathan Allen

It doesn't come up very often because usually the context of the conversation is enough to tell what is intended. I rarely use the term "abstract interface" except in formal writing.

That may change now that we are seeing testing frameworks that can mock public interfaces such as Microsoft Fakes.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

4 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2013 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT