8

I'm researching the new ASP.NET MVC4 Web API framework. I'm running Visual Studio 2011 beta on the Windows 8 consumer preview.

My problem is that none of the official samples for the new Web API framework use any kind of database backend. In the past, I've been able to create a local SQL CE database and serve it up via a WCF Data Service using Entity Framework as the ORM. How do I do the same with Web API?

Also, is this even a valid question? Should I just keep using a WCF Data Service if I want to expose an Entity Framework mapped SQL CE database? It seems to work fine, other than not offering the flexibility to choose response formatters that I might get with web api.

frennky
  • 12,581
  • 10
  • 47
  • 63
Jeremy Bell
  • 5,253
  • 5
  • 41
  • 63

2 Answers2

4

If you look at the official Contact Manager sample, you'll find that the Repository Pattern is used to access the data layer. Also, bear in mind that in this particular example there's also DI via Ninject.

In my case I've easily plugged this onto an already existing EF model.

Here's an example for a 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);
    }
}

Your mileage may vary though, and you might want to abstract out some of this data access even further. Good news is that plenty of patterns and ideas that you may have already used on MVC-based projects are still valid.

JSancho
  • 1,435
  • 1
  • 13
  • 15
  • 3
    Not bad, but I wouldn't bother creating a repository in this case. Entity Framework, especially newer versions, are already using the repository pattern. The wrapper doesn't do anything different aside from adding complexity. – ShadowChaser May 23 '12 at 15:41
  • I have pretty much the same setup as your code above - I add new records via the normal MVC app then attempt to retrieve them with the web api using the same repository. However the web api won't return newly added records, only ones that I initially add with the Seed method in the main app. Any idea why this is, or how I can get the web api to return newly added records? – Ciarán Bruen Jun 14 '12 at 07:38
  • Ciaran, that sounds like you might either have connection strings pointing to different resources, i.e SQL compact vs SQL server. Or that you might be completely recreating the database with every execution. But I haven't got a lot to work with here. Your query probably warrants a whole new question with an isolated example. – JSancho Jun 14 '12 at 13:34
  • I'm referencing the same repository as the main app...there's no db connection info at all in my web api app. I've also tried referencing the DbContext layer in the main app directly but still no joy. I have a separate question here if you're interested: http://stackoverflow.com/questions/11024885/web-api-not-returning-newly-added-records-from-ef-4-1-dbcontext – Ciarán Bruen Jun 15 '12 at 06:50
  • Ok scratch that - I thought I had removed the db connection info from the web api but I hadn't so thanks for the hint :) – Ciarán Bruen Jun 15 '12 at 07:41
2

I haven't worked with WCF Web API, so I can say for sure if you can use it same way as you did with WCF Web API, but I'm sure you can use EF with ASP.NET Web API. I suggest you take a look at how ASP.NET MVC makes use of EF, it should be very similar how you would use it with ASP.NET Web API.

As for other question, if you're planning some new development you should consider using ASP.NET Web API since there's an announcement on wcf.codeplex.com saying:

WCF Web API is now ASP.NET Web API! ASP.NET Web API released with ASP.NET MVC 4 Beta. The WCF Web API and WCF Support for jQuery content on this site will be removed by the end of 2012.

frennky
  • 12,581
  • 10
  • 47
  • 63