Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF

A Silverlight Introduction for Line-of-Business Applications

Rate me:
Please Sign up or sign in to vote.
4.71/5 (15 votes)
30 Aug 2009CPOL18 min read 54.1K   660   56  
An introduction on Silverlight for developers of administrative applications. The article starts by explaining the basics of WPF and then delves deeper in the business-oriented aspects.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.Linq;
using System.IO;
using System.IO.Compression;
using System.Xml.Serialization;

namespace CarSelector.Web
{
    // NOTE: If you change the class name "CarService" here, you must also update the reference to "CarService" in Web.config.
    public class CarService : ICarService
    {
        #region ICarService Members

        public List<SimpleCar> GetCarsOverview()
        {
            DBDataClassesDataContext db = new DBDataClassesDataContext();
            var list = (from c in db.Cars
                        select new SimpleCar { ID=c.Id, Make = c.Make, Model = c.Model, Type = c.Type }).Take(100);

            List<SimpleCar> result = new List<SimpleCar>();
            foreach (var a in list)
            {
                result.Add(a);
            }

            return result;
        }

        public bool UpdateCar(Car car)
        {
            if (car != null)
            {
                car.Picture = DeflateData(car.Picture);

                DBDataClassesDataContext db = new DBDataClassesDataContext();
                db.Cars.Attach(car, true);
                db.SubmitChanges();

                return true;
            }
            else
            {
                return false;
            }
        }

        public Car GetCar(int carId)
        {
            DBDataClassesDataContext db = new DBDataClassesDataContext();
            Car car = (from c in db.Cars
                        where c.Id == carId
                        select c).Take(1).First();

            if (car != null)
            {
                //  decompress image serverside: SL has not native compression classes !
                //  initialize the default lenght to the compressed data length times 2
                car.Picture = InflateData(car.Picture);

                return car;
            }
            else
                return null;
        }

        /// <summary>
        /// Compresses data using the <see cref="DeflateStream"/> algorithm.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] DeflateData(byte[] data)
        {
            if (data == null) return null;

            byte[] buffer = null;
            using (MemoryStream stream = new MemoryStream())
            {
                using (DeflateStream inflateStream = new DeflateStream(stream, CompressionMode.Compress, true))
                {
                    inflateStream.Write(data, 0, data.Length);
                }

                stream.Seek(0, SeekOrigin.Begin);

                int length = Convert.ToInt32(stream.Length);
                buffer = new byte[length];
                stream.Read(buffer, 0, length);
            }

            return buffer;
        }

        /// <summary>
        /// Inflates compressed data using the <see cref="DeflateStream"/> algorithm.
        /// </summary>
        /// <param name="compressedData"></param>
        /// <returns></returns>
        public static byte[] InflateData(byte[] compressedData)
        {
            if (compressedData == null) return null;

            //  initialize the default lenght to the compressed data length times 2
            int deflen = compressedData.Length * 2;
            byte[] buffer = null;

            using (MemoryStream stream = new MemoryStream(compressedData))
            {
                using (DeflateStream inflatestream = new DeflateStream(stream, CompressionMode.Decompress))
                {
                    using (MemoryStream uncompressedstream = new MemoryStream())
                    {
                        using (BinaryWriter writer = new BinaryWriter(uncompressedstream))
                        {
                            int offset = 0;
                            while (true)
                            {
                                byte[] tempbuffer = new byte[deflen];

                                int bytesread = inflatestream.Read(tempbuffer, offset, deflen);

                                writer.Write(tempbuffer, 0, bytesread);

                                if (bytesread < deflen || bytesread == 0) break;
                            }   // end while

                            uncompressedstream.Seek(0, SeekOrigin.Begin);
                            buffer = uncompressedstream.ToArray();
                        }
                    }
                }
            }

            return buffer;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO TRI-S bvba, Cogenius bvba
Belgium Belgium
I'm working since 1999 in an IT environment: started developing in PROGRESS 4GL, then VB6 and am working since 2003 with C#. I'm currently transitioning to HTML5, CSS3 and JavaScript for the front-end development.
I started my own company (TRI-S) in 2007 and co-founded another one (Cogenius) in 2012.
Besides being a Microsoft Certified Professional Developer (MCPD) I'm also a Microsoft Certified Trainer (MCT) and am teaching .NET and JavaScript courses.

Comments and Discussions