Creating the interface and the implementation for the PostService
Create a new interface called IPostService.cs into the Services folder in the MvvmCrossDemo.Core project. Add two methods to it, as shown below:
1
namespace MvvmCrossDemo.Core.Services
2
{
3
public interface IPostService
4
{
5
Task<ResponseMessage<List<Post>>> GetPostList();
6
Task<ResponseMessage<Post>> GetPost(int id);
7
}
8
}
Copied!
Create a new implementation class called PostService.cs in the same folder like this:
1
using System;
2
using MvvmCrossDemo.Core.Infrastructure.Extensions;
3
using MvvmCrossDemo.Core.Models;
4
using System.Collections.Generic;
5
using System.Net.Http;
6
using System.Threading.Tasks;
7
8
namespace MvvmCrossDemo.Core.Services
9
{
10
public class PostService : IPostService
11
{
12
private readonly HttpClient _httpClient;
13
private string apiUrl = "https://jsonplaceholder.typicode.com/";
14
15
public PostService()
16
{
17
_httpClient = new HttpClient();
18
}
19
20
public async Task<ResponseMessage<List<Post>>> GetPostList()
21
{
22
try
23
{
24
var response = await _httpClient.GetAsync(quot;{apiUrl}posts");
25
if (response.StatusCode == System.Net.HttpStatusCode.OK)
26
{
27
var result = await response.ReadAsJsonAsync<List<Post>>();
28
return new ResponseMessage<List<Post>>
29
{
30
IsSuccess = true,
31
Result = result
32
};
33
}
34
else
35
{
36
return new ResponseMessage<List<Post>>
37
{
38
IsSuccess = false,
39
// Show the detailed error message here according to the response.
40
Message = "Errors"
41
};
42
}
43
}
44
catch (Exception e)
45
{
46
// TODO: Log the exception here.
47
return new ResponseMessage<List<Post>>
48
{
49
IsSuccess = false,
50
// Show the detailed error message here.
51
Message = "Errors"
52
};
53
}
54
55
}
56
public async Task<ResponseMessage<Post>> GetPost(int id)
57
{
58
try
59
{
60
var response = await _httpClient.GetAsync(quot;{apiUrl}posts/{id}");
61
if (response.StatusCode == System.Net.HttpStatusCode.OK)
62
{
63
var result = await response.ReadAsJsonAsync<Post>();
64
return new ResponseMessage<Post>
65
{
66
IsSuccess = true,
67
Result = result
68
};
69
}
70
else
71
{
72
return new ResponseMessage<Post>
73
{
74
IsSuccess = false,
75
// Show the detailed error message here according to the response.
76
Message = "Errors"
77
};
78
}
79
}
80
catch (Exception e)
81
{
82
// TODO: Log the exception here.
83
return new ResponseMessage<Post>
84
{
85
IsSuccess = false,
86
// Show the detailed error message here.
87
Message = "Errors"
88
};
89
}
90
}
91
}
92
}
Copied!
In the PostService class, I tried to handle all exceptions of the HTTP requests to avoid passing any unexpected result to the caller method. It is helpful to improve the stability of the App. We also need to use a logger to record the error message if you are developing a real App.
We do not need to worry about how to register the implement instance because MvvmCross will do everything by the code we mentioned before in the App.cs, as long as we follow the name convention of the services:
1
CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
Copied!
Last modified 3yr ago
Copy link