Using Teleriks new LINQ implementation to create OData feeds
This week Telerik released
a new LINQ implementation that is simple to use and produces domain models very
fast. Built on top of the enterprise grade OpenAccess
ORM, you can connect to any database that OpenAccess can connect to such as: SQL
Server, MySQL, Oracle, SQL Azure, VistaDB, etc. While this is a separate LINQ implementation
from traditional OpenAccess Entites, you can use the visual designer without ever
interacting with OpenAccess, however, you can always hook into the advanced ORM features
like caching, fetch plan optimization, etc, if needed.
Just to show off how easy our LINQ implementation is to use, I will walk you through
building an OData feed using Data
Services Update for .NET Framework 3.5 SP1. (Memo to Microsoft: P-L-E-A-S-E hire
someone from Apple to name your products.) How easy is it? If you have a fast machine,
are skilled with the mouse, and type fast, you can do this in about 60 seconds via
three easy steps. (I promise in about 2-3 weeks that you can do this in less then
30 seconds. Stay tuned for that.)
Step 1 (15-20 seconds): Building your Domain Model
In your web project in Visual Studio, right click on the project and select Add|New
Item and select Telerik OpenAccess Domain Model as your item template. Give the
file a meaningful name as well.
Select your database type (SQL Server, SQL Azure, Oracle, MySQL, VistaDB, etc) and
build the connection string. If you already have a Visual Studio connection string
already saved, this step is trivial. Then select your tables, enter a name for
your model and click Finish. In this case I connected to Northwind and selected only
Customers, Orders, and Order Details. I named my model NorthwindEntities and
will use that in my DataService.
Step 2 (20-25 seconds): Adding and Configuring your Data Service
In your web project in Visual Studio, right click on the project and select Add|New
Item and select ADO .NET Data Service as your item template and name your service.
In the code behind for your Data Service you have to make three small changes. Add
the name of your Telerik Domain Model (entered in Step 1) as the DataService name
(shown on line 6 below as NorthwindEntities) and uncomment line 11 and add a * to
show all entities. Optionally if you want to take advantage of the DataService 3.5
updates, add line 13 (and change IDataServiceConfiguration to DataServiceConfiguration
in line 9.)
1: using System.Data.Services; 2: using System.Data.Services.Common; 3: 4: namespace Telerik.RLINQ.Astoria.Web 5: { 6: public class NorthwindService
: DataService<NorthwindEntities> 7: { 8: //change the IDataServiceConfigurationto DataServiceConfiguration 9: public static void InitializeService(DataServiceConfiguration
config) 10: { 11: config.SetEntitySetAccessRule("*",
EntitySetRights.All); 12: //take advantage of the "Astoria3.5 Update" features 13: config.DataServiceBehavior.MaxProtocolVersion
= DataServiceProtocolVersion.V2; 14: } 15: } 16: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Step 3 (~30 seconds): Adding the DataServiceKeys
You now have to tell your data service what are the primary keys of each entity. To
do this you have to create a new code file and create a few partial classes. If you
type fast, use copy and paste from your first entity, and use a refactoring
productivity tool, you can add these 6-8 lines of code or so in about 30 seconds.
This is the most tedious step, but dont worry, Ive bribed some of the developers
and our next update will eliminate this step completely.
Just create a partial class for each entity you have mapped and add the attribute
[DataServiceKey] on top of it along with the keys field name. If you have any complex
properties, you will need to make them a primitive type, as I do in line 15. Create
this as a separate file, dont manipulate the generated data access classes in case
you want to regenerate them again later (even thought that would be much faster.)
1: using System.Data.Services.Common; 2: 3: namespace Telerik.RLINQ.Astoria.Web 4: { 5: [DataServiceKey("CustomerID")] 6: public partial class Customer 7: { 8: } 9: 10: [DataServiceKey("OrderID")] 11: public partial class Order 12: { 13: } 14: 15: [DataServiceKey(new string[]
{ "OrderID", "ProductID" })] 16: public partial class OrderDetail 17: { 18: } 19: 20: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Done! Time to run the service.
Now, lets run the service! Select the svc file and right click and say View in Browser.
You will see your OData service and can interact with it in the browser.
Now that you have an OData service set up, you can consume it in one of the many
ways that OData is consumed: using LINQ, the Silverlight OData client, Excel PowerPivot,
or PhP,
etc.
Happy Data Servicing!
Technorati
Tags: Telerik,Astoria,Data
Services
Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.