What do builders mean by this?

Asked

Viewed 210 times

6

This builder has a this, what it really means?

public class HelpController : Controller
{
    private const string ErrorViewName = "Error";

    public HelpController()
        : this(GlobalConfiguration.Configuration)
    {
    }

    public HelpController(HttpConfiguration config)
    {
        Configuration = config;
    }

    public HttpConfiguration Configuration { get; private set; }

    public ActionResult Index()
    {
        ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
        return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
    }

    public ActionResult Api(string apiId)
    {
        if (!String.IsNullOrEmpty(apiId))
        {
            HelpPageApiModel apiModel = Configuration.GetHelpPageApiModel(apiId);
            if (apiModel != null)
            {
                return View(apiModel);
            }
        }

        return View(ErrorViewName);
    }

    public ActionResult ResourceModel(string modelName)
    {
        if (!String.IsNullOrEmpty(modelName))
        {
            ModelDescriptionGenerator modelDescriptionGenerator = Configuration.GetModelDescriptionGenerator();
            ModelDescription modelDescription;
            if (modelDescriptionGenerator.GeneratedModels.TryGetValue(modelName, out modelDescription))
            {
                return View(modelDescription);
            }
        }

        return View(ErrorViewName);
    }
}

Look at the full class. Does he inherit from the builder below him? I have more or less understood what colleagues have said, but there is still a doubt: why this has it: GlobalConfiguration.Configuration. Ttalvez I’m in doubt in the nomenclature.

2 answers

5


You cannot call other overloaded constructors within a constructor, you can only indicate that it should be called, so the compiler put the call in the most suitable location.

In case it is calling the other constructor that has a parameter and already passing a default value:

public HelpController(HttpConfiguration config) => Configuration = config;

the parameter config will receive GlobalConfiguration.Configuration past in the other builder.

Not quite so, but this builder would be the same as writing this:

public HelpController() => this.HelpController(GlobalConfiguration.Configuration);

This is called an initializer. This is useful when something needs to be added to the behavior of a constructor that is already present in another constructor (overload of it)). In this example, the constructor was created to give a facility, to have a constructor who does not need to pass anything he chooses a default value.

If this is an enumeration could be used the value default for the parameter. Something like this:

public HelpController(HttpConfiguration config = GlobalConfiguration.Configuration);

I put in the Github for future reference.

Just to complement, not for this code, there are cases that can be used the : base in place of : this, then it will call the base class constructor to this. Example: public HelpController() : base(passa alguma coisa aqui). Note that whenever there is inheritance in the constructor class default of the base class is implicitly called, if your code does not spell out. So even if you are not seeing when it does public HelpController(), you’re actually doing public HelpController() : base().

I’ve used C# 7 syntax for one-line constructors.

What is the signature of a method?

  • Big, I made an issue and look at my question there, please.

  • That builder with the : this() is something new? I never knew it, I always created a private method called Inicializa() that I called in all builders (normally I use no more than 2 builders, but only did it to facilitate).

  • Now it became clearer (after the last edition), I understood why this, from this. Thank you and thank you.

  • 2

    @Alisson since version 1.0 :)

4

It is as if this constructor "inherits" another constructor of the same class:

Example:

    public PessoaRepositorio() : this("teste")
    {

    }
    public PessoaRepositorio(string a)
    {

    }
  • You’re not actually inheriting anything there. The fact that the syntax is similar doesn’t mean that it has any relation to inheritance.

Browser other questions tagged

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