Utilizing JSON Endpoints in WCF Domain Services

In the real-world, there is a possibility that you may be interested in leveraging the WCF Domain Services that you’ve created alongside your Silverlight application… for other client types. (Such as Javascript clients.) In order to do this, there are some simple hoops to jump through. However, once you’ve made it through those initial setup hoops… the REAL work begins. You’ll want to successfully execute a query across the JQuery/JSON platforrm against the WCF Domain Services. You may also want to perform update and insert data operations! How is that done?

These videos (4 in total) will explain how it is done. They will be using a relatively simple T4-template that I’ve implemented specifically for the .NET 4.0 version of Entity Framework. The T4-Template is written to support basic CRUD operations against a single collection of table-related classes. (Notice I didn’t mention scenarios where there are INCLUDE statements in the LINQ syntax behind the WCF Domain Services.) The T4-Template doesn’t do much in the way of supporting the necessary operations to track related entities in JSON that come back from the WCF Domain Service query.

At any rate… the following video takes about 6 minutes and describes how to setup a basic ASP.NET MVC3 application to utilize Entity Framework data models, and expose them via a WCF Domain RIA Service through JSON formatting… to a JQuery client-side script. (Note, it is assumed in the video that the user has already installed and configured the ASP.NET MVC3 Razor engine in their VS2010 environment. It is also assumed that the user has installed VS2010 SP1 successfully.)

This 12-minute video is a simple demonstration of how to get your JQuery AJAX request to work with WCF Domain Services (RIA Services) that expose your Entity Framework class elements. It’s a simple matter of understanding the JQuery AJAX API and options… and then understanding what Microsoft wants you to set the AJAX parameters to.

Finally, these 2 (related) videos demonstrate how to leverage a somewhat simple T4 template that I created to auto-generate an entire Javascript file that replicates the entities/classes that are exposed via the Entity Framework model. In other words, this video explains how to easily work with the Entity Framework class-fields and WCF Domain Services tricks that are necessary to implement a working C.R.U.D. application with zero post-backs. (14 and 10 minutes respectively)

… and here is the second part of the final JQuery INSERT logic necessary to get records into your SQL Server database via WCF RIA Services from JQuery.



So now that we have the videos out-of-the-way, I wanted to make certain that the readers understood what the JSON actually looked like in the ‘REQUEST’ body prior to being processed by the WCF RIA Service’s ‘SubmitChanges’ method.

Below, you can see what an ‘INSERT’ operation actually looks like:

1
{"changeSet":[{"Id":0,"Operation":2,"OperationName":null,"OperationData":null,"Entity":{"__type":"Candidate:#MvcApplication2.Models","CandidateId":1,"FirstName":"Chad","LastName":"Stoker","Nickname":"sdf"}}]}

Below, you can see what an ‘UPDATE’ operation actually looks like:

1
{"changeSet":[{"Id":0,"Operation":3,"OperationName":null,"OperationData":null,"Entity":{"__type":"Candidate:#MvcApplication2.Models","BonusOptions":null,"CandidateId":1,"FirstName":"Chad2","IsNeverHire":false,"LastName":"Stoker2","NegotiatedSalary":null,"Nickname":"sdf","RankingId":null},"OriginalEntity":{"__type":"Candidate:#MvcApplication2.Models","BonusOptions":null,"CandidateId":1,"FirstName":"Chad","IsNeverHire":false,"LastName":"Stoker","NegotiatedSalary":null,"Nickname":"sdf","RankingId":null}}]}

Finally, this is what a ‘DELETE’ operation looks like :

1
{"changeSet":[{"Id":0,"Operation":4,"OperationName":null,"OperationData":null,"Entity":{"__type":"Candidate:#MvcApplication2.Models","BonusOptions":null,"CandidateId":1,"FirstName":"Chad2","IsNeverHire":false,"LastName":"Stoker2","NegotiatedSalary":null,"Nickname":"sdf","RankingId":null}}]}

Attached here, you can find the T4-Template that I created to automatically generate a series of Javascript classes based on the Entity-Framework C# code and foreign-key relationships.

T4-Template : EF_to_Javascript_T4

Enjoy! Let me know if you have any questions…

Thanks.

Comments are closed.

Our Capabilities Include:


Custom Software Development
Enterprise Architecture
Project Management
Systems Analysis
Performance Testing

AND THE LIST GOES ON...

These methods are vital to our work:


Agile Methodology
PMBOK
Test-Driven Development

LEARN WHY...

About CodeSmart, Inc.


CodeSmart has been locally owned and operated in the Olympia, WA area since 2002. We direct, design, develop and deliver full end-to-end information systems using leading edge Microsoft .Net technologies and recommended best practices.

LEARN MORE...