I am currently working on a website and I had a good separation of problems by repository template with repositories and managers. Now I'm trying to implement a web API, as I would greatly benefit from it in the future, being able to use it from different clients. Since I'm fairly new to REST services, I am having problems with the correct procedure to use my web API from the Service in my MVC4 application, then to use this service in my MVC controllers. I do not want to use knockout for every API call.
My web interfaces look something like this:
public class UserController : ApiController { private readonly IUserManager _manager; public UserController(IUserManager manager) { this._manager = manager; } // GET api/user public IEnumerable<User> Get() { return _manager.GetAll(); } // GET api/user/5 public User Get(int id) { return _manager.GetById(id); } // POST api/user public void Post(User user) { _manager.Add(user); } // PUT api/user/5 public void Put(User user) { _manager.Update(user); } // DELETE api/user/5 public void Delete(User user) { _manager.Delete(user); } }
I would like to create a service to use my web API as such:
public class UserService : IUserService { ....Implement something to get,post,put,and delete using the api. }
so I can use it in my mvc controller:
public class UserController: Controller { private readonly IUserService _userService; public UserController(IUserService userService) { this._userService = userService; }
I know that this is possible because I saw this in some workplaces, but it is very difficult to find articles about it, I only found articles explaining how to use the web API via knockout. Any help or advice would be appreciated.
source share