在实践中,应上下文(实体框架)的控制器或抽象的另一层使用? [英] In practice, should Contexts (Entity Framework) be used in controllers or in another layer of abstraction?

查看:125
本文介绍了在实践中,应上下文(实体框架)的控制器或抽象的另一层使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在学习的.Net MVC用C#。我像人们如何使用他们的实体框架上下文很好奇。几个相关的问题在下面列出。我希望你能提供反馈。

I'm currently learning .Net MVC with C#. I'm very curious as to how people use their Entity Framework contexts. Several related questions are listed below. I hope you can provide feedback.


  1. 如果上下文中的控制器实例化?在模型?在抽象的另一层完全?什么是正确的封装?

  1. Should contexts be instantiated in controllers? In models? In another layer of abstraction altogether? What would be the proper encapsulation?

在做一些简单的CRUD操作,应该在电话说context.Add(实体)在控制器中叫什么?

When doing simple CRUD operations, should the call to say context.Add(entity) be called in the controller?

在创建查询的背景下,应该将这些查询可以在控制器中做了什么?或模型?如果这些使用静态方法,如果在模型中使用?

When creating queries to the context, should these queries be done in the controller? Or in the model? Should these use static methods if used in the model?

我希望我的问题是很清楚的。总的来说,我很感兴趣应该如何与数据库进行交互,以及如何将抽象的,适当从应用程序。涉及到的任何信息或意见是值得欢迎的。

I hope my questions are quite clear. In general I'm interested how one should interact with a database, and how to abstract that properly from the application. Any info or advice related to that is welcome.

推荐答案

一切都取决于你的应用程序的大小。但是,对于大型应用程序通常不需要在控制器实例的依赖。它使你的代码紧耦合一些特定的数据访问提供者实现。如果你将移至 MongoDB的明天?想想哪些地方你需要在你的应用程序来改变。

All depends on size of your application. But for large applications you usually do not instantiate any dependencies in controllers. It makes your code tightly coupled to some specific data access provider implementation. What if you will move to MongoDB tomorrow? Think about which places you will need to change in your application.

同样涉及当你做控制器单元测试数据访问提供的嘲笑。你应该能够切换实物,它不持久性应用程序,其中一些嘲笑的实现。 。这将是不可能的(或至少是很难),如果你将使你的控制器依赖于上下文,如果您将实例在控制器方面

Same relates to mocks of data access provider when you do unit testing of controllers. You should be able to switch real object, which does persistence in your application, with some mocked implementation. That will be impossible (or at least very hard) if you will make your controllers depend on context and if you will instantiate context in controllers.

传统的做法是以下内容:

Traditional approach is following:


  • 创建数据访问抽象,你的控制器,将取决于。通常是这样抽象。

  • 并注射执行这一抽象与一些依赖注入框架控制器

  • Create data access abstraction, which your controllers will depend on. Usually repositories are such abstraction.
  • And inject implementation of this abstraction into controller with some dependency injection framework.

这样控制器就会知道大约只有抽象,这将是容易改变的数据访问提供者(只需创建存储库的MongoDB,并给该实施控制器)也将很容易给嘲笑实现数据访问提供的,当你做的控制器单元测试。

Thus your controller will know only about abstraction, it will be easy to change data access provider (just create repository for MongoDB and give that implementation to controller) also it will be easy to give mocked implementation of data access provider, when you do unit testing of controllers.

示例:

public class SalesController : Controller
{
    private IOrderRepository _repository; // depend on interface

    // inject some implementation of dependency into controller
    public SalesController(IOrderRepository repository)
    {
       _repository = repository;
    }

    public ActionResult Index()
    {
        var orders = _repository.FindAll();
        return View(orders);
    }
}



在这里您将用实实在在的仓库实现的唯一场所,是依赖注入框架的配置,这是很容易改变的实现:

The only place where you will use concrete repository implementation, is configuration of dependency injection framework, it's very easy to change implementation:

Bind<DbContext>().To<ShopEntities>();
Bind<IOrderRepository>().To<EFOrderRepository>();



此外,它是很容易做到的单位的测试

[Test]
public void ShoulReturnAllOrders()
{
    List<Order> orders = CreateListOfOrders();
    var mock = new Mock<IOrderRepository>();
    mock.Setup(r => r.FindAll()).Returns(orders);

    var controller = new SalesController(mock.Object);
    var result = (ViewResult)controller.Index();

    mock.VerifyAll();
    Assert.That(result.Model, Is.EqualTo(orders));
}

这篇关于在实践中,应上下文(实体框架)的控制器或抽象的另一层使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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