How to marry EntityFramework, Repository, UnitOfWo

2019-02-14 16:43发布

问题:

First I decided to create one Interface called it IDataAccessLayer and started putting everything into it: methods like GetUsers(), GetUser(int id), GetOrderByNumber(int number), DeleteOrder(int Id) etc.

That worked just perfect at first. But then I realized that the concrete implementation of DataLayer:IDataLayer is growing to big. I decided to cut it into several partial class files. Still I was feeling that I'm doing something really wrong.

Then I decided to create interfaces for each logical part like IUsers, IOrders, IItems etc. Didn't work, because I was accessing repository through one dependent property injected into controller's constructor. So I couldn't just add another property everytime I need to use different type of dataContext in my controller.

Then after many hours reading through articles about Entity Framework, I finally realized that I have to use Repository and Unit of work patterns. And still I need to somehow separate POCOs from my ViewModel objects, although almost all the time they'd be sharing similarities. Automapper helps a lot. But now, I'm not sure how to use everything together. Entity Framework, Patterns, Automapper and Dependency injection framework like Ninject.

I don't have clear understanding how to mix that all into one awesome architecture. Can you please show me some nice examples.

回答1:

First here is an overall article about n-tier architecture using the Repository and UnitOfWork principles: link. I have some experience working with EF and the afore mentioned patterns and I found this article of great help.

Take a look here as well as here for the MSDN articles on those principles.

Regards.



回答2:

You might take a look at this sample (MVCArch) I've written some months ago. It takes advantages of :

  • Entity Framework
  • Repository & Unit Of Work patterns
  • Automapper
  • Ninject
  • etc...

Hope this helps.



回答3:

Did you go through these tutorials.