简体   繁体   English

为什么大多数ASP.NET MVC示例都直接在表示层中访问数据库?

[英]Why do most ASP.NET MVC examples access the database directly in the presentation layer?

Most of (nearly all?) the examples I find on how to set up an ASP.NET MVC project are accessing the database context directly in the controller. 我发现的有关如何设置ASP.NET MVC项目的大多数(几乎所有)示例都是直接在控制器中访问数据库上下文。

Like this for example: 这样的例子:

public class MoviesController : Controller
{
    private MovieDBContext db = new MovieDBContext();

    //
    // GET: /Movies/

    public ViewResult Index()
    {
        return View(db.Movies.ToList());
    }

I also know that there are a lot of controls (at least for the aspx view engine) that you can bind to a table in the database directly as a data source, so that it automatically displays the data. 我也知道,有很多控件(至少对于aspx视图引擎而言)可以作为数据源直接绑定到数据库中的表,以便它自动显示数据。

To me this feels weird and I would like some kind of separation between the presentation layer and the database. 对我来说,这感觉很奇怪,我希望在表示层和数据库之间进行某种分离。 Some kind of business layer and/or data layer that maps data from the database to view models, before using them in the view. 某种业务层和/或数据层,用于将数据从数据库映射到视图模型,然后再在视图中使用它们。 Is it just me or are the examples all like this because it's easier to do? 仅仅是我还是因为这些例子更容易做到? Is there some great gain that I'm missing? 我缺少一些巨大的收获吗? I guess it's a bit faster, but it just feels like I shouldn't use the same models in my database as in my views. 我想它会更快一些,但是感觉就像我不应该在数据库中使用与视图中相同的模型。 I finally found an example that feels more right, where the database models are separated from the view models. 我终于找到了一个感觉更正确的示例 ,其中数据库模型与视图模型分离。 But it's one example of a hundred. 但这是一百个例子。

What are your thoughts on this? 您对此有何想法?

I understand your concerns as I have the same. 我也知道您的担心。 It's really a pity that most of the examples out there are not using view models. 遗憾的是,大多数示例都没有使用视图模型。 Because of this people are struggling a lot when they start implementing a real application that differs from the most trivial examples seen in those articles. 因此,当人们开始实现一个与那些文章中看到的最琐碎的示例不同的真实应用程序时,他们会付出很多努力。

As far as directly accessing the database from the controller, I don't think that this is such a big concern. 至于直接从控制器访问数据库,我认为这不是一个大问题。 You really don't need to implement lots of layers of abstraction if you don't need them and if they don't bring any additional value to your application. 如果您不需要抽象层,并且它们不会给您的应用程序带来任何附加价值,那么您实际上不需要实现许多抽象层。 Jimmy Bogard wrote an excellent blog post on the subject of limiting your abstractions. 吉米·博加德(Jimmy Bogard)在关于限制抽象的主题上写了一篇很棒的博客文章

Most MVC tutorials teach you how to do it simply because it can be done, and that the explanation of the Controller usually comes before explaining the Model. 大多数MVC教程仅因为可以完成操作,而教您如何进行操作,并且通常在解释模型之前先对Controller进行解释。

Take the Movie App tutorial for example - http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3 以电影应用程序教程为例-http: //www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3

The majority of these tutorials first teach you how to display data using on the view, then using the controller to display in the view, then entering the data into the model and directing to the view via. 这些教程中的大多数都首先教您如何在视图上使用显示数据,然后使用控制器在视图中显示,然后将数据输入模型并通过定向到视图。 the controller. 控制器。

这是为了使示例简单易行,并专注于手头的主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC数据库访问层 - ASP.NET MVC Database Access Layer 如何将数据从表示层传递到业​​务逻辑层? (ASP.NET MVC 5) - How do I pass data from presentation layer to business logic layer? (ASP.NET MVC 5) 如何使表示层(ASP.NET MVC)的数据访问技术(Entity Framework)无知? - How to make the data access technology (Entity Framework) ignorance from the presentation layer (ASP.NET MVC)? 具有服务客户端作为数据库访问层的ASP.NET MVC 3 - ASP.NET MVC 3 with service client as database access layer ASP.NET MVC:如何让我的业务规则验证冒泡到表示层? - ASP.NET MVC: How can I get my business rule validation to bubble up to the presentation layer? ASP.NET MVC3和Unity-将Unity实现与表示层分离? - ASP.NET MVC3 and Unity - Decouple Unity implementation from presentation layer? ASP.NET MVC数据库访问 - ASP.NET MVC Database access ASP.NET MVC数据库无法访问 - ASP.NET MVC database can not access 数据库访问和ASP.NET MVC - database access and asp.net mvc 令人费解……为什么我在 ASP.NET MVC 中的大多数链接都附加了 Length=4? - Puzzling … why do most of my links, in ASP.NET MVC, have Length=4 appended to them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM