How does the communication between the layers in MVC work?

2

I started to study MVC, but I'm still in theory and I was wondering how the conversation between the layers works in practice. Could anyone give an example C # code without using the MVC framework?

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

    
asked by anonymous 14.05.2014 / 19:50

2 answers

1
  • The VIEW layer invokes the Click button.
  • The button click will call the BLL business layer .
  • BLL takes care of business rules and invokes layer DAL to interact with the database.
  •   

    Example, VIEW VIEW:

    <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);    
        }    
    %>
    

    BLL LAYER:

    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:
    link

        
    14.05.2014 / 20:21
    0

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

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

    You may notice that the first thing he calls is the controller / action which in turn works as a manager, doing a step by step to 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);
        }
    

    In my view, the controller is a manager who can consult a model to fetch data and finally move to a view that will appear on a user's screen.

    Note: All ActionResult has a view.

        
    22.05.2014 / 15:35