How does MVC layer-to-layer communication work?

Asked

Viewed 1,179 times

2

I started studying MVC, but I’m still in theory and I was wondering how the conversation between the layers works in practice. One could give an example of C# code without using the MVC framework?

Note: Before you suggest, I’ve seen the answers to a similar question (How the interaction between the layers in C# works and what the function of each one?) and I still continue with my doubt. I would like examples, code.

2 answers

1


  1. The VIEW layer invokes the Click button.
  2. The click of the button will call the business layer BLL.
  3. BLL is in charge of making the business rules and invokes the layer DAL to interact with the database.

Example, VIEW LAYER:

<asp:TextBox ID="txtNome" runat="server"></asp:TextBox>    
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>    
<asp:Button ID="btnSalvar" runat="server" Text="Salvar" OnClick="btnSalvar_Click" />    


<%    
    void btnSalvar_Click(object sender,EventArgs e)    
    {    
        //Envia os dados do formulário    
        //para a camada de lógica da aplicação(BLL)    
        Bll.Usuario bllUsuario = new Bll.Usuario();    
        bllUsuario.Salvar(txtNome.Text,txtEmail.Text);    
    }    
%>

LAYER BLL:

namespace Bll
{
    public class Usuario
    {
        public void Salvar(String Nome, String Email)
        {
        //Realiza a lógica com os dados recebidos
        //da camada de apresentação, e envia para
        //a camada de acesso a dados (DAL)
            if (Nome != "" && Email != "")
            {
                Dal.Usuario dalUsuario = new Dal.Usuario();

                dalUsuario.Salvar(Nome, Email);
            }
        }
    }

DAL LAYER:

namespace Dal
{
    public class Usuario
    {
        public void Salvar(String Nome, String Email)
        {
        //Recebe os dados recebidos da camada de lógica (BLL)
        //e salva os dados no banco de dados (back-end)
            SqlCommand cmd = new SqlCommand("SalvarUsuario", conexao);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@Nome", Nome);
            cmd.Parameters.AddWithValue("@Email", Email);
            cmd.ExecuteNonQuery();
        }
    }
}
}

SOURCE:
http://imasters.com.br/desenvolvimento/visual_studio/aplicacao-em-3-camadas-com-asp-net-c/

  • You’re sure this is MVC. It’s not the MVC I know. The article you quoted says otherwise. You’re using Webforms. Although it’s possible model it as MVC (it takes work, it doesn’t pay, I don’t know anyone who did it) it’s not him who is used for MVC.

  • I gave this example because I think the question of @Caique C. was what the conversation code between the layers looks like, see this excerpt from the question "Could someone give an example of C# code without using the MVC framework?".

  • I get it, the title and internal questions are discrepant.

0

The Mvc is composed of 3 layers Model View and Controller, in Asp . net mvc we have the following flow:

the web browser calls an action such as your/Controller/action domain or to better exemplify localhost/home/index as shown in the code below:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

You can notice that the first thing it calls is the controller/Action that in turn works as a manager, doing a step by step until you get to the view, in the code below we have an implementation where the controller has to fetch data, so in this case it will have to access the model.

public ActionResult Index()
    {
        var person = new PersonRepositoty();
        var model = person.PersonRep.Get();

        return View(model);
    }

Which then goes to the view, which is in View/Home/index.

Soon to my way of seeing the controller is a manager who can consult a model to fetch data and finally pass to a view that will present in a user’s screen.

Note: Every Actionresult has a view.

  • 1

    Technically Actionresult can be many things besides a View. It can be a JSON, a Stream, an image etc

  • Opa perfect your comment...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.