public class UserService : ICrudService{ private readonly MyDbContext _context; public UserService(MyDbContext context) { _context = context; } public async Task GetByIdAsync(int id) { return await _context.Users.FindAsync(id); } }
public class ProductService : ICrudServiceThe package library for ICrudService and GetByIdAsync would depend on the implementation. In the first example, it would likely be Microsoft.EntityFrameworkCore, while in the second example it could be Microsoft.Net.Http.{ private readonly HttpClient _client; public ProductService(HttpClient client) { _client = client; } public async Task GetByIdAsync(int id) { var response = await _client.GetAsync($"api/products/{id}"); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject (json); } else { throw new Exception($"API returned {(int)response.StatusCode} status code."); } } }