OdeToCode IC Logo

Testability & Entity Framework 4.0

Tuesday, June 15, 2010

This white paper describes and demonstrates how to write testable code with the ADO.NET Entity Framework 4.0 and Visual Studio 2010. This paper does not try to focus on a specific testing methodology, like test-driven design (TDD) or behavior-driven design (BDD). Instead this paper will focus on how to write code that uses the ADO.NET Entity Framework yet remains easy to isolate and test in an automated fashion. We’ll look at common design patterns that facilitate testing in data access scenarios and see how to apply those patterns when using the framework. We’ll also look at specific features of the framework to see how those features can work in testable code.

Read the entire paper on MSDN.

Gravatar Brad Mead Thursday, June 17, 2010
Nice! I've been looking for something like this as I am now in an MS-centric shop where, if I can convince/teach them to invert-inject-test, will be required to perform these functions via EF and MSTest.

BTW - Missed you at this year's Alt.Net Seattle - at least I didn't spot you.
Gravatar scott Friday, June 18, 2010
@Brad - thanks! Yeah - I had to miss the Seattle event this year.
Gravatar Rick Weyrauch Tuesday, August 24, 2010
I loved your suggestions and they got me most of the way to where I wanted to be but...

I am having a problem with the "auto-inclusion" feature of EF4. That is, EF4 will detect that you have associated a new object via an existing objects properties: oldObject.Foo = new Foo() - this will get the new Foo to be added to the context.Foos during the SaveChanges. That makes my app run just fine but my tests do not see the new Foo when I try to confirm it was added (via the context.Foos collection). I have to also do a context.Foos.Add(theNewFoo) in my code.

Does that make any sense and what, if anything, should I be doing about it?
Gravatar stefano Friday, October 29, 2010
Realy interesting article.
what did you can suggest in a modularity scenario about repository and UoW (dbContext) ? There could be some problem with entity separated in different modules.
Gravatar Alan Rutter Monday, November 8, 2010
Great article but I am confused about the POCO and EDM. Maybe I missed something but you say that with the POCOs in place, we can create the EDM - but your article does not say how? I also needed to add IEntity to my POCOs to get teh custom repository to compile.
Gravatar scott Tuesday, November 9, 2010
Right - the article didn't want to focus on how to create the EDM - jsut the testing aspect. You do have to add the interface by hand (in fact, for the article I created all the entities by hand). When I create the EDM I turn off code generation (in the properties window), and EF is smart enough to use it as a mapping to my POCOs.
Comments are closed.