background preloader

Entity Framework

Facebook Twitter

Testability and Entity Framework 4.0. Scott Allen Published: May 2010 Introduction This white paper describes and demonstrates how to write testable code with the ADO.NET Entity Framework 4.0 and Visual Studio 2010.

Testability and Entity Framework 4.0

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. What Is Testable Code? The ability to verify a piece of software using automated unit tests offers many desirable benefits. A good unit test suite allows a development team to save time and remain in control of the software they create. Unit testing comes with a price, however. Microsoft designed the ADO.NET Entity Framework 4.0 (EF4) with testability in mind. The Qualities of Testable Code Code that is easy to test will always exhibit at least two traits. Public int Add(int x, int y) { return x + y; } The Repository Pattern Figure 1. Agile EF4 Repository: Part 3 -Fine Tuning the Repository. Third post in my Agile EF4 Repository blog series: In the previous post I created an uber repository (someone called it a ‘god class’ :)) that was responsible for all of my object generation.

Agile EF4 Repository: Part 3 -Fine Tuning the Repository

You’ll see some great discussion in the comments with Bobby Johnson (who I met when speaking at the South Sound .NET group in Olympia), Mike Campbell (a friend and unbeknownst to him, sometimes mentor) and myself about the structure of this repository. They encouraged me to look deeper and create repositories that had singular responsibility for classes. So with a lot of cajoling and many emails back and forth where I tried to explain EF to them and they helped me with a better understanding of the repository pattern and of persistence ignorance, I have taken the repository to the next step and refactored my solution.

Faisal Mohamood’s (Entity Framework team) post on EF, Repository and Unit of Work helped as well. Okay back to code. Repository Interface for Entities Implementing IEntityRepository. Using Repository and Unit of Work patterns with Entity Framework 4.0 - ADO.NET Blog. The information in this post is out of date. Visit msdn.com/data/ef for the latest information on current and past releases of EF. If you have been watching this blog, you know that I have been discussing the various aspects of POCO capabilities we added to Entity Framework 4.0. POCO support makes it possible to do persistence ignorance with Entity Framework in a way that was never possible with Entity Framework 3.5. If you missed the series on POCO, I’ve listed them here for your convenience.

It might be a good idea to quickly check these out.

Code first

Database first. Flattening Objects (EntityDataSource) The topic you requested is included in another documentation set.

Flattening Objects (EntityDataSource)

For convenience, it's displayed below. Choose Switch to see the topic in its original location. The following code shows how to get an entity object from a wrapper object: protected void ProductsGridView_RowDataBound(object sender, GridViewRowEventArgs e) { var entity = dataItem as Product; if (entity == null) { var td = dataItem as ICustomTypeDescriptor; if (td ! = null) { entity = (Product)td.GetPropertyOwner(null); } } } Extending Entity Framework Generated Types.