Reducing Duplicate Code in a C # Class

This is the wrapper for the API I'm working on, am I doing it right? I don’t particularly like all the repeating code in the constructor, if someone can show me if I can reduce that it will be very useful!

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)
        {

        }
    }
}
+3
source share
5 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());
}
+4
source

I would leave the assignment of your local properties alone and use helper methods to read values ​​from 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));
    }
}
+4
source

? , - :

public Dictionary<string, string> ForumVars = null;

public WebWizForumVersion(XmlReader Data)
{
    ForumVars = new Dictionary<string, string>();
    ForumVars.Add("Software", GetValue("Software"));
    ForumVars.Add("Version", GetValue("Version"));
    ForumVars.Add("APIVersion", GetValue("APIVersion"));
}

protected string GetValue(string key)
{
    Data.ReadToFollowing(key);
    return Data.ReadElementContentAsString();
}

, (, NewsPad), Object.

+1

, .

new Dictionary<string, string>() {
    {"key", "val"},
    ...
}
0

IF InstallationID InstallID , refelction:

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 InstallID { get; private set; }  // changed property name
    public bool NewsPad { get; private set; }
    public string NewsPadURL { get; private set; }

    public WebWizForumVersion( XmlReader Data )
    {
        try
        {
            PropertyInfo[] props = this.GetType().GetProperties();

            foreach( PropertyInfo pi in props )
            {
                Data.ReadToFollowing( pi.Name );
                if( pi.PropertyType == typeof( bool ) )
                {
                    pi.SetValue( this, bool.Parse( Data.ReadElementContentAsString() ), null );
                }
                else
                {
                    pi.SetValue( this, Data.ReadElementContentAsString(), null );
                }
            }
        }
        catch( Exception e )
        {
          // do something with exception
        }
    }
}
0

All Articles