views:

378

answers:

5

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
+4  A: 

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.

Hrvoje
+1 "RIA and WCF Data Services", however the request are not necessarily REST but thats not something that the developer need worry too much about.
AnthonyWJones
the term that brings up the most relevant hits is "WCF RIA Services", e.g. http://silverlight.net/getstarted/riaservices/
Edward Tanguay
It's hard to keep up with constant renaming of this framework. Long time ago there was Astoria, which is REST-style interface to IQueryable data through WCF web services. On top of that they recently build WCF RIA Services for Silverlight, to enable quick and easy consumption of Astoria with LINQ queries (WCF Data Services). But, there are some concepts inside RIA Services that i really don't like and remembers me to ASP.NET SqlDataSource control inside ASPX file, and SQL query in it! Keep your data access logic in separate loosely coupled layer, not UI declaration file!
Hrvoje
+1  A: 

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.

Henrik Söderlund
right, I'm looking for something that will generate this web service layer for my just as LINQ-to-SQL generates the data model class for me so I don't have to think about it, just "use it via linq"
Edward Tanguay
+1 for pointing out that I want to generate a web service layer, not data model classes, changed that.
Edward Tanguay
+3  A: 

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.

CraigS
+1 this is the kind of path-through-the-jungle advice I am looking for, will try
Edward Tanguay
Let me know how it goes - happy to offer further advice. I've done this sort of Silverlight-to-linq thing a few times now...
CraigS
on step 3 when I add a silverlight-enabled wcf service to the web app, I get "Object reference not set to an instance of an object", although it seems to create the service, any advice there?
Edward Tanguay
then in step 5, it says "error searching for services unter http://localhost:49417/Test.svc"
Edward Tanguay
Regarding step 3, does it give you a line reference? What code is having trouble. Regarding step 5, you may need to do a recompile of the whole solution before adding the service
CraigS
I know this is pretty old, but I am having trouble making this stuff work. How/where do you "Instantiate the service"? I can create my methods, but cannot access them.
Jacob Huggart
+1  A: 

I would strongly recommend that you clear an hour in your schedule and just watch this video:-
net-ria-services-intro

In fact clear 2 hours and work along side the video building your own copy of the app being built.

AnthonyWJones
+1  A: 

If you want to develop a regular LOB (Line Of Business) Application, then you should follow the approach using a web service interface to your database. The RIA services or WCF data service is the current RAD (Rapid Application Development) technology by Microsoft to make this task easier.

If you're talking about a special scenario you need: Silverlight 4 when running out of browser can talk to COM servers on windows now. Talking to databases is described in this vast blog post: Cutting Edge Silverlight 4 Com Features.

If you're looking for a way to manipulate database-like files, you may take a look at the csharp-sqlite project. I think it compiles for Silverlight without much ado (quick and dirty proof of concept here: Proof of Concept csharp-sqlite in Silverlight). With it you could create and manipulate a database file in the user's isolated file storage in a regular Silverlight application and then upload it to wherever you desire.

herzmeister der welten
+1 for the idea of running a sqlite database in the users's isolated storage, clever
Edward Tanguay
here's an article describing sqlite in silverlight: http://www.itwriting.com/blog/1695-proof-of-concept-c-sqlite-running-in-silverlight.html
Edward Tanguay