I have observed in lot many C# example where in following pattern is being followed but i am not sure how this will going to help us in long run
Typical approach i have seen is
- create a interface
- implement interface
- create a manager
- call manager
it would be really nice if anyone can tell me how this approach will going to help in real world senario
interface IRestService
for various web requests
public interface IRestService { Task<List<TodoItem>> RefreshDataAsync (); Task SaveTodoItemAsync (TodoItem item, bool isNewItem); Task DeleteTodoItemAsync (string id); }
Code that implements IRestService
interface
public class RestService : IRestService { HttpClient client; public List<TodoItem> Items { get; private set; } public RestService () { var authData = string.Format("{0}:{1}", Constants.Username, Constants.Password); var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData)); client = new HttpClient (); client.MaxResponseContentBufferSize = 256000; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue); } public async Task<List<TodoItem>> RefreshDataAsync () { } public async Task SaveTodoItemAsync (TodoItem item, bool isNewItem = false) { } public async Task DeleteTodoItemAsync (string id) { } }
Manager
that calls the implemented methods
public class TodoItemManager { IRestService restService; public TodoItemManager (IRestService service) { restService = service; } public Task<List<TodoItem>> GetTasksAsync () { return restService.RefreshDataAsync (); } public Task SaveTaskAsync (TodoItem item, bool isNewItem = false) { return restService.SaveTodoItemAsync (item, isNewItem); } public Task DeleteTaskAsync (TodoItem item) { return restService.DeleteTodoItemAsync (item.ID); } }
to execute request
TodoManager = new TodoItemManager (new RestService ()); TodoManager.GetTasksAsync ();
few questions are running in my mind
- why we need a manager why can not we just use
RestService
- if some day i need to develop one module to fetch contact related data from server then do i need to add methods in
IRestServie
toaddContact()
,deleteContact()
,getContact()