开发者

Exposing OData on a MVC application

I want to offer my users a rich query functionality so they can dynamically add Where clauses, change sort orders and groupings. oData seems to be the best technology for the job, but I don't know how to implement it correctly in a MVC application.

1) How can I connect a feature rich client that supports oData with a MVC architecture?

2) Must I expose IQueryable on my repository?

3) Can an开发者_开发知识库yone explain how to Create, Update, or Delete when using 2 joined tables in MVC? (in the service layer?)


A good place to start learning about 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 physical tiers need to be separated for security or performance reasons or 2) Multiple applications or consumers need to use the same API. There are of course other reasons but generally if you have to ask if OData makes sense you don't have those requirements. ;)


I was writing a Restfull API in MVC and found ODATA. Great implementation by MS.

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

is where you can get a great start on odata.

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 up and running in no time.

Hope this helps. Such a nice thing to work with designing and consuming.

www.odata.org/ for more info

PS. the links i posted have CRUD operation examples as well.... PSS. IQueryable is the point.....


I am going to explain it in some simple way and I hope it will useful for you.

1) Create empty Console application.

2) Make a service reference to any public OData service. I.e. http://services.odata.org/northwind/northwind.svc/

Exposing OData on a MVC application

After this Visual Studio is going to add some more assembly references like you can see below

Exposing OData on a MVC application

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) Setup a breakpoint at the end of the Main method. And now debug application. You are going to see the list of Categories.

Exposing OData on a MVC application

5) If the OData have exposed with permission to realize all CRUD then you can do it. And surely you can return response in ASP .NET MVC but first you have to transform it into your Model class.

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

And also you get property value like this:

Exposing OData on a MVC application

P.S. Take a look at this video http://www.youtube.com/watch?v=e07TzkQyops as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜