vendredi 29 avril 2016

Class design for loading data from HTML page

I need to make integration with ancient ERP system that doesn't provide any kind of API. The only way to programmatically access its data is parsing the system's backend (thankfully it's web) via Http Post/Get.

The design of my classes looks as follows (C#):

// Represents product in ERP.
public class Product 
{
    public int ID {get;set;}
    public string Name {get;set;}
    public double {get;set;}
}

// Provides CRUD and Get operations on Product.
public class HttpRepository : IRepository 
{
    public Product Get(int id) {
        var html = WebClient.HttpGet("http://ift.tt/1Wv7BnV"+ id);
        return Parser.ParseHtmlAndGetProduct(html);
    }
}

// Parses html pages and returns useful data out of those.
public class Parser 
{
    public static Product ParseHtmlAndGetProduct(string html) {
        var p = new Product();

        // use Regex for getting product properties
        p.ID = GetProductID(html);
        p.Title = GetTitle(html);
        p.Price = GetPrice(html);

       return p;
    }
}

Is that correct design?

I feel like the Parser shouldn't create and return Product class since Repository is the only place responsible for producing products. With the other hand Repository cannot do parsing directly since it takes too much responsibility and violates SOLID.

So what would be correct class design for this scenario?

Aucun commentaire:

Enregistrer un commentaire