Doubt about modularization in MVC Application

2

Friends,

I always try to focus on good programming practices, and one practice that confuses me most is Modularization . For example, I have the method below that receives data from an API and sends View to this collection. Well: in this method some different functions are performed, and the need to modularize is clear. However, I'm confused about how each part should be modularized:

//String da API que eu conecto.
string baseUrl = "http://endereco.com.br/api/";

public async Task<ActionResult> Index()
    {
        List<employee> employee = new List<employee>();

        //Cria um HTTPClient e atribui determinados parâmetros para a conexão
        using (HttpClient client = new HttpClient())
        {
            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage message = await client.GetAsync("employee");
            if (message.IsSuccessStatusCode)
            {
                //Se tudo der certo, ele vai atribuir o resulto à "response" e desserializar para JSON.
                var response = message.Content.ReadAsStringAsync().Result;
                employee = JsonConvert.DeserializeObject<List<employee>>(response);
            }
            return View(employee);
        }
    }

I thought, for example, of creating a specific method for creating and assigning HttpClient. But it could not stay in this controller since it is being used to access the pages.

From the experience of friends, how should we split the code of this method?

    
asked by anonymous 02.03.2018 / 14:52

1 answer

3

In good practices it would be best if this part is inside a service, and it is generically shaped.

public async Task<List<T>> ListaAsync(){
using (HttpClient client = new HttpClient())
        {
            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage message = await client.GetAsync(typeof(T).Name);
            if (message.IsSuccessStatusCode)
            {
                //Se tudo der certo, ele vai atribuir o resulto à "response" e desserializar para JSON.
                var response = message.Content.ReadAsStringAsync().Result;
                return = JsonConvert.DeserializeObject<List<T>>(response);
            } else {
           return T = new List<T>();
           }
        }
}

I do not know how you put this baseUrl, but in good practices the best would be inside Web.config , because in case you need to change the url you can make direct on the hosting site.

 <appSettings>
    <add key="baseUrl" value="http://endereco.com.br/api/"/>
  </appSettings>

C #

string baseUrl = ConfigurationManager.AppSettings["baseUrl"]
    
05.03.2018 / 08:23