Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup architecture for mvc project

I am trying to get my hands on MVC. I am from ASP.Net background.

After creating new mvc 3 application, i got Controller, Models and views under the same webapp project. In ASP.Net, we generally create separate projects for Models and Controllers (which i assume are same as Business Layer). Also i created a separate project for DAL where i will be using 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.

Can someone please throw some light on what am i missing here? Am i not doing it right?

like image 309
Asdfg Avatar asked Jun 17 '11 21:06

Asdfg


People also ask

How is MVC architecture implemented?

MVC architectural pattern follows an elementary idea – we must separate the responsibilities in any application on the following basis: Model: Handles data and business logic. View: Presents the data to the user whenever asked for. Controller: Entertains user requests and fetch necessary resources.

What is MVC architecture in project?

The Model-View-Controller (MVC) framework is an architectural/design pattern that separates an application into three main logical components Model, View, and Controller. Each architectural component is built to handle specific development aspects of an application.

What is MVC architecture with example?

The model contains all the data-related logic that the user works with, like the schemas and interfaces of a project, the databases, and their fields. For example, a customer object will retrieve the customer information from the database, manipulate or update their record in the database, or use it to render data.

What is the architectural structure of an MVC framework?

The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application.


1 Answers

MVC really leaves the "M" part up to the developer.

Even in their official examples you'll see variations. Your question exposes one of the most common misconceptions about 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. It is those View models - models that exist specifically to facilitate the UI - that should exist in the WebApp "Models" folder.

So, you were definitely on the right track creating a new assembly to contain your DAL. One of the "easiest" mechanisms for mapping to a ViewModel is a simple method on each 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.

like image 124
Peter J Avatar answered Nov 14 '22 21:11

Peter J