Monday, March 7, 2016

A generic C# data access layer built on top of the Entity Framework

The Entity Framework is the Microsoft official ORM (Object-Relational Mapping) framework.

This library implements the Repository Pattern. It's mask the detail of the Data Access Logic from the Business Logic using Repositories.
Repositories are build on top of Entities using C# generics.
Repositories offers a few methods to perform CRUD operations (Create, Read, Update, Delete) and other SQL operations like MAX, MIN...
An instance of the entity framewor context class is created for each operation.
Each repository implements an interface that provides methods to performs operation on an entity object of the model.
You can access any repository by the main model instance. Each repository will be instantiated at the model construction.
Any repository method can be overriden.
The main model class implements the a generic class and defines each repository as a public property.

The main methods you will find on repository are:
  • List: load an entities with records
  • Find: load an entities searching for key values
  • Add: add records to the underlying database
  • Update: update records of the underlying database
  • Remove: remove records from the underlying database
The List method filter values by the use of a predicate.
The generated LINQ query is then translated to SQL by the Entity Framework engine.
The Find method implements the Find LINQ method.
Add, Update and Remove methods perform the SQL INSERT, UPDATE and DELETE operations.

Three methods should be overridden almost every time a Repository is implemented:
  • CanAdd: check if entities can be added
  • CanUpdate: check if entities can be updated
  • CanRemove: check if entities can be removed
Those methods should be called before the main Add, Update and Remove operations to check for entites consistency.

Other SQL/LINQ operators are implemented in the repository, like:
Any, FirstOrDefault, Average, Order, Count, LongCount, Max, Min, Remove, Sum.

A LanguageHelper class provides usefull methods to internationalize String fields used in repositories. Those fields can be used to localize errors message for repository methods.

A few lines of codes are enough to implemt a fully loaded model.

This project is as always release open source. You can find source code, on the link above.
The code also contains a sample model, and a few UnitTest to check against it.


  • read risk disclaimer
  • excuse my bad english