6
votes

I'm starting to integrate WebApi & OData into a test bed application. Let's keep it simple and stick with one domain entity, Customer. Obviously I will have an MVC controller. Searching gets it own view model (based on a Lucene index), so that will be separate controller, right now ODataController. But since the view/edit pages will have their own view models, they'd be their own controller. This starts to feel like overkill.

Trying to figure out a good design to make this work and still work with the idea of the URL representing the entity. Should the entity in the URL be Customer and somehow I provide different representations based on URL params? Or should Customer/CustomerSearch/CustomerEdit be different entities (which doesn't sound right)?

1
Rich, I'm currently working on a project where we intend to use OData and Web API, but I want to make it flexible (likee EF calls). We're not at that stage (intend to move from direct db to services). You are on thinking ahead of the curve. That's why I don't think there is anyone to help you. OData is not new anymore but I don't think many shops are using it yet. The marriage with web API seemed intuitively obvious to me so I did a research. here are some videos on implementing such a solution from our frnds at MS asp.net/web-api/overview/odata-support-in-aspnet-web-api - Dave Alperovich

1 Answers

0
votes

I'm presuming that this WebAPI application is going to be a separate solution from the ASP.NET MVC solution you are going to build. In a nutshell, the WebAPI will be the business/domain layer and the MVC will be the presentation layer.

So, in speaking of the WebAPI solution, you only need a single ApiController for the Customer example you presented above. The view/edit request may have their own view models...or not. Depending on how you craft the view model, you may be able to have a single view model for customers, or you could develop a customer view hierarchy where the base view model holds the search-related data and the descendant view model fleshes out the details.

Your routing requests could look like:

GET - /Customer/                  retrieve multiple customers 
                                  (supplying OData parameters in query string)
GET - /Customer/{id}              retrieve a single customer
PUT - /Customer/{id}              edit customer

What it looks like is you will need is two routes, one ApiController for Customer, and three request methods for what you described. I don't recommend a separate ApiController for OData, because the functionality is dependent on a entity.