Created by Ministry
- .net Framework (various) / C#
This project provides T4 Templates for generating Repository layer code on top of Entity Framework database first. There are NuGet packages targeting each major version of Entity Framework. ObjectContext based EF is supported from 4.3.1 onwards and DbContext from 5.
We've been working with T4 templates for use with Entity Framework for a couple of years now. T4 templates are great because they actually let you write code to generate code so you can say goodbye to a load of unnecessary boilerplate.
We really like Entity Framework as an ORM tool, but where it's always fallen down has been in an ability to easilly unit test the applications that use it (particularly back when we started, before the common usage of POCOs and DbContext). In order to mitigate these problems we began work on developing a T4 template that would auto generate a repository pattern infrastrucre from the EDMX file generated by Entity Framework. You can then use the repositories for accessing data, a Repository Factory for accessing the repositories themselves and maintaining save state and all with built in interfaces that can be easilly mocked. This also allows you to add alternative data sources to the same model by adding additional classes.
Unit testing is a straightforward matter of then mocking the Repository Factory interface and then adding the needed repository mocks for each testing layer. All of the autogenerated classes are rendered as Partial Classes, so you can easilly add custom functionality in addition to the default generated methods of...
We've now done a new version of the template that should work with DbContext and POCOs in pretty much the same way (although it's easier to test the POCO approach, I still prefer the repository layer for data source abstraction).
The templates have variables at the top of them to specify the linked EDMX name and the name of the Id field to use when code generating.
The ObjectContext version is pretty solid and there is a version out now for EF 4.3.1, 5 and 6. The DbContext one hasn't been tested in anger yet, so any issues you may find please let us know - we've set up an issue tracker.
You can get these template from nuget - just search for 'Ministry' and choose the appropriate template for your EF version and Context.
Using Entities (ObjectContext) when upgrading to EF6
This is rather broken on the Microsoft side of things - if you upgrade an EDMX the code generation strategy still generates code using the wrong namespaces for EF 6. You can fix this by right clicking the EDMX surface, selecting 'Add Code Generation Item' and selecting 'EF6.x EntityObject Generator' - If it's not available locally it'll be available in the 'online' section. Marry this with the new EF6 ObjectContext Repo template and you should be good to go.