Entity Framework 6 , Should I use repository pattern?

entity-framework entity-framework-6 repository-pattern

Question

I am quite new to Entity Framework, and still struggling to solve it.So many different examples and explanations that i can't figure out what to do ?

I have a few questions :

In my current application I used a pattern which consist of :

  • Repository Pattern
  • (On top of )Unit Of Work

For each from , I am declaring a NEW Unit Of Work .So when I make a Savechanges, only the related items are affected.

For complex business logics , I am writing my methods inside the Repository for each class For example :

  • One method for : Remove one item from one inventory location , move it to another location and then decrease the quantity at the old location, etc... -Or much more complex jobs, which reads from different tables and write to many tables.

However recently I found out that by Entity Framework 6.0 :

  1. DbContext = Unit Of Work
  2. DbSet is equivalent to REPOSITORY

So the questions are :

  1. Should i use ONE DBCONTEXT ,or declare a new for each form/operation so the transactions remain in the local context ( form/operation =
    1. How will I replace the methods in my the repository ?

Thanks

1
2
11/27/2014 3:40:49 PM

Accepted Answer

First of all trying to treat your DbContext as singleton object is a bad idea, because you can't manage transactions in your operations. you have to instantiate your DbContext per operation.

Second, try to separate your "Data layer concerns" from your "Business concerns". for example :

One method for : Remove one item from one inventory location , move it to another location and then decrease the quantity at the old location

I think you are writing your repository methods per use case which is not a data layer concern. your repositories are only need to know about persisting and retrieving objects. in another way your repository methods are just CRUD methods. Your business layer will use this methods to perform business logic.

1
11/27/2014 4:32:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow