Use Pattern View Model or a POST method with Json?

Asked

Viewed 76 times

2

I wanted a tip from you who are experienced me. I am developing an application where I will use properties of 2 models, which wanted the best option to be used this case and if possible please list which are the advantages.

1 answer

2


Viewmodels

I’ve given a lot of answers about that, but I didn’t mention the advantages of them, so I’m going to talk about some.

1. Variable Exposure Control

The approach is often used when it is not desirable to expose the variables of a Model directly mapped into database. The Viewmodel acts as a data container that is handled by Controller, which can avoid some problems, such as the insertion of improper data in a database.

2. Organisation of Forms

In several cases it is desirable that a form has information that is common or divisible between two or more Models. The Viewmodel, besides rationalizing the form, it also hides the internal structure of information.

3. Assembly of Search Results Pages

This is one of my favorites. Imagine a search screen with filters (resubmitted by a form) and their respective results.

You can use a form that accepts the verb GET and accommodate in a Viewmodel both the search parameters used and the results of this search, in the form of a collection or list. For example:

namespace MeuProjeto.ViewModels
{
    public class ProcessoViewModel
    {
        [DisplayName("Formato de Saída dos Dados")]
        public FormatoSaidaDados SaidaDados { get; set; }

        [Column("DATA_ENTRADA")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [DisplayName("Data de Entrada Inicial")]
        public DateTime? DataEntradaInicial { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [DisplayName("Data de Entrada Final")]
        public DateTime? DataEntradaFinal { get; set; }
        [Column("DT_DISTRIB")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [DisplayName("Data de Distribuição Inicial")]
        public DateTime? DataDistribuicaoInicial { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [DisplayName("Data de Distribuição Final")]
        public DateTime? DataDistribuicaoFinal { get; set; }
        [Column("DATA_ENCERRAMENTO")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [DisplayName("Data de Encerramento Inicial")]
        public DateTime? DataEncerramentoInicial { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [DisplayName("Data de Encerramento Final")]
        public DateTime? DataEncerramentoFinal { get; set; }

        [Column("PASTA")]
        [DisplayName("Caso Inicial")]
        public int? PastaInicial { get; set; }
        [DisplayName("Caso Final")]
        public int? PastaFinal { get; set; }
        [Column("COD_CLIENTE")]
        [DisplayName("Cliente Inicial")]
        public int? ClienteIdInicial { get; set; }
        [DisplayName("Cliente Final")]
        public int? ClienteIdFinal { get; set; }

        [Column("COD_REGIONAL")]
        [DisplayName("Código da Regional")]
        public String RegionalId { get; set; }
        [Column("COD_REGIONAL")]
        [DisplayName("Regionais")]
        public String[] RegionalIdMultiple { get; set; }

        public virtual IEnumerable<Models.Processo> Resultados { get; set; }
    }
}

4. Serialization and Deserialization of JSON

Here is possibly where the goal of your question best fits. Serialize in JSON using Viewmodel is even more advantageous than using only the Model for three reasons:

  • You can structure JSON using Viewmodel, composing the Models however you wish;
  • Does not have the circular reference problem of Models ASP.NET MVC;
  • Uses only a single command for serialization:

    return Json(meuViewModel, JsonRequestBehavior.AllowGet);
    

Browser other questions tagged

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