If you look at the official Sample Contact Manager , you will find that the repository template is used to access the data layer. Also, keep in mind that there is also a DI through Ninject in this particular example.
In my case, I easily connected this to an existing EF model.
Here is an example repository implementation
///MODEL public class SampleRepository : ISampleRepository { public IQueryable<Users> GetAll() { SampleContext db = new SampleContext(); return db.users; } [...] } ///CONTROLLER private readonly ISampleRepository repository; public SampleController(ISampleRepository repository) { this.repository = repository; } //GET /data public class SampleController : ApiController { public IEnumerable<DataDTO> Get() { var result = repository.GetAll(); if (result.Count > 0) { return result; } var response = new HttpResponseMessage(HttpStatusCode.NotFound); response.Content = new StringContent("Unable to find any result to match your query"); throw new HttpResponseException(response); } }
Perhaps your mileage may change, and you can further ignore some of this data. The good news is that many of the templates and ideas that you may have already used in MVC-based projects remain valid .
Jsancho
source share