Providing OData in an MVC Application

I want to offer my users rich query functionality so that they can dynamically add Where clauses, change the sort order and grouping. oData seems like the best technology to work with, but I don't know how to implement it correctly in an MVC application.

1) How to connect a multifunctional client that supports oData with MVC architecture?

2) Should I open IQueryable in my repository?

3) Can someone explain how to create, update or delete using 2 linked tables in MVC? (in the service layer?)

+4
source share
3 answers

A good place to start learning OData is here: http://msdn.microsoft.com/en-us/data/odata.aspx

In general, OData and web services should be used if they are required because 1) you must share physical layers for security or performance reasons or 2) many applications or consumers need to use the same API. Of course, there are other reasons, but usually, if you need to ask if OData makes sense, you do not have these requirements.;)

+1
source

I am going to explain this in a simple way, and I hope that it will be useful to you.

1) Create an empty console application.

2) Make a service link to any public OData service. That is http://services.odata.org/northwind/northwind.svc/

enter image description here

After that, Visual Studio is going to add some more assembly references, as shown below.

enter image description here

3) Write the following code

using System; using System.Collections.Generic; using System.Data.Services.Client; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace ConsoleApplication4 { class Program { static DataServiceContext ctx = new DataServiceContext(new Uri("http://services.odata.org/northwind/northwind.svc/")); static void Main(string[] args) { IEnumerable<ServiceReference1.Category> response = ctx.Execute<ServiceReference1.Category>(new Uri("http://services.odata.org/northwind/northwind.svc/Categories")); } } } 

4) Set a breakpoint at the end of the main method. And now the debugging application. You will see a list of categories.

enter image description here

5) If OData is exposed with permission to implement all CRUDs , you can do it. And, of course, you can return response in ASP.NET MVC, but you must first convert it to your Model class.

Perhaps you can save a static DataServiceContext ctx = new DataServiceContext(new Uri("http://services.odata.org/northwind/northwind.svc/")); in the BaseController class.

And also you get the property value as follows:

enter image description here

PS Check out this video http://www.youtube.com/watch?v=e07TzkQyops .

+1
source

I wrote the Restfull API in MVC and found ODATA. Great MS implementation.

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api

is where you can get you started with odata perfectly.

I looked at this page

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/getting-started-with-odata-in-web-api/create-a-read-only- odata-endpoint

and was launched as soon as possible.

Hope this helps. Such a nice thing to work with design and consumption.

www.odata.org/for more information

PS. the links I posted also have CRUD .... PSS operations examples. IQueryable is the point .....

+1
source

All Articles