Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cutting down repeating code in c# Class

Tags:

c#

class

wrapper

This is a wrapper for an API I'm working on, am I doing it sort of right? I'm not particularly fond of all the repeating code in the constructor, if someone can show me if I can reduce that it would be very helpful!

public class WebWizForumVersion
{
    // Properties of returned data
    public string Software { get; private set; }
    public string Version { get; private set; }
    public string APIVersion { get; private set; }
    public string Copyright { get; private set; }
    public string BoardName { get; private set; }
    public string URL { get; private set; }
    public string Email { get; private set; }
    public string Database { get; private set; }
    public string InstallationID { get; private set; }
    public bool NewsPad { get; private set; }
    public string NewsPadURL { get; private set; }

    public WebWizForumVersion(XmlReader Data)
    {
        try
        {
            Data.ReadToFollowing("Software");
            this.Software = Data.ReadElementContentAsString();
            Data.ReadToFollowing("Version");
            this.Version = Data.ReadElementContentAsString();
            Data.ReadToFollowing("ApiVersion");
            this.APIVersion = Data.ReadElementContentAsString();
            Data.ReadToFollowing("Copyright");
            this.Copyright = Data.ReadElementContentAsString();
            Data.ReadToFollowing("BoardName");
            this.BoardName = Data.ReadElementContentAsString();
            Data.ReadToFollowing("URL");
            this.URL = Data.ReadElementContentAsString();
            Data.ReadToFollowing("Email");
            this.Email = Data.ReadElementContentAsString();
            Data.ReadToFollowing("Database");
            this.Database = Data.ReadElementContentAsString();
            Data.ReadToFollowing("InstallID");
            this.InstallationID = Data.ReadElementContentAsString();
            Data.ReadToFollowing("NewsPad");
            this.NewsPad = bool.Parse(Data.ReadElementContentAsString());
            Data.ReadToFollowing("NewsPadURL");
            this.NewsPadURL = Data.ReadElementContentAsString();
        }
        catch (Exception e)
        {

        }
    }
}
like image 348
Tom Gullen Avatar asked Sep 13 '26 18:09

Tom Gullen


2 Answers

var properties = new [] {
    new {Name = "Software", Setter = new Action<string>(value => this.Software = value)},
    new {Name = "Version", Setter = new Action<string>(value => this.Version= value)},
    new {Name = "ApiVersion", Setter = new Action<string>(value => this.ApiVersion = value)},
    // ...
    new {Name = "NewsPad", Setter = new Action<string>(value => this.NewsPad = bool.Parse(value))},
}

foreach (var property in properties)
{
    Data.ReadToFollowing(property.Name);
    property.Setter(Data.ReadElementContentAsString());
}
like image 173
Snowbear Avatar answered Sep 16 '26 06:09

Snowbear


I'd leave the assignment of your local properties alone, and use helper methods to read the values from the XML.

public class WebWizForumVersion
{
    public WebWizForumVersion(XmlReader Data)
    {
        this.Software = Data.ReadString("Software");
        this.Version = Data.ReadString("Version");
        this.APIVersion = Data.ReadString("ApiVersion");
        this.NewsPad = Data.ReadBool("NewsPad");
    }
}

public static class XmlReaderHelpers
{
    private string ReadString(this XmlReader Data, string name)
    {
        Data.ReadToFollowing(name);
        return Data.ReadElementContentAsString();
    }

    private bool ReadBool(this XmlReader Data, string name)
    {
        return bool.Parse(Data.ReadString(name));
    }
}
like image 40
David Yaw Avatar answered Sep 16 '26 07:09

David Yaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!