Skip to content

Easy constructor for properties binding #107

@RenanStigliani

Description

@RenanStigliani

Proposition

An easy way to implement default constructor that only bind the parameter of class properties

Situation

When you have a class/struct and want to enforce the initialization through constructor we have to do something like this:

public class Access
{
    public string Name { get; set; }
    public bool IsActive { get; set; }


    public Access( // It's obvious that it's the constructos for Access class, is's inside it!
        string name, // Have to duplicate the name and type of the property
        bool isActive) // Again the name and type of the property
    {
        this.Name = name; // Obvious associations
        this.IsActive = isActive; // Obvious associations
    }
}

Why not write something like:

public class Access
{
    public string Name { get; set; }
    public bool IsActive { get; set; }

    public(Name, IsActive = true)
}

We could even use DataContracts and remove the necessity to validate the parameters, it would be very helpful for creating small helper classes.

public class Access
{
    public string Name { get; set; }
    public bool IsActive { get; set; }

    public([NotNull]Name, IsActive = true)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions