danesparza / MailChimp.NET

:envelope: .NET Wrapper for the MailChimp v2.0 API
MIT License
179 stars 119 forks source link

Please create default instances for objects #78

Open skironDotNet opened 10 years ago

skironDotNet commented 10 years ago

Scenario: I create CampaignSegmentOptions

CampaignSegmentOptions x = new CampaignSegmentOptions(); x.Conditions.Add(some obj); //here I get null object error because Conditions is null

Here is the solution: a public constructor (I will add to my fork)

/// <summary>
/// A list of criteria on which to segment a list
/// </summary>
[DataContract]
public class CampaignSegmentOptions
{
    public CampaignSegmentOptions()
    {
        Conditions = new List<CampaignSegmentCriteria>();
    }

    /// <summary>
    /// Controls whether to use AND or OR when applying your options - expects "any" (for OR) or "all" (for AND)
    /// </summary>
    [DataMember(Name = "match")]
    public string Match { get; set; }
    /// <summary>
    /// Collection of up to 5 structs for different criteria to apply while segmenting. 
    /// Each criteria row must contain 3 keys - "field", "op", and "value" - and possibly a fourth, "extra", based on these definitions: http://apidocs.mailchimp.com/api/2.0/campaigns/segment-test.php
    /// </summary>
    [DataMember(Name = "conditions")]
    public List<CampaignSegmentCriteria> Conditions { get; set; }

}
skironDotNet commented 10 years ago

P.S. Would be nice to go over all object and create constructors with default instances, if of course there is no reason not to do so :)

mdissel commented 10 years ago

One reason to not do this is, if you convert the object back to json this will result into a empty array instead of null value (resulting in leaving the property out in the result json).

skironDotNet commented 10 years ago

so you talking optimization of the resulting JSON string, usually the focus is on optimization of hours spent on development and decreasing bug possibility, anyhow, I don't care, I wish to be removed from this topic