Click here to Skip to main content
11,577,494 members (57,339 online)
Click here to Skip to main content
Add your own
alternative version

A Silverlight Introduction for Line-of-Business Applications

, 30 Aug 2009 CPOL 34.6K 596 58
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.
silverlightlob_src.zip
CarSelector
CarSelector.csproj.user
Img
exit16.png
open16.png
Properties
Service References
CarService
CarSelector.CarService.Car.datasource
CarSelector.CarService.SimpleCar.datasource
CarService.disco
CarService.wsdl
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
ServiceReferences.ClientConfig
Themes
CarSelector.Web
App_Data
CarSelector.Web.csproj.user
CarService.svc
ClientBin
CarSelector.xap
Media
DBDataClasses.dbml
DBDataClasses.dbml.layout
Properties
Settings.settings
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)

Share

About the Author

Xavier Spileers
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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 30 Aug 2009
Article Copyright 2009 by Xavier Spileers
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid