Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service Layer/Repository Pattern

I am building an MVC app using the Service Layer/Repository/Unit of Work pattern with EF4.

I am a bit confused on the logic. I know the point is to decouple the system, but I am a little confused.

So the MVC Controllers call on the Services to fill the View Models. So is it safe to say the MVC App is coupled to the Service Layer?

Then the Service Layer calls on the Repository to get and persist objects. Is then safe to say the Service Layer is dependent to the Repository?

The Repository the utilizes EF4 to get and persist data to SQL server, so I would assume the Repository depends on EF4 which in turn depends on SQL Server.

Where does the unit of work all fit in.

Any examples please? Thanks!!

like image 720
Sam Avatar asked Mar 03 '26 04:03

Sam


2 Answers

I started with hiding Unit of work somewhere in lower layer but it is wrong way to do that. After some experience my opinion is:

  • In case of monolitic application UnitOfWork should be accessible by Controller and lower layers.
  • In case of distributed application (UI and BL are on different servers) UnitOfWork should be accessible by business layer facade (service layer for remote calls) and lower layers.

The reason is that mentioned layers define what is the "business transaction" = what is current unit of work. Only this layer knows when it wants to commit changes to data store. Doing it this way allows service composition (code reuse). I discussed similar question here and here.

like image 105
Ladislav Mrnka Avatar answered Mar 05 '26 20:03

Ladislav Mrnka


Sam,

Julie Lerman did a good screencast on DNR tv, talking about this, there is also another screen cast on Channel 9, around creating and testing repositories just EF here.

The general thing abut these is create the abstraction of the Unit of Work in Nhibernate it would be Session, in EF would be you context and passing that session or context into your repositories, as part of you test you can fake the connections to use a list of dictinary.

Hope these help.

Iain

like image 35
Iain Avatar answered Mar 05 '26 21:03

Iain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!