At work we are going to do a big refactoring on a ASP.NET MVC project. I learned a lot of this project. One hard lesson was: the code base is as good is the guidance of the senior coders. If you know the power of the Entity Framework, then you also know the countless possibilities to write wicked code. But I’m present at the office only for days a week. That’s why I’m planning to write some articles in the next time.
We now decided to radically limit EF’s possibilities to a handsome DAL (data access layer), which is testable, stateless, easy to use and brutally simple. Please keep in mind that those limitations are not a real handicap. In fact they should allow newcomers to focus on their real target.
These are some details about the project:
- Usage of a Service- and Repository Layer
- Data Objects are all POCOs (Plain Old CLR Object)
- Entity Framework is the DAL underneath the repository (some people are arguing that EF is already fulfilling the Repository-Pattern)
- Dependency Injection (DI) is done with the help of Unity
- Unit Tests are running on the TFS (Team Foundation Server), therefore we mix MSTest and NUnit
At office and for my own projects I used the ADO.NET C# POCO Entity Generator for a while. Here is a great article describing this approach:
Walkthrough: Test-Driven Development with the Entity Framework 4.0
At this point I also recommend the book Programming Entity Framework, 2nd edition by Julia Lerman, which also covers Unit Testing in one chapter.
If you have a lot of small repositories, then you have lot of recurring stuff to do. This totally ignores the DRY-principle. (Don’t Repeat Yourself) For that reason Rab Hallett has extended the “ADO.NET C# POCO Entity Generator” with some more features. Please have a look at his articles:
This extension is a perfect starting point for some more handy code generation, called “ADO.NET Unit Testable Repository Generator”.
With this extension you will get a complete solution:
- all functionalities of the ADO.NET C# POCO Entity Generator, which includes:
- a compatible ObjectContext (replaces the “Entities-Class”)
- all entity types as POCOs (Plain Old CLR Objects)
- as well as:
- an Interface for the ObjectContext
- a mock of the ObjectContext (provides access to ObjectSets)
- a mock that implements IObjectSet (stores data)
- a partial Repository class
- an abstract Unit Test class
- optional: support for Microsoft Unity *
- optional: equality members for the POCOs (Equals and GetHashCode) **
The new Interface now hides all advanced functionalities of the EF.
This simplifies things dramatically. Welcome back to the good old Linq-To-SQL days!
Many thanks to the guys from Microsoft and to Rab Hallett who offered their work under the Microsoft Public License !
* Use the preprocessor definition “DO_NOT_USE_UNITY”, if you do not want to utilize the Unity-Framework.
** Some people might prefer the POCOs completely plain. For that case you should define the directive “DO_NOT_INCLUDE_ EQUALITY_MEMBERS”.
Please note: I renamed to extension from “ADO.NET Mocking Context Generator – Extended” to “ADO.NET Unit Testable Repository Generator”.