Click here to Skip to main content
15,885,244 members
Articles / Database Development / SQL Server

GPS Tracer Extension: storing the path on SQL2005 via Web Services

Rate me:
Please Sign up or sign in to vote.
4.53/5 (9 votes)
7 Sep 20075 min read 33.6K   427   33  
This article extends Leonardo Salvatore's project "A GPS tracer application for Windows Mobile CE 5" using the Web Service Software Factory. It shows how to create a robust solution for storing the path on a DB server, using Web Services and SQL
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using GpsTracerServer.BusinessEntities;
using System.Data.SqlClient;
using System.Diagnostics;
using GpsTracerServer.DataAccess.SQLServer;

namespace GpsTracerServer.DataAccess
{
    /// <summary>
    /// Respository that lets you find Device in the
    /// CRM database.
    /// </summary>
    public class DeviceRepository : Repository<Device>
    {
        private string databaseName;

        public DeviceRepository(string databaseName)
            : base(databaseName)
        {
            this.databaseName = databaseName;
        }


        public List<Device> GetAllFromDevice()
        {
            ISelectionFactory<NullableIdentity> selectionFactory = new GetAllFromDeviceSelectionFactory();

            try
            {
                NullableIdentity nullableIdentity = new NullableIdentity();
                return base.Find(selectionFactory, new GetAllFromDeviceFactory(), nullableIdentity);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, selectionFactory);
            }

            return new List<Device>();
        }

        public void Add(Device device)
        {
            DeviceInsertFactory insertFactory = new DeviceInsertFactory();
            try
            {
                base.Add(insertFactory, device);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, insertFactory);
            }
        }

        public void Remove(System.Guid iD)
        {
            IDeleteFactory<System.Guid> deleteFactory = new DeviceDeleteFactory();

            try
            {
                base.Remove(deleteFactory, iD);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, deleteFactory);
            }
        }


        public void Save(Device device)
        {
            DeviceUpdateFactory updateFactory = new DeviceUpdateFactory();
            try
            {
                base.Save(updateFactory, device);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, updateFactory);
            }
        }

        private void HandleSqlException(SqlException ex, IDbToBusinessEntityNameMapper mapper)
        {
            if (ex.Number == ErrorCodes.SqlUserRaisedError)
            {
                switch (ex.State)
                {
                    case ErrorCodes.ValidationError:
                        string[] messageParts = ex.Errors[0].Message.Split(':');
                        throw new RepositoryValidationException(
                            mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),
                            messageParts[1], ex);

                    case ErrorCodes.ConcurrencyViolationError:
                        throw new ConcurrencyViolationException(ex.Message, ex);

                }
            }

            throw new RepositoryFailureException(ex);
        }
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions