locked
PSA regarding Silverlight + WCF + Data Access RRS feed

  • Question

  • I can't find the setting that lets me flag this as "not a question", but I'm posting this to help people who are new to Silverlight &/or WCF but not new to .NET application development and data access in general.

    Although the new Entity Framework, Linq to SQL, etc. are great new technologies and can be very useful in your development projects, you do not have to use them to access data from a Silverlight / WCF application.  You can still use the tried-and-true methods of classic ADO.NET, the MS Enterprise Libraries, or whatever 3rd party object relational modelling tool you are used to working with.  It may seem like I'm stating the obvious, but since just about every tutorial I've seen and every answered question on the topic points to all the new database access technologies, I feel I must mention it.

    In your WCF service, you can call any .NET class library and/or use the full .NET framework.  So there is nothing stopping you from writing a WCF method that calls your old .NET data access libraries which use datasets and adapters and all those fun things, and then converting those datasets to a list of an xml serializable class defined as a WCF DataContract for returning to the Silverlight layer and then using in the UI.

    For example, if I want to be cliche'd and query my database for a list of the books I sell,

    I might create a WCF OperationContract as:

    [OperationContract]

    public Booklist GetBooks()

    {

        Booklist list = new Booklist();

        BookData bookdata = new BookData(); //my old legacy class which knows how to retrieve a datatable of books

        DataTable dt = bookdata.GetBooks();

        if(dt!=null && dt.Rows.Count > 0)

        {

            foreach(DataRow row in dt)

            {

                    list.Add(new Book(row));

            } 

        }

        return list;

     } 

     For this to work, I need to define Book and Booklist:

    [DataContract]

    public class Book

    {

        public Book(DataRow row)

        {

            BookID=(int)row["BookID"];

           Name = row["BookName"].ToString();

        ...etc

    }

       [DataMember]

       public string Name{get;set;}

        [DataMember]

        public int BookID{get;set;}

        ...etc.

    }

    Booklist is a special WCF class called a "CollectionDataContract" -- use these to avoid having WCF turn your lists into arrays...ick!

    [CollectionDataContract]

    public class Booklist : List<Book> {}  //that's all you have to do here

     

    There are other resources that cover calling the WCF service, binding the resulting data etc. so I won't go into detail here on how to do that part of it.

    I just know I found it very frustrating when I was first starting with Silverlight and WCF to see everyone talking as if to do data access you had to use these new techologies, giving me yet another unknown to try to work out when I was already trying to figure out other new things.  So, I wanted the information that you don't HAVE to use the new data access technologies to come up in people's forum searches as well as all the suggestions to use them.

    Now, once you understand the Silverlight & WCF aspects I encourage you to learn about all the new data access technologies too.  There is some neat stuff there that can in some scenarios save you oodles of effort.  There was a very good article in the Sept/Oct 2008 issue of "Code - Component Developer Magazine" that described the differences, advantages and disadvantages of the myriad of ways you can do .NET data access now, I encourage you all to read it if you want to learn about these as well.

     Happy coding,

     

    Friday, January 30, 2009 3:10 PM

Answers

  • To answer the question in your post (how to post a non-question), the only thing you can do is change the icon for your post to something like  so that people see it as different.

    In regards to you post that is great information. You should also write it up in a blog post or turn it into a sample for the community section.

    Friday, January 30, 2009 3:19 PM