3
Is there a shortcut in Visual Studio, which creates a constructor, with all attributes created in the class?
class Alimento
{
public int ID { get; set; }
public string Titulo { get; set; }
public string SubTitulo { get; set; }
public string Conteudo { get; set; }
public string Filtro { get; set; }
public DateTime DataCriacao { get; set; }
public string NomeImagem { get; set; }
public string DicasCuriosidades { get; set; }
public string Umidade { get; set; }
public string Energia_kcal { get; set; }
public string Proteina_g { get; set; }
public string Lipideos_g { get; set; }
public string Saturados_g { get; set; }
public string Mono_insaturados_g { get; set; }
public string Poli_insaturados_g { get; set; }
public string Colesterol_mg { get; set; }
public string Carbo_idrato_g { get; set; }
public string fibra_alimentar_g { get; set; }
public string Calcio_mg { get; set; }
public string Magnesio_mg { get; set; }
public string Manganes_mg { get; set; }
public string Fosforo_mg { get; set; }
public string Ferro_mg { get; set; }
public string Sodio_mg { get; set; }
public string Potassio_mg { get; set; }
public string Zinco_mg { get; set; }
public string Retinol_mcg { get; set; }
public string Re_mcg { get; set; }
public string Rae_mcg { get; set; }
public string Tiamina_mg { get; set; }
public string Riboflavina_mg { get; set; }
public string Piridoxina_mg { get; set; }
public string Niacina_mg { get; set; }
public string Vitamina_c { get; set; }
public Alimento(int id, string titulo, ............)
{
ID = id;
Titulo = titulo;
............
}
I am new in the development aria, as passed the parameters to the builder? following the criterion of good practices?
– Eduardo F. Santos
It doesn’t happen unless it’s really needed in the builder. You can’t answer why there is a constructor here, but you don’t necessarily have to initialize all the members by the constructor. There are some cases where this is really necessary. Briefly you only do this when you need the initialization done before the object is available. This is not usually a real need in most cases. It is best to initialize members by their properties as shown above. This is even easier in C# 6. Somehow I improved the answer.
– Maniero
"The result will be essentially the same if there is no competition." - Even if there is concurrence, the result will be the same. When using a Object initializer, the object is only published after the setters are executed. (By publishing of an object means the assignment of the object to the variable
alimento
)– dcastro
@dcastro is true, for the competition doesn’t really make a difference since the Object initializer was created, although technically the creation is made differently than it would be with the builder. In this case there would only be problems if it really needed to do some operation in the construction. His example doesn’t seem to make much sense to have a builder, except he has some unspecified requirement. In theory not even the 2 parameters, which I created in the example just to show that it is possible to merge, would be necessary.
– Maniero
I agree, it makes no sense to create a constructor. In fact, the class itself with so many properties is soon a code Smell...
– dcastro
Got it personal. However the constructor is being used yes, this object is instantiated in the repository layer, which connects to the database, and initializes with all parameters described here....
– Eduardo F. Santos
@Santos needs to see exactly how this is being done, I am not going to continue on this because it is already another subject that does not fit here in the question, but you have had good information here to at least think about design of this class.
– Maniero