views:

950

answers:

2

How to implement Repository pattern withe LinqToEntities how to implement the interface

+1  A: 

I do the following:

A service layer contains my business objects. It is passed the repository via an Inversion of Control (Castle Windor is my usual choice). The repository is in charge of mapping between the business objects and my entity framework objects.

The advantages: You have no problems with object state or the context of the EF objects because you are just loading them during data manipulation on the repository side. This eases the situation when passing them to WCF/Web-Services.

The disadvantages: You are losing some of the tracking functionality of Entity Framework, you have to manually load the data object (ef objects), possibly if required manually to optimistic concurrency checks (via a timestamp on the business object for example).

But generally I prefer this solution, because it is possible to later change the repository. It allows me to have different repositories (for example my user object is actually using the ASPNetAuthenticationRepository instead of the EntityFrameworkRepository) but for my service layer it's transparent.

With regards to the interface, I would use the business objects from the service layer as your parameter objects and don't let those EF objects out of the repository layer. Hope that helps

AlexDuggleby
+1  A: 

I´ve almost like this except for the "Castle Windor" stuff. Take a look at openticket.codeplex.com

Glenn