2
Well, in C# we put attributes as follows:
[Required("Este campo é obrigatório")]
public string Nome {get; set;}
What I would like to know is whether there is a way to add attributes dynamically to instantiated objects. Like the following hypothetical code:
var pessoa = new Pessoa();
pessoa.Nome.AdicionarAtributo(Required());
Context
The context is as follows: I am creating a Universal Windows project, using Prism and applying MVVM. But for each field of a model I am creating a field in Viewmodel, then in the Save() method, I create an instance of the model and assign the values of Viewmodel. The attributes, at the moment, are in the fields of Viewmodel, but I wanted to do something different. My Viewmodel would be basically this way:
public class ClienteViewModel: ValidatableBindableBase
{
private Cliente _modelo;
public Cliente Modelo
{
get => _modelo;
set => SetProperty(ref _modelo, value);
}
public ClienteViewModel()
{
Modelo.Campo.AdicionarAtributo(Required());
[...]
}
[...]
public void Salvar()
{
if(ValidateProperties()){
_serviço.Salvar(_modelo);
}
}
}
My goal is to decontaminate my Viewmodel, if anyone knows an even better way to do this, it will be very welcome.
Manual labor would not even be so much the problem, actually I worry more the code getting ugly and polluted haha. I did some research and saw something about putting an object between the model and Viewmodel, and in this object the properties and attributes I want, basically letting viewmodel take care only of the commands... do you think this is valid? Or is it just more work for little return?
– Raul Medeiros
I would need to see in detail, but Viewmodel should be super simple, only with the properties
– Maniero