Where to place the View Model rule

Asked

Viewed 206 times

3

I need to create a View Model to use on the screen.

I’m wondering where to put the button rule be enabled or not.

I thought about this implementation, I would like to know if it is the best way. Another idea I had was to put the contents of the constructor in a class of Business.

public class ApoQueueVM
{
    public ApoQueueVM(ApoQueue apoQueue, ApoFileBL apoFileBL)
    {
        this.EnableExport = apoQueue.Status == ApoQueueStatus.Gerado.ToString() || apoQueue.Status == ApoQueueStatus.Enviado.ToString();
        this.KeyFigure = apoQueue.KeyFigure;
        this.PathFileGenerated = apoFileBL.GetFullPathApo(apoQueue, true);
    }


    public string KeyFigure { get; set; }
    public bool HaveInconsistencies { get; set; }
    public string PathFileGenerated { get; set; }
    public bool EnableExport { get; set; }
}

2 answers

3


Seems like a good approach in the builder.

Additionally, I would make the class constructor without parameters private, to avoid another form of class initialization other than this:

public class ApoQueueVM
{
    private ApoQueueVM() { }

    public ApoQueueVM(ApoQueue apoQueue, ApoFileBL apoFileBL)
    {
        this.EnableExport = apoQueue.Status == ApoQueueStatus.Gerado.ToString() || apoQueue.Status == ApoQueueStatus.Enviado.ToString();
        this.KeyFigure = apoQueue.KeyFigure;
        this.PathFileGenerated = apoFileBL.GetFullPathApo(apoQueue, true);
    }

    public string KeyFigure { get; set; }
    public bool HaveInconsistencies { get; set; }
    public string PathFileGenerated { get; set; }
    public bool EnableExport { get; set; }
}

3

I agree with Gypsy that the approach looks good, it just wouldn’t do the private default constructor since every time you create a constructor with parameters, the empty default constructor is disabled by the compiler and cannot be called. It would still be possible to create a constructor without parameters if you wanted to, but it does not seem to be the case. If you didn’t have the constructor with parameters, and you didn’t want there to be a construction (rare), then the private constructor would be useful.

This can be proven in the codes below:

public class Program {
    public static void Main() {
        var vmc = new ApoQueueVMConstrutor(); //construiu sem inicializar nada, mesmo sem ter sido declarado, o construtor está lá
        var vm1 = new ApoQueueVM("xxx", "yyy"); //construiu conforme o esperado
//      var vm2 = new ApoQueueVM(); //não existe construtor com esta assinatura
    }
}
 
public class ApoQueueVM {
    public ApoQueueVM(string apoQueue, string apoFileBL) {
        this.EnableExport = true;
        this.KeyFigure = apoQueue;
        this.PathFileGenerated = apoFileBL;
    }
 
    public string KeyFigure { get; set; }
    public bool HaveInconsistencies { get; set; }
    public string PathFileGenerated { get; set; }
    public bool EnableExport { get; set; }
}
 
public class ApoQueueVMConstrutor {
    public string KeyFigure { get; set; }
    public bool HaveInconsistencies { get; set; }
    public string PathFileGenerated { get; set; }
    public bool EnableExport { get; set; }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • 1

    For the specific case of Viewmodels, there is no construction anywhere else of the application that is not necessarily the Action of Controller of it, then the concern is not necessary. Now, if we were talking about Models and even Controllers, then I agree with you.

Browser other questions tagged

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