Patterns for using EntityFramework?

What are alternative usage patterns for the Entity Framework?

Some of them I know:

  • "Normal" EntityFramework - aka Unity of Work

     using (Data.Model c = new Data.Model()) { var z = c.Users.Where(x=>x.Name=='John'); } 
  • Storage template

     //Model implements IRepository User user = Model.Instance.Get<User>(u => u.Name == "John"); 
  • What else?
+6
c # design-patterns entity-framework repository-pattern
source share
5 answers

A good book to watch is Martin Fowler Corporate Enterprise Architecture Templates .

There, he looks at some templates for receiving / matching data, such as DTO, work unit, repository template, etc. Maybe something can be useful along with the Entity Framework. I need to take a look at this.

+2
source share

I answer your question on the assumption that you are using the Entity Framework directly in your interface / controller / services.

It has been proven that using any ORM that includes EF directly in the UI / Controllers / Services will cause many problems in the future. In addition, it makes it so difficult, if not impossible, to unit test your application.

The second approach, i.e. The "model implements the repository" is also mistaken, since the model and repositories have different responsibilities and are based on the "Unified" part of the SOLID principles, you should not combine these two concepts together. Even if you want to use the Active Objects template in your model, which I do not recommend, you should separate your model from the ORM you are using.

The best and most recommended solution is to have an interface, such as IRepository or IRepository, with the most basic members, as the template suggests. something like:

 Interface IRepository<T> where T:class { void Insert(T entity); void Update(T entity); void Delete(T entity); // if you don't want to return IQueryable T FindById(object id); IEnumerable FindXXXXX(params) // if you prefer to return an IQueryable IQueryable<T> Find(Expression<Func<T, bool>> predeicate); } 

Note that some poeple beleive repositories should not return IQueryable. Alternatively, you can use ISpecification instead of expressions and lambda.

You will need to implement the IRepositoy interface for most of your objects. Using this approach, you can also make fun of your repositories when writing unit tests. In production, you will need to use an Ioc provider such as Unity, Ninject, Linfu, Catsle, etc. You can also take advantage of the implementation of the local microsoft service to avoid communication with a specific IoC infrastructure.

In the old days, I had a data access interface that was implicated for a specific business domain or service. One of the problems with this approach is that you will have duplicate code in different data access services if you keep track of your source code, and in the end.

+1
source share

We use code similar to what you have in the example of your unit of work.

What we do in addition is mapping objects to data transfer objects.

0
source share

There are many articles on this subject, but the list will be greatly reduced if you want to get a good one. This MSDN article is pretty good, although it is specific to n-tier applications. But since you are not saying that you are building, perhaps this will help.

0
source share

LINQ 2 SQL may be an alternative. Here's the article Dependency Injection with Unity and Linq to SQL DataContexts

UNITY - http://unity.codeplex.com/

0
source share

All Articles