HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

API wrapper for translating XML requests and responses into objects

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
translatingobjectsintoxmlresponseswrapperforrequestsandapi

Problem

I'm looking for remarks on pretty much anything. This has been my first project that's meant for use by others and I'd like to have it as clean as possible. I will post the main source code here but because of the size you might prefer reading it on Github instead.

The code should be adequately documented so I don't think a lot of explanation is needed.
The project itself is a wrapper for an API to translate XML requests and responses into simple C# objects.

Any advice is welcome!

TVDB

namespace TVDBSharp {
    /// 
    /// The main class which will handle all user interaction.
    /// 
    public class TVDB {
        private readonly IDataProvider _dataProvider;

        /// 
        /// Creates a new instance with the provided API key and dataProvider.
        /// 
        /// The API key provided by TVDB.
        /// Specify your own  instance.
        public TVDB(string apiKey, IDataProvider dataProvider) {
            _dataProvider = dataProvider;
            _dataProvider.ApiKey = apiKey;
        }

        /// 
        /// Creates a new instance with the provided API key and standard .
        /// 
        /// The API key provided by TVDB.
        public TVDB(string apiKey) {
            _dataProvider = new DataProvider { ApiKey = apiKey };
        }

        /// 
        /// Search for a show in the database.
        /// 
        /// Query that identifies the show.
        /// Maximal amount of results in the returning set. Default is 5.
        /// Returns a list of shows.
        public List Search(string query, int results = 5) {
            return new Builder(_dataProvider).Search(query, results);
        }

        /// 
        /// Get a specific show based on its ID.
        /// 
        /// ID of the show.
        /// Returns the corresponding show.
        public Show GetShow(string showId) {
            return new Builder(_dataProvider).BuildShow(showId);
        }
    }
}


Models.DAO.IDataProvider

```
namespace TVDBSharp.Models.DAO {

Solution

Some minor stuff for now:

-
Any specific reason to use StringBuilder rather than string.Format()? I personally find

string.Format("http://thetvdb.com/api/{0}/series/{1}/all/", ApiKey, showID)


easier to read than

new StringBuilder("http://thetvdb.com/api/").Append(ApiKey).Append("/series/").Append(showID).Append("/all/").ToString()


The structure of the resulting string is much easier to see with string.Format (and it's also less code).

-
You do something like this fairly often:

string.IsNullOrWhiteSpace(doc.GetSeriesData("Runtime"))
        ? (int?) null
        : Convert.ToInt32(doc.GetSeriesData("Runtime"));


You could look at encapsulating this in an extension method like this

T GetSeriesData(this XDocument doc, string key, Func converter);

Code Snippets

string.Format("http://thetvdb.com/api/{0}/series/{1}/all/", ApiKey, showID)
new StringBuilder("http://thetvdb.com/api/").Append(ApiKey).Append("/series/").Append(showID).Append("/all/").ToString()
string.IsNullOrWhiteSpace(doc.GetSeriesData("Runtime"))
        ? (int?) null
        : Convert.ToInt32(doc.GetSeriesData("Runtime"));
T GetSeriesData(this XDocument doc, string key, Func<string, T> converter);

Context

StackExchange Code Review Q#31468, answer score: 6

Revisions (0)

No revisions yet.