Generic template, that's why I'm trying to simplify it to reduce duplication

All this, if the conditions in the method below have a similar structure, Any ideas to come up with a general method to reduce duplication in this method?

    public override Func<JObject, dynamic, string> version => (jobject, parameters) =>
    {
        bool hasValidObject = false;

        if (jobject["Version1"] != null)
        {
            _radio.Version1 = new VersionInfo(jobject["Version1"].Value<string>());
            hasValidObject = true;
        }

        if (jobject["Version2"] != null)
        {
            _radio.Version2 = new VersionInfo(jobject["Version2"].Value<string>());
            hasValidObject = true;
        }

        if (jobject["Version3"] != null)
        {
            _radio.Version3 = new VersionInfo(jobject["Version3"].Value<string>());
            hasValidObject = true;
        }

        if (jobject["Version4"] != null)
        {
            _radio.Version4 = new VersionInfo(jobject["Version4"].Value<string>());
            hasValidObject = true;
        }

        if (jobject["Version6"] != null)
        {
            _radio.Version6 = new VersionInfo(jobject["Version6"].Value<string>());
            hasValidObject = true;
        }

        if (hasValidObject)
        {
            return GenerateSuccess();
        }

        return GenerateUnsuccessful( try again.");
    };
+4
source share
3 answers

Another alternative could be with the operator switch:

public override Func<JObject, dynamic, string> version => (jobject, parameters) =>
{
    bool hasValidObject = false;

    foreach (char n in "12346")
    {
        var jObj = jobject["Version" + n];
        if (jObj != null)
        {
            var versionInfo = new VersionInfo(jObj.Value<string>());
            switch (n)
            {
                case '1': _radio.Version1 = versionInfo; break;
                case '2': _radio.Version2 = versionInfo; break;
                case '3': _radio.Version3 = versionInfo; break;
                case '4': _radio.Version4 = versionInfo; break;
                case '6': _radio.Version6 = versionInfo; break;
            }
            hasValidObject = true;
        }
    }

    return hasValidObject ? GenerateSuccess() : GenerateUnsuccessful(" try again.");
};

or a more advanced version with an array of delegates .. because I like the vertical space:] (also not verified):

public override Func<JObject, dynamic, string> version => (jobject, parameters) =>
{
    Func<VersionInfo, VersionInfo>[] a = { null, _radio.Version1 = v, 
                v => _radio.Version2 = v, v => _radio.Version3 = v,
                v => _radio.Version4 = v, null, v => _radio.Version6 = v };

    var q = from n in new[] { 1, 2, 3, 4, 6 }
            let j = jobject["Version" + n] where j != null
            select a[n](new VersionInfo(j.Value<string>()));

    return q.Count() > 0 ? GenerateSuccess() : GenerateUnsuccessful(" try again.");
};
+1
source

One way is to use reflection and loop

public override Func version => (jobject, parameters) => 
{ 
    bool hasValidObject = false;
    for (int i = 1; i<7;i++) 
    {
        hasValidObject = this.SetVersionInfo(i) || hasValidObject;
    }

    if (hasValidObject)
    {
        return GenerateSuccess();
    }

    return GenerateUnsuccessful( "try again.");
};


private bool SetVersionInfo(int i)
{
    if (jobject["Version" + i] == null) return false;

    _radio.GetType().GetProperty(propName)
        .SetValue(_radio, new VersionInfo(jobject["Version" + i].Value<string>()));
    return true;
}

Another way would be to create Dictionary<int,VersionInfo> Versionsin your class _radioand then you don't need to reflect:

private bool SetVersionInfo(int i)
{
    if (jobject["Version" + i] == null) return false;

    _radio.Versions[i] = new VersionInfo(jobject["Version" + i].Value<string>());
    return true;
}
+2
source

jobject ,

foreach(string version in jobject.keys)
{
     if(jobject[version]!=null){
     //do something
     }
}

the problem is with the radio, but if this is your class, you can use an array of versions in the class, and not so much, and access them using a counter that you will use to calculate the loop number

0
source

Source: https://habr.com/ru/post/1672959/


All Articles