Instantiating a property when the required attributes have been set I have a simple class which holds my product. Someone can add a serial to this product and also a purchase id. On change of the properties, I want to check whether both properties are set, and get the license class and add it to the product.

public class License
{
    public bool IsTrial { get; set; }

    public bool IsValid { get; set; }

    public string Type { get; set; }

    public License()
    {

    }

    public License(string purchaseId, string serial)
    {
        this.IsTrial = true;
        this.IsValid = true;
        this.Type = "Enterprise";
    }


Example License class

public class Product
{
    private License _license;
    public License license
    {
        get
        {
            return _license;
        }
        set
        {
            _license = value;
        }
    }

    public string Name { get; set; }

    private string _purchaseId;
    public string PurchaseId {
        get
        {
            return _purchaseId;
        }
        set
        {
            _purchaseId = value;
            GetLicense();
        }
    }

    private string _serial;
    public string Serial
    {
        get
        {
            return _serial;
        }
        set
        {
            _serial = value;
            GetLicense();
        }
    }

    private void GetLicense()
    {
        if (!string.IsNullOrEmpty(this.Serial) &amp;&amp; !string.IsNullOrEmpty(this.PurchaseId))
        { 
            License lice = new License(serial, PurchaseId);
            this.License = lice;
        }
    }


That's the approach I used. Is this a correct way to do it? How is this called and what is the best practice? I need to use .NET Framework 3.5.
Getters &amp; Setters + Validation

I think it's better to validate the serial and the purchase-id in their setters instead in the GetLicense method that actually could be a readonly-property.


private void GetLicense()
{
    if (!string.IsNullOrEmpty(this.Serial) &amp;&amp; !string.IsNullOrEmpty(this.PurchaseId))
    { 
        License lice = new License(serial, PurchaseId);
        this.License = lice;
    }
}



After this tiny refactorings the result is:

public class Product
{
    // it's prettier if you group private fields togehter at the top
    private string _purchaseId;
    private string _serial;   

    public string Name { get; set; }

    public string PurchaseId
    {
        get { return _purchaseId; }
        set
        {
            if (string.IsNullOrEmpty(value)) 
                throw new ArgumentNullException(nameof(PurchaseId));
            _purchaseId = value;
        }
    }


    public string Serial
    {
        get { return _serial; }
        set
        {
            if (string.IsNullOrEmpty(value)) 
                throw new ArgumentNullException(nameof(Serial));
            _serial = value;
        }
    }   

    public License License
    {
        get { new License(Serial, PurchaseId); }
    }
}




Magic Strings


  this.Type = "Enterprise";


You should create a const/readonly string for the "Enterprise" and name it for example DefaultLicenseType



Bug

There is a bug. The license constructor expects purchaseId + serial and you create a license with a serial + purchase id

Constructor signature:

public License(string purchaseId, string serial)


License creation:

new License(serial, PurchaseId);