views:

238

answers:

4

In short: What is a good method for setting up read-only data access from Silverlight to a MySQL database?


Here are the details of my situation:

I'm currently trying to set up a Silverlight application to present data from a MySQL database. Currently, I need to set-up read-only access to the MySQL database (I may set up other tables for complete CRUD functionality at a later, date, but for these particular tables, I'm only ever going to be concerned with the retrieve aspect).

I tried setting it up using RIA Services (CTP July 2009) with Entity Framework, but I had trouble debugging it and ended up trying to recompile the source code from the MySQL ADO.NET connector in order to install custom DLLs into the GAC. I wasn't able to get any of this stuff to work correctly.

My problem was that I had date values stored as 0000-00-00 in lots of my MySQL tables. The MySQL ADO.NET Connector throws an exception everytime it tries to bring down a row with an invalid date in it. I would try to recompile the connector (see links above), but that's feeling very much like a hack. I would try to update the values in the MySQL database to be within the appropriate spec for dates, but our IT manager (and effectively our DBA) does not want to do it.

I don't mind learning to work with LINQ (LINQ-to-what?), but I want to avoid concatenating my own strings of SQL commands. Because of the Date restrictions, I need a way to specify Case When orders.OrderDate = '0000-00-00' Then '0001-01-01' Else orders.OrderDate End for pretty much every date instance.

I'm especially interested to hear from folks who have worked with .NET and MySQL together. What will work in my situation?

A: 

You should use RIA Services, the newest version came out last week, and it's included in the silverlight 4 beta now.

http://silverlight.net/getstarted/silverlight-4-beta/

You don't have to use the entity framework with RIA, there are other options. We do, but we use SQL Server so that might not be your favorite.

They have changed the errors some in the new RIA Stuff, so I'd recommend taking a 2nd look. Here's Brad Abrams' example from last week:

http://microsoftpdc.com/Sessions/CL21

Finally, if you're having a lot of trouble debugging, you could take a look at Fiddler. It's a program that watches the traffic and it can display you the errors you're having in a more obvious fashion.

thepaulpage
Thanks for the suggestion. I'll take another look at RIA Services with beta 4. However, I still need to find that link *between* the Domain Data Source and the MySQL DB. Somewhere along the way, I still have to be able to account for the invalid date.
Ben McCormack
I should also note: Entity Framework and RIA Services work *really well* with SQL Server since they are all Microsoft products. However, they don't play as nicely with MySQL, especially when there are issues with the MySQL data. I'd love to be able to use RIA Services, but if I feel like I'm hacking it to make it work, it may not be the best solution.
Ben McCormack
microsoft has been stressing that RIA Services (or WCF RIA Services) should play nicely with things that aren't Entity. If you check out Colin Blair's blog:http://www.riaservicesblog.com/Blog/He talks about how he dislikes the Domain Data Source, and suggests (in my mind) better ways. You really are more playing with your data context. There's a video about using RIA with NHibernate instead of Entity and you could maybe look into that too. I think that this is it: http://microsoftpdc.com/Sessions/CL07
thepaulpage
why the heck does this answer deserve negative votes? What did it ever do to you?
thepaulpage
A: 

My problem was that I had date values stored as 0000-00-00 in lots of my MySQL tables.

Can you just write Select NullIf( SomeDate, '0000-00-00') As SomeDate From SomeTable in your SQL queries? I don't know MySQL, but that's what I would do in T-SQL.

Jonathan Allen
If I were writing straight SQL, I would probably do something similar, such as `CASE WHEN SomeDate = '0000-00-00' Then '1901-01-01' Else SomeDate End As SomeDate`. However, my problem is setting up the plumbing so that I'm *not* doing straight SQL but sill have the flexibility to enter something like the `CASE` statement above as needed.
Ben McCormack
Can you wrap your tables in views? If so, that would effectively clean your data without forcing you to write SQL in a particular way.
Jonathan Allen
That's not a bad suggestion, but I just tested it and performance was terrible when trying to use the data field in the `Where` part of the SQL statement. I imagine this is because the `CASE` statement in the view must be evaluated before a query can be run against the resulting data set of the views. Either way, it's a difference of 31 ms versus 3078 ms.
Ben McCormack
Wow, that really sucks. How about just giving up and using a copy of SQL Express that is bulk-loaded from the real database on a regular basis?
Jonathan Allen
A: 
Kamal
you can add validation code to you Silverlight app to check for incorrect dates.
Kamal
I appreciate the suggestion and like your creative use of Linq to XML. However, I'm trying to avoid queries to the server that involve `SELECT * FROM ...`. Even though the data is more "parseable" once in XML format, I'd have to download hundreds of thousands of records before I began parsing them. It would take way too much time.
Ben McCormack
+1  A: 

Why has no one suggested using a ORM to hide the mySQL details? Both NHibernate and Subsonic support mySQL. Both are very customisable in how they interact with the database and should allow you to cater for malformed dates.

By using an ORM your data objects are now POCOs, and you can use whatever you want to get the data to the Silverlight client. Vanilla web services or WCF should be fine. RIA services if you want to try out the bleeding edge.

IMHO, this will be simpler than setting up a mysql->php->xml->asp.net->silverlight chain.

Alex