Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between MVC controller and business logic (3 tier)

I have been trying to find the different between MVC and 3-tier architecture in ASP.NET. I referred to some previous some previous questions and some pages, but could find a clear answer.
Here is a a msdn page about MVC implementation: http://msdn.microsoft.com/en-us/library/ff647462.aspx

Consider, I ahve this code:
Single page aspx UI and code as well

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
   <head>
      <title>start</title>
      <script language="c#" runat="server">
         void Page_Load(object sender, System.EventArgs e)
         {
            String selectCmd = "select * from Recording";

            SqlConnection myConnection = 
               new SqlConnection(
                  "server=(local);database=recordings;Trusted_Connection=yes");
            SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, 
               myConnection);

            DataSet ds = new DataSet();
            myCommand.Fill(ds, "Recording");

            recordingSelect.DataSource = ds;
            recordingSelect.DataTextField = "title";
            recordingSelect.DataValueField = "id";
            recordingSelect.DataBind();
         }
       </script>
   </head>
   <body>
         <asp:dropdownlist id="recordingSelect" runat="server" />
         <asp:button runat="server" text="Submit" OnClick="SubmitBtn_Click" />
      </form>
   </body>
</html>

Now, consider I have different files for
---- View and Code-behind spearated ----
.aspx

<%@ Page language="c#" Codebehind="Solution.aspx.cs" 
   AutoEventWireup="false" Inherits="Solution" %>
<html>
         <asp:dropdownlist id="recordingSelect" runat="server" />
      </form>
   </body>
</html>

.aspx.cs

using System;
using System.Data;
using System.Data.SqlClient;
public class Solution : System.Web.UI.Page
{
   private void Page_Load(object sender, System.EventArgs e)
   {
      if(!IsPostBack)
      {
         String selectCmd = "select * from Recording";
         SqlConnection myConnection = 
            new SqlConnection(
               "server=(local);database=recordings;Trusted_Connection=yes");
         SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, myConnection);
         DataSet ds = new DataSet();
         myCommand.Fill(ds, "Recording");
         recordingSelect.DataSource = ds;
         recordingSelect.DataTextField = "title";
         recordingSelect.DataValueField = "id";
         recordingSelect.DataBind();
      }
   }
  1. Seeing the above msdn page link for the Controller class, I am not able to discern the difference between the business logic (that would have been similar for a middle tier in a 3 tier architecture) and the controller.
  2. Is 3-tier and MVC completely different thing? Are the ASP.NET application in Visual Studio already separated file as in MVC form? If these are not different, which one is the preferred style?
  3. What's the MVC framework about then if the .aspx and .aspx.cs are already spearated?
like image 612
user1240679 Avatar asked Mar 17 '12 11:03

user1240679


People also ask

What is the difference between MVC and 3 tier architecture?

Conceptually the three-tier architecture is linear. However, the [model-view-controller] MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model. The MVC architecture is not necessarily triangular, it can be either.

What is the difference between MVC and n tier architecture?

MVC abstracts away the details of how the architecture of an app is implemented. N-tier just refers to the physical structure of an implementation. These two are sometimes confused because an MVC design is often implemented using an N-tier architecture.

How will you define the 3 logical layers of MVC?

MVC pattern architecture is basically a three-layered architecture. It separates the characteristics of application. Its first layer is related to the user input logic, second layer is related to the business logic and third layer is used to implement user interface logic.

What is a business logic tier?

The business logic layer in a three-tier application consists of components that implement business rules, centralizing business logic, and separating it from the user interface and data access. The business logic tier simplifies maintenance and integration of new components.


2 Answers

I actually strugled with this a while myslef, there are different philosophies on how to implement it correctly, so this is how I understand it in my own words how I understand the relation between the different things involved (Models/Views/Controllers/Business Logic):

  • Views
    Have all the Html / jQuery code, they use the data in form of Model instances that come from the controller
  • Models
    The classes that hold the information your View needs to render (List of products etc)
  • Controllers
    • They take incoming requests
    • Make the necessary preparations (parameter extraction etc.) to call your actual business logic code.
    • Then the call into the business logic code and retrieve the result
    • Afterwards they take the result and transforms it into a Model that the UI understands
  • Business logic
    This is your actual business logic code, calling the db etc. I my eyes this is totally separate from the entire MVC thing, in fact it should not even know it is executed from a MVC web application. Usually we put this in a different assembly (class library) to make sure there is no dependency to MVC code at all.
    This makes it very simple to unit test just the business logic, since there are no dependencies to MVC.

I have seen other approaches where the business logic is actually put into the controllers, but in my eyes that defeats the purpose. We are not building MVC applications to have a nice structure, but also to be able to perform unit test better.

Getting back to your question, how does it all relate to ASP.NET 3-tier architecture.
One could say that basically the entire MVC web application is nothing more the Presentation layer (+ wireing up the Presentation layer with the Busines layer).

The other layers stay separate and independent from the presentation layer, as they should have before.

like image 157
ntziolis Avatar answered Sep 18 '22 16:09

ntziolis


MVC and 3-tier are completely different things.
I see a lot of people confuse the two because both got 3 parts.

MVC is a UI pattern:
View: contains html and js only (in case of a web project)
Controller: is a kind of a mediator between the UI (= the view) and the back-end (= the model)
Model: this is where your domain objects live, as well as the business and data-access logic

3-tier concerns the whole of you application:
UI: contains the html/js as well as the code behind of the pages.
There is absolutely no logic here, other than UI code and calling the business layer.
Business layer: this is where you put things like calculations, conditions, validation, ..
So the actual behaviour of your application. There is no data access code here.
Data access: here where you talk to the database and return the data to the business layer.
Nothing else, the business layer should know what to do with it.

So if you combine the two, you'll get:
UI: views and controllers
Business layer: part of the model
Data access: part of the model
Domain object: you'll want to put the objects you're working with (product, order, ..) in a separate layer.
This is also a part of the model.

Shoot if you got questions!

like image 20
David Avatar answered Sep 22 '22 16:09

David