简体   繁体   English

如何为mvc项目设置架构

[英]How to setup architecture for mvc project

I am trying to get my hands on MVC. 我正试图搞定MVC。 I am from ASP.Net background. 我来自ASP.Net背景。

After creating new mvc 3 application, i got Controller, Models and views under the same webapp project. 在创建新的mvc 3应用程序后,我在同一个webapp项目下获得了Controller,Models和views。 In ASP.Net, we generally create separate projects for Models and Controllers (which i assume are same as Business Layer). 在ASP.Net中,我们通常为模型和控制器创建单独的项目(我假设它们与业务层相同)。 Also i created a separate project for DAL where i will be using EF. 我还为DAL创建了一个单独的项目,我将使用EF。

I am confused as is this the ideal solution structure? 我很困惑,因为这是理想的解决方案结构? Should we not create separate projects for each layer? 我们不应该为每一层创建单独的项目吗? Since i created DAL as a separate project, i had to put a reference of WebApp in it because i wanted to return Model from the DAL and because of that now i am not able to add a reference of DAL to my WebApp. 由于我创建了DAL作为一个单独的项目,我不得不在其中添加WebApp的引用,因为我想从DAL返回Model,因此我现在无法将DAL的引用添加到我的WebApp中。

Can someone please throw some light on what am i missing here? 请问有人可以解释我在这里失踪的内容吗? Am i not doing it right? 我做得不对吗?

MVC really leaves the "M" part up to the developer. MVC确实将“M”部分留给了开发人员。

Even in their official examples you'll see variations. 即使在他们的官方示例中,您也会看到变化。 Your question exposes one of the most common misconceptions about MVC. 您的问题暴露了一个关于MVC的最常见的误解。 You should NOT bind your domain or data models directly to views, nor should your controller methods accept them as parameters. 您不应将域或数据模型直接绑定到视图,也不应将控制器方法接受为参数。 See this post on over and under-posting . 关于过度和不足的帖子,请参阅此帖子

Ideally, your controllers will call out to a DAL, and some mechanism will map those Data or Domain models to View models. 理想情况下,您的控制器将调用DAL,并且某些机制会将这些数据或域模型映射到View模型。 It is those View models - models that exist specifically to facilitate the UI - that should exist in the WebApp "Models" folder. 正是那些View模型 - 专门用于促进UI的模型 - 应存在于WebApp“Models”文件夹中。

So, you were definitely on the right track creating a new assembly to contain your DAL. 所以,你肯定是在正确的轨道上创建一个新的程序集来包含你的DAL。 One of the "easiest" mechanisms for mapping to a ViewModel is a simple method on each ViewModel: 映射到ViewModel的“最简单”机制之一是每个ViewModel上的一个简单方法:

public class MyWidgetFormModel()
{
   public string Name { get; set; }
   public string Price { get; set; }

   public MapFromDAL(DAL.Widget widget)
   {
      this.Name = widget.Name;
      this.Price = widget.Price;
   }
}

Update : based on your comments, here is an excellent answer about one user's project layout. 更新 :根据您的评论, 这是一个关于一个用户的项目布局的优秀答案

When I started with MVC i followed the Jeffrey Palermo onion architecture. 当我开始使用MVC时,我遵循Jeffrey Palermo洋葱架构。 You can read about it : 你可以阅读它:

here : http://jeffreypalermo.com/blog/the-onion-architecture-part-1/ 这里: http//jeffreypalermo.com/blog/the-onion-architecture-part-1/

here : http://jeffreypalermo.com/blog/the-onion-architecture-part-2/ 这里: http//jeffreypalermo.com/blog/the-onion-architecture-part-2/

and here : http://jeffreypalermo.com/blog/the-onion-architecture-part-3/ 在这里: http//jeffreypalermo.com/blog/the-onion-architecture-part-3/

It's using a IoC support for decoupling services. 它使用IoC支持解耦服务。 I think that you should consider usage of IoC containers because the MVC architecture was thought around patterns using IoC in order to decouple services (layers). 我认为您应该考虑使用IoC容器,因为MVC架构被认为是使用IoC来解耦服务(层)的模式。

You can also dowload a working sample from http://codecampserver.codeplex.com/ using onion architecture. 您还可以使用洋葱架构从http://codecampserver.codeplex.com/下载工作样本。

It's not the only architecture you can use with MVC but it's a very good place to start and to learn about IoC and decoupling in MVC applications. 它不是唯一可以与MVC一起使用的架构,但它是一个非常好的起点和了解IoC以及在MVC应用程序中解耦的地方。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM