Pages

Monday, March 7, 2016

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.