.Net Code Monkey RSS 2.0
 Wednesday, June 18, 2014

Viewing contents of SQL SERVER Data Tools DacPac files

You can view the contents of .dacpac files created by Visual Studio SQL Server datatools using either the DacUnpack.exe tool or by opening in WinZip / WinRar.

Wednesday, June 18, 2014 9:38:39 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
SQL Server | SSDT | Visual Studio 2012 | DacPac
 Thursday, May 22, 2014

C# - Simple Provider Pattern

Today we are going to look at what it takes to implement a simple provider pattern. I need this pattern as part of a bigger project. In a later article we will look at configuring this provider via custom sections in a web.config file.

First we are going to create our base provider which will inherit from System.Configuration.Provider.ProviderBase.

BiscuitProvider

using System;
using System.Configuration.Provider;

namespace Providers
{
    public abstract class BiscuitProvider : ProviderBase
    {
        public virtual String Action { get { return "I provide buiscuits"; } }
        public virtual Boolean RequiresAFilling { get { return false; } }
    }
}
We will now create three different biscuit providers which will inherit from BiscuitProvider and we will override the base functionality where required.

BourbonProvider

using System;

namespace Providers
{
    public class BourbonProvider : BiscuitProvider
    {
        public override String Description { get { return "I provide bourbons"; } }
        public override Boolean RequiresAFilling { get { return true; } }
    }
}

CustardCreamProvider

using System;

namespace Providers
{
    public class CustardCreamProvider : BiscuitProvider
    {
        public override String Description { get { return "I provide custard creams"; } }
        public override Boolean RequiresAFilling { get { return true; } }
    }
}

HobnobProvider

using System;

namespace Providers
{
    public class HobnobProvider : BiscuitProvider
    {
        public override String Description { get { return "I provide hob-nobs"; } }
    }
}
Next we need a collection to hold any of the providers we may want to use in code. This collection will inherit from System.Configuration.Provider.ProviderCollection. There will be two members, an indexer and an Add() method.

BiscuitProviderCollection

using System;
using System.Configuration.Provider;

namespace Providers
{
    public sealed class BiscuitProviderCollection : ProviderCollection
    {
        /// <summary>
        /// Gets the provider with the specified name.
        /// </summary>
        /// <param name="name">The name of the provider.</param>
        /// <returns></returns>
        public new BiscuitProvider this[String name]
        {
            get { return base[name] as BiscuitProvider; }
        }

        /// <summary>
        /// Adds a BiscuitProvider to the collection.
        /// </summary>
        /// <param name="provider">The provider to be added</param>
        /// <exception cref="System.ArgumentNullException">provider is null</exception>
        /// <exception cref="System.NotSupportedException">provider is of wrong type</exception>
        public void Add(BiscuitProvider provider)
        {
            // Validate arguments
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (!provider.GetType().IsSubclassOf(typeof(BiscuitProvider)))
            {
                throw new NotSupportedException("provider must inherit from BiscuitProvider");
            }

            // Add the provider to the base class
            base.Add((BiscuitProvider)provider);
        }
    }
}
Lastly we require a static class which will provide us with access to a single instance of the provider collection throughout the layer of the application. This class a private collection of providers and a public property that exposes the private collection called "Providers". The class also has a property "DefaultProvider" which can be set to hold the name of the default provider. A function "Provider" that returns the default provider, based upon the provider name held in "DefaultProvider".

Biscuits

using System;

namespace Providers
{
    public static class Biscuits
    {
        private static BiscuitProviderCollection _providers;

        /// <summary>
        /// Initializes the <see cref="Biscuits"/> class.
        /// </summary>
        static Biscuits()
        {
            _providers = new BiscuitProviderCollection();
        }

        /// <summary>
        /// Gets or sets the default provider
        /// </summary>
        public static String DefaultProvider { get; set; }

        /// <summary>
        /// Gets the default provider.
        /// </summary>
        /// <value>
        /// The default sweet provider.
        /// </value>
        public static BiscuitProvider Provider
        {
            get { return Providers[DefaultProvider]; }
        }

        /// <summary>
        /// Gets the provider collection.
        /// </summary>
        /// <value>
        /// The collection of providers.
        /// </value>
        public static BiscuitProviderCollection Providers
        {
            get { return _providers; }
        }
    }
}
Example usage of this pattern can be seen in the unit tests

Unit Tests

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Providers;
using System;

namespace ProvidersTests
{
    [TestClass]
    public class BiscuitProviderTests
    {
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Test_AddNullProvider_ResultsIn_ArgumentNullExceptionThrown()
        {
            // Arrange
            BiscuitProvider provider = null;

            // Act
            Biscuits.Providers.Clear();
            Biscuits.Providers.Add(provider);

            // Assert
            // Should not get here as exception should be thrown
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Test_SetNoDefultProvider_ResultsIn_ArgumentNullExceptionThrown()
        {
            // Arrange
            var bourbonProvider = new BourbonProvider();
            bourbonProvider.Initialize("bourbon", null);

            var custardCreamProvider = new CustardCreamProvider();
            custardCreamProvider.Initialize("custard cream", null);

            var hobnobProvider = new HobnobProvider();
            hobnobProvider.Initialize("hobnob", null);

            // Act
            Biscuits.Providers.Clear();
            Biscuits.Providers.Add(bourbonProvider);
            Biscuits.Providers.Add(custardCreamProvider);
            Biscuits.Providers.Add(hobnobProvider);

            // Assert
            Assert.IsNull(Biscuits.Provider);
        }

        [TestMethod]
        public void Test_SetDefultProvider_ResultsIn_ProviderNameMatchingDefault()
        {
            // Arrange
            var bourbonProvider = new BourbonProvider();
            bourbonProvider.Initialize("bourbon", null);

            var custardCreamProvider = new CustardCreamProvider();
            custardCreamProvider.Initialize("custard cream", null);

            var hobnobProvider = new HobnobProvider();
            hobnobProvider.Initialize("hobnob", null);

            var defaultProviderName = "hobnob";

            // Act
            Biscuits.Providers.Clear();
            Biscuits.Providers.Add(bourbonProvider);
            Biscuits.Providers.Add(custardCreamProvider);
            Biscuits.Providers.Add(hobnobProvider);
            Biscuits.DefaultProvider = defaultProviderName;

            // Assert
            Assert.AreEqual(defaultProviderName, Biscuits.Provider.Name);
        }

        [TestMethod]
        public void Test_SetDefultProvider_ResultsIn_ExpectingFillingRequired()
        {
            // Arrange
            var bourbonProvider = new BourbonProvider();
            bourbonProvider.Initialize("bourbon", null);

            var custardCreamProvider = new CustardCreamProvider();
            custardCreamProvider.Initialize("custard cream", null);

            var hobnobProvider = new HobnobProvider();
            hobnobProvider.Initialize("hobnob", null);

            var defaultProviderName = "custard cream";
            var expectedRequiresAFilling = true;

            // Act
            Biscuits.Providers.Clear();
            Biscuits.Providers.Add(bourbonProvider);
            Biscuits.Providers.Add(custardCreamProvider);
            Biscuits.Providers.Add(hobnobProvider);
            Biscuits.DefaultProvider = defaultProviderName;

            // Assert
            Assert.AreEqual(expectedRequiresAFilling, Biscuits.Provider.RequiresAFilling);
        }
    }
}
In the next article we will look at how to configure the providers in a web.config file using custom config sections.

The full code for this article is available here on my github.

Thursday, May 22, 2014 6:14:51 AM (GMT Standard Time, UTC+00:00)  #    Comments [2] -
.Net | C# | Config Files | Inheritance | On My GitHub | Provider Pattern | Providers
 Tuesday, May 06, 2014

If you are receiving this error regularly or you HTC One X plus is dropping calls randomly it may be due to the poor SIM card cradle design in the phone not holding your SIM card contacts against the phone contacts correctly.

Thanks to advice found elsewhere on the internet, I have applied a fix to my phone which I'd like to share with others.

process

  • Turn off your phone
  • Use a SIM removal tool or a straightened paper clip to pop the SIM card tray out of the phone
  • Remove the SIM card from the tray
  • Selotape three layers of tape carefully to the back of the SIM (not the side with electrical contacts). Make sure there are no air bubbles
  • Very carefully with a sharp craft knife or Stanley knife, cut the selotape to match the profile of the SIM card
  • Replace the SIM card in the SIM card tray.
  • Replace the SIM card tray in the phone.

This process will now have made the SIM card around 120-250 microns thicker, depending upon the tape you used. That should help hold the contacts of your SIM cars against the contacts of the phone much better

Good luck!

Tuesday, May 06, 2014 5:56:00 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
HTC | One X Plus | SIM Card
 Friday, April 11, 2014

IEnumerableExtensions.ForEach<T>


I recently required to be able to iterate through an IEnumerable object and perform an action so have built a simple extension for the IEnumerable class.

using System;
using System.Collections.Generic;

namespace Dibware.Extensions.System.Collections
{
    /// <summary>
    /// Provides extension methods to the IEnumerable class
    /// </summary>
    public static class IEnumerableExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="action"></param>
        public static void ForEach<T>(this IEnumerable<T> source,
            Action<T> action)
        {
            foreach (T element in source)
            {
                action(element);
            }
        }
    }
}

Unit Tests


using Dibware.Extensions.System.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Dibware.Extensions.Tests.Collections
{
    /// <summary>
    /// Tests extensions to the IEnumerable class
    /// </summary>
    [TestClass]
    public class IEnumerableExtensionsTests
    {
        #region ForEach

        [TestMethod]
        public void Test_ForEach_PerformActionOnListUsingForEach_ResultsIn_TheActionBeingPerformed()
        {
            // Arrange
            IEnumerable<String> list =
                new List<String>() { "One", "Two", "Three" };
            var result = String.Empty;

            // Act
            // Create an action that concatenates each item in 
            // the list using commas.  A Trailing comma will be present.
            Action<String> action = s => result = String.Format("{0}{1},", result, s);

            // Perform the action on the list using the ForEach we are testing
            list.ForEach(action);

            // Remove the last comma
            result = result.TrimEnd(',');

            // Assert
            Assert.AreEqual("One,Two,Three", result);
        }

        #endregion
    }
}

GitHub

This project is available on GitHub, here: https://github.com/dibley1973/Dibware.Extensions

Friday, April 11, 2014 4:39:42 AM (GMT Standard Time, UTC+00:00)  #    Comments [1] -
C# | On My GitHub | Extensions | IEnumerable
Archive
<July 2014>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Blogroll
 Clemens Vasters
 Harry Pierson
Passion * Technology * Ruthless Competence
 Joshua Flanagan
A .NET Software Developer
 Michael Schwarz's Blog
Developing applications on the Microsoft platform since Windows 3.1!
 Omar Shahine
Yet another Microsoft blogger
 Scot GU
Scott Guthrie lives in Seattle and builds a few products for Microsoft
 Scott Hanselman
Programming Life and the Zen of Computers
 Tom Mertens
Tom's corner
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Duane Wingett
Sign In
Statistics
Total Posts: 52
This Year: 9
This Month: 0
This Week: 0
Comments: 49
Themes
Pick a theme:
All Content © 2014, Duane Wingett
DasBlog theme 'Business' created by Christoph De Baene (delarou)