1

I'm working on a ASP.NET application that is required to communicate with a Flask web service.

My initial thought was to include the communication with web service directly into the aspx file, similiar to Scenario 1. However, I thought that Scenario 2 could be a cleaner solution as I can hide the properties such as url and details of the implementation within an additional class.

My question is, by introducing another class to handle the direct communication with the web service, and then using that object within the web form, am I over complicating it or is it a feasible design decision?

Scenario 1:

private sealed string url = "someUrl";

public partial class AspWebPage 
{
    private string GetAllData() 
    { 
        string jsonResponse = "";
        using(var client = new WebClient())
        {
            // some code to get data from web service
        }
        return jsonResponse;
    }
}

Scenario 2:

public class MiddleManClass
{
    private sealed string url = "someUrl";

    public string GetAllData(string key) { // code to get data }

    public string SubmitData(SomeType data) { // code to submit data }

}

public partial class AspWebPage 
{

    protected void Page_Load(object sender, EventArgs e)
    {
       ...
       var data = middleManClass.GetAllData(someKey);
       ...
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ...
        middleManClass.submitData(someData);
        ...
    }

}
1
  • It looks like a reasonable way of implementing it to me.
    – Zalomon
    May 12, 2016 at 13:13

1 Answer 1

2

Well,

Think about it this way : "What will happen if I need to use my WS somewhere else ?"

From your first implementation I can already tell that you may have to do some copy/paste as time goes on, and you don't want that (DRY Principle). Another advantage to separate the communication is testing and updating. Working with partners is never an easy task and you may encounter different problems. It'd be nice to have only one place to modify the code and have the correction applied everywhere.

And about testing, if you declare an interface (OOP way of speaking) during testing you can change the implementation this way you can test the code that call your service and not the service itself.

So 2nd implementation for the win.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.