Is it correct in MVC to access data within the model?

Asked

Viewed 122 times

3

In a C# MVC project, it is correct to access data within the model? For example:

public class Cliente
{
    public int Id { get; set; }

    public string Nome { get; set; }

    //outros atributos...

    public void Inserir(){
        Cliente c = new Cliente();

        //Outras ações

        DaoCliente dao = new DaoCliente();
        dao.InserirCliente(c);
    }
}

That is correct?

2 answers

5


In general everything that refers to the data should be done in the model, even if indirectly. Any data handling operations should be in the template.

Then the method of entering that data should be placed in the same model. And you can put the detail out of it, as was done in this example.

I imagine the question is whether the controller should make all implementation of the insertion and generally should not.

Each with their own responsibility. The controller should tell you what to do and not how to do it.

I hadn’t paid attention initially, but the line Cliente c = new Cliente(); does not do what you imagine. You are creating another object within the object you are manipulating. This should not be done unless you have a reason to have a new customer within the customer, which is highly unlikely.

It is not this client that should use to record the template, it is the this.

I think it’s weird create a new client in invalid state.

And I don’t think this DAO is built well. I actually question the use of specific DAO like this, but this is another subject.

  • That was the question. It helped too much. Thank you.

4

Just one observation, if you have a method within the class itself, you don’t need to instantiate a new object:

public class Cliente
{
    public int Id { get; set; }

    public string Nome { get; set; }

    //outros atributos...

    public void Inserir(){
        //Outras ações

        DaoCliente dao = new DaoCliente();
       this.Id = dao.InserirCliente(this); //Já atribui a Id que foi inserida ao objeto atual
    }
}

But I would use a static method:

public class Cliente
{
    public int Id { get; set; }

    public string Nome { get; set; }

    //outros atributos...

    public static int Inserir(Cliente c)
    {
        //Outras ações

        DaoCliente dao = new DaoCliente();
        return dao.InserirCliente(c);
    }
}

or

public class Cliente
{
    public int Id { get; set; }

    public string Nome { get; set; }

    //outros atributos...

    public static int Inserir(string nome)
    {
        Cliente c = new Cliente();
        c.Nome = nome;
        //Outras ações

        DaoCliente dao = new DaoCliente();
        return dao.InserirCliente(c); //retorna a id que foi inserida
    }
}

can still be overloaded.

I know it’s not the answer to what was asked, but it’s just an observation that I think is valid. I hope I don’t get negatives for that =]

Browser other questions tagged

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