Shortcut to create constructor with parameters

Asked

Viewed 8,139 times

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;            
        ............
    }

2 answers

5


As far as I know, natively it doesn’t exist, or at least it didn’t exist. I can’t say in Visual Studio 2015 that it has new code helpers. There may be some plugin that does this (this for example, I don’t know if it’s good and probably doesn’t exist anymore when you read this).

I think the Resharper does what you want with a Alt+Ins.

I know Visual Studio can generate the builder from its use, but it’s not what you want.

It is possible that someone will do something about it since the new compiler . NET Compiler Platform (old Roslyn) makes it very easy to parse code. There is even a Brazilian project where they are doing refactoring. If you find nothing, and do not know how to make one, ask the staff that participates in the Code-Cracker (abandoned project) make a code generator for you.

Anyway I already warn that it is not such a good practice to make a builder with so many parameters. Maybe what you should use is a code generator that creates a initializer of the object in its statement. The result will essentially be the same if there is no competition. Even if there is, there will hardly be any problems in this case. He would create something like this:

var alimento = new Alimento(identificador, titulo) {
    SubTitulo = subtitulo,
    Conteudo = conteudo,
    ...
    ...
}

It has other implications on how you’re doing, maybe you’re violating encapsulation by creating properties for all members, but I’m not going into this because it’s not within the scope of the question.

More information about constructor.

In C# 9 you can use the code generators to create this for you. Before I gave but they were more complicated.

  • I am new in the development aria, as passed the parameters to the builder? following the criterion of good practices?

  • 2

    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.

  • 2

    "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)

  • 2

    @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.

  • 2

    I agree, it makes no sense to create a constructor. In fact, the class itself with so many properties is soon a code Smell...

  • 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....

  • 1

    @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.

Show 2 more comments

1

Digital (ctor + TAB + TAB) is the shortcut to the default constructor.

Don’t forget to use private security using getters and setters.

shortcut to encapsulation (get/set) is: (Control + .)

Selecting the generate get/set and also pressing the (Control + .) it generates the constructor with the selected parameter.

Browser other questions tagged

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