Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια

Dot Net Rules

Yes, to dance beneath the diamond sky with one hand waving free

Ιστορικό Δημοσιεύσεων

Entity Framework and Repository Pattern

In this post I will try to explain why the Repository Pattern is not redundant when we use an ORM like Entity Framework in our application.

There are many blog posts and many questions/answers in places like StackOverflow where developers argue about that issue.

I will not be writing code in this article but I will try to explain why Entiti Framework does not implement the Repository Pattern and what is the difference between them are.

Before reading this post by all means do have a look at this post of mine where I explain the Repository Pattern in detail.


Let me say a few words first about Entity Framework first. Entity Framework is an ORM.

The .Net framework provides support for Object Relational Mappingthrough EF.

So EF is a an ORM tool and it is now the main data access technology that microsoft works on.

I use it quite extensively in my projects.

Through EF we have many things out of the box provided for us. We have the automatic generation of SQL code.

It maps relational data to strongly typed objects.All the changes made to the objects in the memory are persisted in a transactional way back to the data store. 

In a nutshell it bridges the relational with the object oriented world. A lot of developers like it because they do not want to learn SQL or deal with low level database specification details. As with all things there is a learning curve and a lot of developers use EF code in many layers in their application making the code non-testable an non-maintenable.

The Repository Pattern abstracts and encapsulates anything that has to do with persiestence.

We can use Linq to Entities or stored procedures to store data in a data store that can be a file, a document db, a  cloud storage or a relational database. The rest of the application does not know that, as the Repository encapsulates everything storage related.

At this point I want to underline the fact that many developers confuse the domain objects with persistence objects like an EF entity.

The domain model models the behavior of the application. The persistence model models how and what data will be stored, so in essence it models storage structure.

Having all these in mind let's move to the argument that many developers put forward "that there is no need to use the Repository Pattern when we are using Entity Framework which implements the Repository Pattern, hence the Repository Pattern is redundant".

Entity Framework main objects are DbSet and DbContext. DbSet looks like a repository.

DbSets represent objects in memory. There are methods for adding, removing, getting objects.

The DbContext has methods for storing the data. So one could argue that DbContext implements UnitOfWork pattern.

One main benefits of the repository pattern is that there is no duplicate query logic and a separation of concerns. 

All of your queries for data when using Entity Framework are written against DbSet.

For example, when we are querying against a Customer or a Doctor entity, those entities are exposed on our DbContext as a DbSet.

DbSet implements the IQueryable interface. 

public class DbSet<TEntity> : DbQuery<TEntity>, IDbSet<TEntity>, 
	IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable
where TEntity : class

DbSet with the LINQ extension methods implemented on it,  return IQueryable.

That means we will have queries like the one below(if we are implementing a book e-shop)

context.Books

.Where(b=>b.BookID==bookid)

.Select(b=>b.name)

.Include(a=>a.Author)

.ToList();

in all layers of the application. We will have queries like the one above in our controller or service layer.

That is a leaky abstraction. UI or service layer should not know about how to implement data store persistence logic.

So we violate both the seperation of concerns and the duplication of query logic.

Another benefit of the repository pattern is that it decouples your application or domain entities from persistence frameworks.

In our applications when using EF we use DbContext directly in our application, our application is tightly coupled to Entity Framework. So our application is tightly coupled with the persistence framework. In most cases we do not want that. Repository pattern definetely help us to achieve persistence abstraction.

Some people say that I wil use EF and I am not going to change to nHibernate or any other ORM. Well maybe EF in 4-5 years from now has a different implementation of DbContext and DbSet. What will happen if you want to update the code. Many changes should take place inside the code.

Bottom line is that we need to hide DbContext behind a repository and only use the repository interfaces in your application. The repository is a softwar design pattern that helps us abstract all storage related details from the rest of the application. Entity Framework abstracts access to an relational database e.g SQL Server. Basically EF is an implementation detail of the Repository.

Hope it helps!!!

Share
Posted: Κυριακή, 31 Ιουλίου 2016 10:32 μμ από το μέλος nikolaosk
Δημοσίευση στην κατηγορία: ,

Σχόλια:

Χωρίς Σχόλια

Έχει απενεργοποιηθεί η προσθήκη σχολίων από ανώνυμα μέλη