Where to put the View Model rule

2

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

I'm not sure where to put the button rule to be enabled or not.

I thought of this implementation, I wonder if it's the best way. Another idea I had was to put the builder content in a Business class.

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; }
}
    
asked by anonymous 05.12.2016 / 16:19

2 answers

2

It seems to be a good approach in the constructor.

In addition, I would make the constructor of the class without parameters be private, to avoid another form of initialization of the class 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; }
}
    
05.12.2016 / 17:42
2

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

This can be proven in this dotNetFiddle code .

    
05.12.2016 / 17:59