Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of LINQ-to-SQL for Silverlight?

I have a WPF application that uses LINQ-to-SQL on a local .MDF file. This solution is simple, easy, and effective, i.e. I set up my model once, then read/write data anywhere via LINQ:

        using (var db = Datasource.GetContext())
        {
            oldItem = (from i in db.Infos
                       where i.Id == TheId
                       select i).SingleOrDefault();
            CreateForm(db, FormBase, oldItem, Button_Save);
        }

What is the dead-simple equivalent of this for Silverlight apps?

Searching I find an explosion of terms:

  • WCF RIA Services, WCF Data Services (ADO.NET Data Services, Astoria), Data Services Toolkit
  • .NET RIA Services
  • OData(Dallas)
  • GData
  • REST, REST-based, REST-like, REST-inspired
  • XML, JSON, RDF+XML
  • web services, SOA
  • cloud-based services, Azure, SQL Azure, Azure Services Platform

All I want to do is this:

  • create an .mdf file
  • use some LINQ-to-SQL-like tool to generate a web-enabled (REST?) data layer etc.
  • ftp the .mdf file and classes up to my ASP.NET web hosting service
  • write silverlight clients that read and write to this data source with LINQ
like image 274
Edward Tanguay Avatar asked Jan 28 '10 10:01

Edward Tanguay


3 Answers

Concentrate on learning RIA Data Services, or WCF Data Services. It converts your LINQ queries inside Silverlight to REST requests, and saves you from writing some of the infrastructure code. The whole idea is that your SL app communicates only to web services, you don't have access to physical DB, like when you're using some ORM (L2S). In SL, your are inside browser's sandbox, which prevents you from accessing file system, including db files.
Other approach is to write web service and expose data through it (GetArticleByID), and then you consume that services from Silverlight. Then you use LINQ to iterate on fetched, loaded data.

like image 118
Hrvoje Hudo Avatar answered Oct 21 '22 10:10

Hrvoje Hudo


Here's what to do -

  1. Create a Silverlight app using Visual Studio. You will get two projects, one with the Silverlight XAML, and the other a web application to host it.

  2. In the web application, add your DBML (Linq-2-SQL) file. Configure as normal

  3. In the web application, add a Silverlight enabled WCF service

  4. In the WCF service, define some methods that access the L2S data context

  5. In the Silverlight project, go to add Service Reference, hit "discover" and add the WCF service in

  6. Instantiate the service, and access your methods

Sounds a little complex, but pretty straight forward in practice.

like image 44
Craig Schwarze Avatar answered Oct 21 '22 11:10

Craig Schwarze


Not possible. Silverlight cannot access databases directly. Some sort of web service layer is required in between. I think WCF and RIA Services are the most widely used.

like image 38
Henrik Söderlund Avatar answered Oct 21 '22 09:10

Henrik Söderlund