如何在一个MVC应用程序中结合EntityFramework,Repository,UnitOfWork和Automapper? [英] How to marry EntityFramework, Repository, UnitOfWork and Automapper altogether in one MVC application?

查看:192
本文介绍了如何在一个MVC应用程序中结合EntityFramework,Repository,UnitOfWork和Automapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我决定创建一个名为 IDataAccessLayer 的界面,并开始将其中的所有内容加入到其中:方法如 GetUsers() GetUser(int id) GetOrderByNumber(int number) DeleteOrder )等。

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.

起初刚刚完美。但是后来我意识到, DataLayer:IDataLayer 的具体实现正在增长。我决定把它切成几个部分类文件。仍然我觉得我在做一些真正错误的事情。

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.

然后我决定为每个逻辑部分创建接口,如 IUsers IOrders IItems 等没有工作,因为我通过注入一个依赖属性访问存储库进入控制器的构造函数。所以我每次需要在控制器中使用不同类型的dataContext时,都不能添加另一个属性。

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.

然后经过很多小时阅读有关实体框架的文章,我终于意识到我必须使用存储库工作单位图案。而且我仍然需要以独特的方式将POCO与我的ViewModel对象分开,尽管几乎所有的时间他们会分享相似之处。 Automapper帮助很多。但现在,我不知道如何一起使用。实体框架,模式,自动插件和依赖注入框架,如Ninject。

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.

推荐答案

首先,这里是一个关于使用Repository和UnitOfWork的n层架构的整体文章原则:链接。我有一些使用EF和上述模式的经验,我发现这篇文章很有帮助。

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.

看看这里以及这里针对这些原则的MSDN文章。

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

问候。

这篇关于如何在一个MVC应用程序中结合EntityFramework,Repository,UnitOfWork和Automapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆