CSharp · Console
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
}
}
public class TestController : Controller
{
private ITestAPI api;
#warning If you want, implement and register an IHttpControllerActivator to inject the IAPI implementation and remove this constructor if you want to have DI all the way up your stack
public TestController() : this(new TestAPI(new UserDataService("http://10.8.200.1/test/"), new MenuService("http://10.8.200.1/menu/"), new UserItemsService("http://10.8.200.1/items/"))) {}
public TestController(ITestAPI api) { this.api = api; }
public ActionResult Index(string id)
{
return View(this.api.GetTestModel(id));
}
}
public interface ITestAPI
{
TestModel GetTestModel(string id);
}
public class TestAPI : ITestAPI
{
private IUserDataService userDataService;
private IMenuService menuService;
private IUserItemsService userItemsService;
public TestAPI(IUserDataService userDataService, IMenuService menuService, IUserItemsService userItemsService)
{
this.userDataService = userDataService;
this.menuService = menuService;
this.userItemsService = userItemsService;
}
public TestModel GetTestModel(string id)
{
var testModel = new TestModel();
testModel.UserData = this.userDataService.GetUserData(id);
testModel.MenuList = this.menuService.GetMenus(id);
testModel.UserItems = this.userItemsService.GetUserItems(id);
return testModel;
}
}
public interface IUserDataService { UserData GetUserData(string id); }
public interface IMenuService { List<Menu> GetMenus(string id); }
public interface IUserItemsService { List<UserItem> GetUserItems(string id); }
public abstract class BaseHttpServiceClient<TEntity, TPrimaryKey> where TEntity : class
{
private string remoteUri;
protected BaseHttpServiceClient(string remoteUri) { this.remoteUri = remoteUri; }
protected virtual TEntity GetRemoteItem(TPrimaryKey id)
{
TEntity testData = null;
try
{
using (HttpClient httpClient = new HttpClient())
{
string requestUri = this.remoteUri + id.ToString();
HttpRequestMessage testRequest = new HttpRequestMessage(HttpMethod.Get, requestUri);
HttpResponseMessage response = httpClient.SendAsync(testRequest).Result;
if (response.IsSuccessStatusCode)
{
var jsonData = response.Content.ReadAsStringAsync().Result;
testData = JsonConvert.DeserializeObject<TEntity>(jsonData);
}
}
}
catch (Exception ex)
{
}
return testData;
}
}
public class UserDataService : BaseHttpServiceClient<UserData, string>, IUserDataService
{
public UserDataService(string remoteUri) : base(remoteUri) {}
public UserData GetUserData(string id) { return this.GetRemoteItem(id); }
}
public class MenuService : BaseHttpServiceClient<List<Menu>, string>, IMenuService
{
public MenuService(string remoteUri) : base(remoteUri) {}
public List<Menu> GetMenus(string id) { return this.GetRemoteItem(id); }
}
public class UserItemsService : BaseHttpServiceClient<List<UserItem>, string>, IUserItemsService
{
public UserItemsService(string remoteUri) : base(remoteUri) {}
public List<UserItem> GetUserItems(string id) { return this.GetRemoteItem(id); }
}
public class TestModel
{
public string Name{get;set;}
public string PublisherId{get;set;}
public string AccountType{get;set;}
public UserData UserData {get;set;} //There will be a UserData model
public List<Menu> MenuList {get;set;} //There will be a Menu model
public List<UserItem> UserItems {get;set;} //There will be a UserItem model
}
public class UserData {}
public class Menu {}
public class UserItem {}
// Fills for external classes
public class Controller
{
public ActionResult View(object model) { throw new NotImplementedException(); }
}
public class ActionResult {}
public class HttpClient : IDisposable
{
public void Dispose() {}
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request) { throw new NotImplementedException(); }
}
public class HttpContent
{
public Task<String> ReadAsStringAsync() { throw new NotImplementedException(); }
}
public class HttpResponseMessage
{
public bool IsSuccessStatusCode { get { throw new NotImplementedException(); } }
public HttpContent Content { get { throw new NotImplementedException(); } }
}
public enum HttpMethod
{
Get,
Post
}
public class HttpRequestMessage
{
public HttpRequestMessage(HttpMethod method, string requestUri) {}
}
public class JsonConvert
{
public static T DeserializeObject<T>(string jsonData){ throw new NotImplementedException(); }
}