September 7, 2015

Another Murmur3 implementation - 128 bit output, 64 bit platform version

Another Murmur3 implementation - 128 bit output, 64 bit platform version
/// <summary>
/// Taken from here - http://blog.teamleadnet.com/2012/08/murmurhash3-ultra-fast-hash-algorithm.html
/// but corrected the code and made the class re-usable, the original will only work on the first call
/// There's another version here: http://pastebin.com/aP8aRRHK
/// Corrected the code using code from https://github.com/darrenkopp/murmurhash-net/blob/master/MurmurHash/Unmanaged/Murmur128UnmanagedX64.cs
/// In ProcessBytes both h1 and h2 should be set to the seed
/// 128 bit output, 64 bit platform version
/// </summary>
internal class Murmur3
{
 // 128 bit output, 64 bit platform version
 private static readonly ulong READ_SIZE = 16;
 private static readonly ulong C1 = 0x87c37b91114253d5L;
 private static readonly ulong C2 = 0x4cf5ad432745937fL;

 private ulong length;
 private uint seed = 0; // if want to start with a seed, create a constructor
 private ulong h1;
 private ulong h2;


 /// <summary>
 /// Murmur3 constructor
 /// </summary>
 /// <param name="seed"></param>
 public Murmur3(uint seed = 0)
 {
  this.seed = seed;
 }

 /// <summary>
 /// Compute a hash from an input byte array
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public byte[] ComputeHash(byte[] input)
 {
  ProcessBytes(input);
  return Hash;
 }

 /// <summary>
 /// Create a string hash from an input string
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public string ComputeHash(string input)
 {
  Byte[] inBytes = Encoding.UTF8.GetBytes(input);
  Byte[] hash = this.ComputeHash(inBytes);
  string output = Convert.ToBase64String(hash);
  return output.TrimEnd('='); // There can be up to 2 trailing '=' characters which are just for padding (Not required for a hash)
 }

 #region Private Methods

 
 /// <summary>
 /// 
 /// </summary>
 private byte[] Hash
 {
  get
  {
   h1 ^= length;
   h2 ^= length;

   h1 += h2;
   h2 += h1;

   h1 = Murmur3.MixFinal(h1);
   h2 = Murmur3.MixFinal(h2);

   h1 += h2;
   h2 += h1;

   var hash = new byte[Murmur3.READ_SIZE];

   Array.Copy(BitConverter.GetBytes(h1), 0, hash, 0, 8);
   Array.Copy(BitConverter.GetBytes(h2), 0, hash, 8, 8);

   return hash;
  }
 }
 
 private void MixBody(ulong k1, ulong k2)
 {
  h1 ^= MixKey1(k1);

  h1 = h1.RotateLeft(27);
  h1 += h2;
  h1 = h1*5 + 0x52dce729;

  h2 ^= MixKey2(k2);

  h2 = h2.RotateLeft(31);
  h2 += h1;
  h2 = h2*5 + 0x38495ab5;
 }

 private static ulong MixKey1(ulong k1)
 {
  k1 *= C1;
  k1 = k1.RotateLeft(31);
  k1 *= C2;
  return k1;
 }

 private static ulong MixKey2(ulong k2)
 {
  k2 *= C2;
  k2 = k2.RotateLeft(33);
  k2 *= C1;
  return k2;
 }

 private static ulong MixFinal(ulong k)
 {
  // avalanche bits

  k ^= k >> 33;
  k *= 0xff51afd7ed558ccdL;
  k ^= k >> 33;
  k *= 0xc4ceb9fe1a85ec53L;
  k ^= k >> 33;
  return k;
 }

 private void ProcessBytes(byte[] bb)
 {
  h2 = seed;
  h1 = seed;
  this.length = 0L;

  int pos = 0;
  ulong remaining = (ulong) bb.Length;

  // read 128 bits, 16 bytes, 2 longs in eacy cycle
  while (remaining >= READ_SIZE)
  {
   ulong k1 = bb.GetUInt64(pos);
   pos += 8;

   ulong k2 = bb.GetUInt64(pos);
   pos += 8;

   length += READ_SIZE;
   remaining -= READ_SIZE;

   MixBody(k1, k2);
  }

  // if the input MOD 16 != 0
  if (remaining > 0)
   ProcessBytesRemaining(bb, remaining, pos);
 }

 private void ProcessBytesRemaining(byte[] bb, ulong remaining, int pos)
 {
  ulong k1 = 0;
  ulong k2 = 0;
  length += remaining;

  // little endian (x86) processing
  switch (remaining)
  {
   case 15:
    k2 ^= (ulong) bb[pos + 14] << 48; // fall through
    goto case 14;
   case 14:
    k2 ^= (ulong) bb[pos + 13] << 40; // fall through
    goto case 13;
   case 13:
    k2 ^= (ulong) bb[pos + 12] << 32; // fall through
    goto case 12;
   case 12:
    k2 ^= (ulong) bb[pos + 11] << 24; // fall through
    goto case 11;
   case 11:
    k2 ^= (ulong) bb[pos + 10] << 16; // fall through
    goto case 10;
   case 10:
    k2 ^= (ulong) bb[pos + 9] << 8; // fall through
    goto case 9;
   case 9:
    k2 ^= (ulong) bb[pos + 8]; // fall through
    goto case 8;
   case 8:
    k1 ^= bb.GetUInt64(pos);
    break;
   case 7:
    k1 ^= (ulong) bb[pos + 6] << 48; // fall through
    goto case 6;
   case 6:
    k1 ^= (ulong) bb[pos + 5] << 40; // fall through
    goto case 5;
   case 5:
    k1 ^= (ulong) bb[pos + 4] << 32; // fall through
    goto case 4;
   case 4:
    k1 ^= (ulong) bb[pos + 3] << 24; // fall through
    goto case 3;
   case 3:
    k1 ^= (ulong) bb[pos + 2] << 16; // fall through
    goto case 2;
   case 2:
    k1 ^= (ulong) bb[pos + 1] << 8; // fall through
    goto case 1;
   case 1:
    k1 ^= (ulong) bb[pos]; // fall through
    break;
   default:
    throw new Exception("Something went wrong with remaining bytes calculation.");
  }

  h1 ^= MixKey1(k1);
  h2 ^= MixKey2(k2);
 }

 #endregion Private Methods

}

/// <summary>
/// Some helper functions for reading 64 bit integers from a byte array and rotating unsigned longs:
/// </summary>
public static class IntHelpers
{
 /// <summary>
 /// 
 /// </summary>
 /// <param name="original"></param>
 /// <param name="bits"></param>
 /// <returns></returns>
 public static ulong RotateLeft(this ulong original, int bits)
 {
  return (original << bits) | (original >> (64 - bits));
 }

 /// <summary>
 /// 
 /// </summary>
 /// <param name="original"></param>
 /// <param name="bits"></param>
 /// <returns></returns>
 public static ulong RotateRight(this ulong original, int bits)
 {
  return (original >> bits) | (original << (64 - bits));
 }

 /// <summary>
 /// 
 /// </summary>
 /// <param name="bb"></param>
 /// <param name="pos"></param>
 /// <returns></returns>
 public static unsafe ulong GetUInt64(this byte[] bb, int pos)
 {
  // we only read aligned longs, so a simple casting is enough
  fixed (byte* pbyte = &bb[pos])
  {
   return *((ulong*) pbyte);
  }
 }
}
Unit tests to verify the implementation (but it also shows how to use it).
// Test vectors taken from here: https://github.com/karanlyons/murmurHash3.js/blob/master/tests.html
[Test]
public void Murmur3ValidityTests()
{
 TestVector("I will not buy this tobacconist's, it is scratched.", 0, new ulong[] { 0xd30654abbd8227e3, 0x67d73523f0079673 });
 TestVector("", 0, new ulong[] { 0x0000000000000000, 0x0000000000000000 });
 TestVector("0", 0, new ulong[] { 0x2ac9debed546a380, 0x3a8de9e53c875e09 });
 TestVector("01", 0, new ulong[] { 0x649e4eaa7fc1708e, 0xe6945110230f2ad6 });
 TestVector("012", 0, new ulong[] { 0xce68f60d7c353bdb, 0x00364cd5936bf18a });
 TestVector("0123", 0, new ulong[] { 0x0f95757ce7f38254, 0xb4c67c9e6f12ab4b });
 TestVector("01234", 0, new ulong[] { 0x0f04e459497f3fc1, 0xeccc6223a28dd613 });
 TestVector("012345", 0, new ulong[] { 0x88c0a92586be0a27, 0x81062d6137728244 });
 TestVector("0123456", 0, new ulong[] { 0x13eb9fb82606f7a6, 0xb4ebef492fdef34e });
 TestVector("01234567", 0, new ulong[] { 0x8236039b7387354d, 0xc3369387d8964920 });
 TestVector("012345678", 0, new ulong[] { 0x4c1e87519fe738ba, 0x72a17af899d597f1 });
 TestVector("0123456789", 0, new ulong[] { 0x3f9652ac3effeb24, 0x8027a17cf2990b07 });
 TestVector("0123456789a", 0, new ulong[] { 0x4bc3eacd29d38629, 0x7cb2d9e797da9c92 });
 TestVector("0123456789ab", 0, new ulong[] { 0x66352b8cee9e3ca7, 0xa9edf0b381a8fc58 });
 TestVector("0123456789abc", 0, new ulong[] { 0x5eb2f8db4265931e, 0x801ce853e61d0ab7 });
 TestVector("0123456789abcd", 0, new ulong[] { 0x07a4a014dd59f71a, 0xaaf437854cd22231 });
 TestVector("0123456789abcde", 0, new ulong[] { 0xa62dd5f6c0bf2351, 0x4fccf50c7c544cf0 });
 TestVector("0123456789abcdef", 0, new ulong[] { 0x4be06d94cf4ad1a7, 0x87c35b5c63a708da });
 TestVector("", 1, new ulong[] { 0x4610abe56eff5cb5, 0x51622daa78f83583 });
}

private void TestVector(string key, uint seed, ulong[] hash)
{
 var hasher = new Murmur3(seed);
 Byte[] inBytes = Encoding.UTF8.GetBytes(key);
 Byte[] res = hasher.ComputeHash(inBytes);
 ulong check0 = BitConverter.ToUInt64(res, 0);
 ulong check1 = BitConverter.ToUInt64(res, 8);
 Assert.That(hash[0] == check0);
 Assert.That(hash[1] == check1);
}

September 4, 2015

Using Newtonsoft (Open Source) JSON Serializer

Here is how to serialize and deserialize a JSON object to and from a string
string json = JsonConvert.SerializeObject(target, Formatting.Indented); // Serialize to a string
var reconstituted = JsonConvert.DeserializeObject<ClassOfTarget>(json); // Deserialize from a string
Documentation is here: http://www.newtonsoft.com/json/help/html/Introduction.htm

Can use with attributes:
[DataContract] // ONLY 'marked' properties are serialized in this case
public class ClassOfTarget
{
...
[DataMember] // Use this attribute to 'mark' a property
public Dictionary<string, string> Mappings { get; set; }
...
}
Newtonsoft JSON serializer will recognise these Microsoft attributes and they also have a few of their own they will recognise as well.

Serializing/Deserializing to a file using some extension methods
using Newtonsoft.Json;

public static class JsonExtensions
{

  public static void WriteToJsonFile<T>(
    this T objectToWrite, 
    string filePath
    // This next parameter is optional but ensures that 
    // when you have types mixed with their derived types
    // they can be serialized as well
    bool derivedTypesPresent = false) 
      where T : new()
  {
    FileInfo file = new FileInfo(filePath);
    Debug.Assert(!file.Exists);
    try
    {
      // Serialize JSON directly to a file
      using (StreamWriter sw = new StreamWriter(file.FullName))
      {
        using (JsonWriter writer = new JsonTextWriter(sw))
        {
          JsonSerializer serializer = new JsonSerializer();
          serializer.Formatting = Formatting.Indented;
          if (derivedTypesPresent) // Store type info in JSON
            serializer.TypeNameHandling = TypeNameHandling.All; 

          // target is the object you want serialized
          serializer.Serialize(writer, objectToWrite); 
        }
      }
    }
    catch (JsonSerializationException ex)
    {
        Trace.WriteLine($"Json Serialization Exception occurred: {ex}");
    }
  }

  public static T ReadFromJsonFile<T>(
    this string filePath
    // This next parameter is optional but ensures that 
    // when you have types mixed with their derived types
    // they can be serialized as well
    bool derivedTypesPresent = false)
      where T : class, new()
  {
    FileInfo file = new FileInfo(filePath);
    Debug.Assert(file.Exists);
    T reconstituted = null;

    try
    {
      // deserialize JSON directly from a file
      using (var reader = file.OpenText())
      {
        var serializer = new JsonSerializer();
        if (derivedTypesPresent) // Store type info in JSON
          serializer.TypeNameHandling = TypeNameHandling.All; 
        reconstituted = (T)serializer.Deserialize(reader, typeof(T));
      }
    }
    catch (JsonSerializationException ex)
    {
        Trace.WriteLine($"Json Serialization Exception occurred: {ex}");
    }
    return reconstituted;
  }
  
  public static string WriteToJsonString<T>(
    this T objectToWrite,
    //This parameter is optional but ensures that 
    //when you have types mixed with their derived types
    // they can be serialized as well
    bool derivedTypesPresent = false)
    where T : new()
  {
    string json = "";
    Debug.Assert(objectToWrite != null);
    try
    {
        var serializerSettings = new JsonSerializerSettings();
        serializerSettings.DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate;
        if (derivedTypesPresent) // Then store type info in JSON as well
            serializerSettings.TypeNameHandling = TypeNameHandling.All;

        // Serialize to a string  
        json = JsonConvert.SerializeObject(objectToWrite, Formatting.None); 
    }
    catch (JsonSerializationException ex)
    {
        Trace.WriteLine($"Json Serialization Exception occurred: {ex}");
    }
    return json.ToString();
  }

  public static T ReadFromJsonString<T>(
    this string json,
    bool derivedTypesPresent = false)
    where T : class, new()
  {
    Debug.Assert(!string.IsNullOrWhiteSpace(json));
    T reconstituted = null;

    try
    {
        var serializerSettings = new JsonSerializerSettings();
        serializerSettings.DefaultValueHandling = DefaultValueHandling.
            IgnoreAndPopulate;
        if (derivedTypesPresent) // Then store type info in JSON as well
            serializerSettings.TypeNameHandling = TypeNameHandling.All;

        reconstituted = JsonConvert.DeserializeObject<T>(json, 
            serializerSettings);
    }
    catch (JsonSerializationException ex)
    {
        Trace.WriteLine($"Json Serialization Exception occurred: {ex}");
    }
    return reconstituted;
  }

}
And to serialise and deserialise to and from a file:
StarSystem myObject = new StarSystem(...);
// To write an object to a json file
myObject.WriteToJsonFile<StarSystem>(filePath);
// To read an object from a jsonfile
myObject = filePath.ReadFromJsonFile<StarSystem>(); 
And to serialise and deserialise to and from a string:
StarSystem myObject = new StarSystem(...);
// To write an object to a json string
string json = myObject.WriteToJsonString<StarSystem>();
// To read an object from a json string
myObject = json.ReadFromJsonString<StarSystem>(); 
To convert the JSON to XML, use the JSON string like so
// To convert the JSON to XML
string json = ....
XNode node = JsonConvert.DeserializeXNode(json, "Title");
string xml = node.ToString();

Loading application settings from a custom configuration file

Here is a unit test to show how to load configuration data from a custom configuration file (useful in unit tests)
private string configContents =
 @"<?xml version='1.0' encoding='utf-8'?>" +
 @"<configuration>" +
 @"  <startup>" +
 @"    <supportedRuntime version='v4.0' sku='.NETFramework,Version=v4.5.2'/>" +
 @"  </startup>" +
 @"  <appSettings>" +
 @"" +
 @"    <add key='intValue' value='42' />" +
 @"    <add key='stringValue' value='blah, blah, blah' />" +
 @"" +
 @"  </appSettings>" +
 @"</configuration>";

[Test]
public void RawCustomConfigTest()
{
 // Write out the test config file
 string exeDir = System.IO.Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath);
 string configPath = Path.Combine(exeDir, "Test.config");
 File.WriteAllText(configPath, configContents, Encoding.UTF8);

 // Read it in
 SysConfig.ExeConfigurationFileMap map = new SysConfig.ExeConfigurationFileMap { ExeConfigFilename = configPath };
 SysConfig.Configuration config = SysConfig.ConfigurationManager.OpenMappedExeConfiguration(map, SysConfig.ConfigurationUserLevel.None);
 var appSettings = config.AppSettings.Settings;

 // Read in the test entries
 string intValue = TryGetValue(appSettings, "intValue", "-1");
 string stringValue = TryGetValue(appSettings, "stringValue", "");
 string otherValue = TryGetValue(appSettings, "DoesNotExistInConfig", "not present"); // does not exist in the config file

 // Show that it worked
 Assert.That(intValue == "42");
 Assert.That(stringValue == "blah, blah, blah");
 Assert.That(otherValue == "not present");
}

private static string TryGetValue(SysConfig.KeyValueConfigurationCollection appSettings, string name, string defaultValue)
{
 try
 {
  return appSettings[name].Value;
 }
 catch (NullReferenceException)
 {
  return defaultValue;
 }
}

MemoryCache Extension

The MemoryCache class does not have a "Clear()" method.
Here is  an extension that does the job
// Extend MemoryCache functionality
public static class MemoryCacheExtensions
{
 // Clear the cache
 public static void Clear(this MemoryCache cache)
 {
  List<string> cacheKeys = cache.Select(kvp => kvp.Key).ToList();
  foreach (string cacheKey in cacheKeys)
  {
   cache.Remove(cacheKey);
  }
 }
}


February 12, 2015

Unity

Here are some helpful links: Here are some examples using the following simple classes:
interface ITextService
{
    string Text { get; set; }
}

internal class TextService : ITextService
{
    public string Text { get; set; }
}
With the simplest form of Unity registration, invoking 'Resolve' creates a new instance of the service each time
public void RegisterServiceNewInstanceEachTimeTest()
{
    IUnityContainer myContainer = new UnityContainer();

    myContainer.RegisterType<ITextService, TextService>();
    // Create a TestService instance
    ITextService svc1 = myContainer.Resolve<ITextService>();

    svc1.Text = "Doombar";
    // Creates a new TestService instance
    ITextService svc2 = myContainer.Resolve<ITextService>();
    Assert.AreNotEqual(svc1.Text, svc2.Text, false);
}
Resolve creates a new instance of the service each time however this time the service is registered against a string name or label that can be used to identify and retrieve it
public void RegisterNamedServiceNewInstanceEachTimeTest()
{
    IUnityContainer myContainer = new UnityContainer();

    myContainer.RegisterType<ITextService, TextService>("MyTextService");
    ITextService svc1 = myContainer.Resolve<ITextService>("MyTextService");
    svc1.Text = "Doombar";

    ITextService svc2 = myContainer.Resolve<ITextService>("MyTextService");
    Assert.AreNotEqual(svc1.Text, svc2.Text, false);
    Assert.IsFalse(ReferenceEquals(svc1, svc2));
}
A particular instance of a service object can be registered against the 'ITextService' type, this gives a way of registering singleton objects.
public void RegisterSpecificInstanceForServivceTest()
{
    IUnityContainer myContainer = new UnityContainer();
    TextService svc1 = new TextService();
    svc1.Text = "Doombar";

    myContainer.RegisterInstance<ITextService>(svc1);
    ITextService svc2 = myContainer.Resolve<ITextService>();

    Assert.AreEqual(svc1.Text, svc2.Text, false);
    Assert.IsTrue(ReferenceEquals(svc1, svc2));
}
Injection of service via a constructor First, define a demo class:
internal class ConstructorInjectedClass
{
    ITextService testSvc = null;

    public ConstructorInjectedClass(ITextService mysvc)
    {
        testSvc = mysvc;
        // work with the dependent instance
        testSvc.Text = "Yeehah!";
    }


    public ITextService TestService
    {
        get { return testSvc; }
    }
}
Now use Unity to perform the dependency injection:
public void ConstructorInjectionTest()
{
    IUnityContainer uContainer = new UnityContainer();
    uContainer.RegisterType<ITextService, TextService>();
    // Unity will look at all the objects required to construct the 'ConstructorInjectedClass'
    // object and automatically resolve them
    ConstructorInjectedClass myInstance = uContainer.Resolve<ConstructorInjectedClass>();
    Assert.IsNotNull(myInstance.TestService);
}
Multiple services can also be defined against a single type and retrieved:
public void ResolveAllTest()
{
    IUnityContainer uContainer = new UnityContainer();
    uContainer.RegisterType<ITextService, TextService>("textService1");
    uContainer.RegisterType<ITextService, DifferentTextService>("textService2");
    // Now there are 2 services registered as an ITextService
    // Use 'ResolveAll' to get all the services implementing the named type
    var textServices = uContainer.ResolveAll<ITextService>();
    int ix = 0;
    foreach (var ts in textServices)
    {
        ++ix;
        ts.Text = ix.ToString();
    }
    Assert.AreEqual(2, textServices.Count());
}
Perfect for the case when multiple logger services are used for logging, a debug output logger, a rolling file logger, and so on.

January 28, 2015

Sending EMail Using SMTP in .NET

Here was the old way of sending email using an SMTP email service
using System.Web.Mail; // OLD WAY
...
private void SendEMail()
{
    Common.ResponseHelper rh = new Common.ResponseHelper();
    MailMessage email = new MailMessage();
    email.To = _TargetEMail;
    email.Cc = _CcTargetEMail;
    email.From = _txtFromEMail;
    email.Subject = "EMail Subject";
    email.Body = _txtComments;
    email.Priority = MailPriority.Normal;
    email.BodyFormat = MailFormat.Text; 
    SmtpMail.SmtpServer = "localhost";

    try
    {
        SmtpMail.Send(email);
        rh.WriteComment("Email has been sent successfully");
        Response.Redirect("emailsent.aspx"); // Redirect to an email sent web-page
    }
    catch (Exception ex)
    {
        rh.WriteComment("Send failure: " + ex.ToString());
    }
}
Had to convert this to using the new form
using System.Net.Mail;
...
private void TrySendEMail()
{
    Common.ResponseHelper rh = new Common.ResponseHelper();
    try
    {
        using (MailMessage email = new MailMessage())
        {               
            email.To.Add(_targetEMail);
            email.CC.Add(_ccTargetEMail);
            email.From = new MailAddress(_fromEMailAddress);

            email.Subject = "EMail Subject";
            email.Body = _txtComments;
            email.Priority = MailPriority.Normal;
            email.IsBodyHtml = false;

            using (SmtpClient smtp = new SmtpClient())
            {
                smtp.Host = "localhost";
                smtp.Port = 25;
                smtp.EnableSsl = false;
                NetworkCredential networkCred = new NetworkCredential(
                   _fromEMailAddress, UnmashPassword("oX9iioW7eTX5LS7T"));
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = networkCred;

                smtp.Send(email);
                rh.WriteComment("Email has been sent successfully");
                Response.Redirect("emailsent.aspx");
            }                
        }
    }
    catch (Exception exc)
    {
        rh.WriteComment("Send failure: " + exc.ToString());
    }
}
Had some problems sending email to my googlemail account:
  1. Had to ensure that the email sender address was one that existed
  2. Had to ensure that the "MailMessage.From" property matched the EMail address used for the "NetworkCredentials" constructor.
Without these changes Google rejected my emails (silently).

January 25, 2015

MS-Test Class Template

Here is a template class for an MS-Test unit test class. Pay careful attention to the order of the various test method calls.
[TestClass]
public sealed class VSUnitTestTemplate
{
    // Invoked after AssemblyInitialize
    public VSUnitTestTemplate()
    {
    }

#region Setup/TearDown 
    // Invoked before all tests in an assembly have run
    [AssemblyInitialize]
    public static void AssemblyInitialize(TestContext testContext)
    {
        
    }

    // Invoked after all tests in an assembly have run
    [AssemblyCleanup]
    public static void AssemblyCleanup()
    {

    }

    // Executed before running any tests (but after the constructor)
    [ClassInitialize()]
    public static void ClassInitialize(TestContext context) 
    {

    }

    // Executed after running all the class tests
    [ClassCleanup()]
    public static void ClassCleanup() 
    {

    }

    // Executed before each individual Test
    [TestInitialize]
    public void TestInitialize() 
    {

    }

    // Executed after each individual Test
    [TestCleanup]
    public void TestCleanup() 
    {

    }
#endregion Setup/TearDown        

    // A single test
    [TestMethod]
    public void MyUnitTest()
    {
        // arrange: Setup

        // act: Perform test steps

        // assert: Verify the results
        
    }
    
    // You can add test parameters to a test and use it to test multiple cases 
   [TestMethod]
   [DataRow(5.0d, 0.0d, 3)]
   [DataRow(5.0d, 1.0d, 3)]
   [DataRow(5.0d, 0.0d, 5)]
   public void SomeTest(
     double fullLengthSecs, double startTimeSecs, int expectedValue)
   {
   }
}
From what I have seen in various blogs (see here and MS-Test versus NUnit here) most people recommend sticking to NUnit or some other open-source framework. The reasons are that MS-Test has:
  • Dependence on a test Metadata file.
  • A framework that in itself is quite slow.
  • A dependence on Visual Studio being installed for continous integration testing whereas something like NUnit only needs the NUnit assemblies installed.

January 21, 2015

ASP.NET Web API

According to http://rest.elkstein.org/ : REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used. REST is an architecture style for designing networked applications.

ASP.NET Web API is a framework for building HTTP based services and is ideal for creating RESTful services over HTTP. The best place to start looking for information/tutorials etc. is http://www.asp.net/web-api

Here is how the operations map to HTTP methods:
Operation HTTP Method Relative URI
Get all members GET /api/members
Create a new member POST /api/members
Get member GET /api/members/{username}
Update member PUT /api/members/{username}
Delete member DELETE /api/members/{username}
In general terms then, one could say that the mapping between REST and CRUD is (loosely)
HTTP POST maps to Create
HTTP GET maps to Read
HTTP PUT maps to Update
HTTP DELETE maps to Delete

MVVM/MVC/MVP Explained

There is a nice explanation here

The represent several ways to manage and coordinate the GUI but the important thing is that the model is always segregated from the GUI. Usually the model is described as some database classes but the Model can contain threads and timers and event publishers so this overly simplistic view does not do it any justice. I prefer to view the model as an API on the business logic. A good way to create a model is using test classes from a test framework (such as NUnit or MS-Test). Using a GUI framework is a dangerous approach because it encourages the use of GUI objects within the model, even a simple message dialog within some model code can throw a spanner in the works so to speak.

The View represents the UI components such as CSS, jQuery, html, WinForms, WPF, Silverlight, XAML

January 13, 2015

Reactive Extensions

Here is a good introduction to the main interfaces: http://introtorx.com/Content/v1.0.10621.0/02_KeyTypes.html
IObserver<T> - Reader/Consumer/Observer
IObservable<T> - Writer/Publisher/Observable sequence. [I find this interface name confusing.]
ISubject<in TSource, out TResult> - Represents an object that is both an observable sequence as well as an observer. Used in (non-reactive) samples but not in real situations.
In comparison to IEnumerable<T> where items are pulled out, in an IObservable<T> items are pushed in
A simple sample: Consider an image processing system in a factory that scans parts for results. The outcome of the scan could be one of three states
public enum ImageProcessingSystemResult
{
    Failed, // failed image processing check
    Passed, // succeeded  image processing check
    Indeterminate
}
Here is the publisher of the image processing system results:
// Publishes a sequence of ImageProcessingSystemResult to the observer
public class ImageProcessingSystemPublisher : IObservable<ImageProcessingSystemResult>
{
    public IDisposable Subscribe(IObserver<ImageProcessingSystemResult> observer)
    {
        // These results in this case are just published one after another
        observer.OnNext(ImageProcessingSystemResult.Passed); 
        observer.OnNext(ImageProcessingSystemResult.Passed);
        observer.OnNext(ImageProcessingSystemResult.Failed);
        observer.OnNext(ImageProcessingSystemResult.Passed);
        observer.OnCompleted();
        return Disposable.Empty;
    }
}
Here is a generic results observer:
// Reads/Observes a sequence of T and decides what to do with it
// Basically, what you want to do when you receive some data
public class MyObserver<T> : IObserver<T>
{
    public void OnNext(T value)
    {
        string output = value != null ? value.ToString() : "null";
        Console.WriteLine("Received value: {0}", output);
    }
    public void OnError(Exception error)
    {
        Console.WriteLine("Sequence faulted with exception: {0}", error.ToString());
    }
    public void OnCompleted()
    {
        Console.WriteLine("Sequence terminated");
    }
}
Here is how it all goes together:
public void UnreactiveSimpleTest()
{
    var imageProcessingSystemPublisher = new ImageProcessingSystemPublisher();
    var observer = new MyObserver<ImageProcessingSystemResult>();
    // Here is how the observer subscribes to the publisher
    using (imageProcessingSystemPublisher.Subscribe(observer))
    { }
}
Here is the output:
Received value: Passed
Received value: Passed
Received value: Failed
Received value: Passed
Sequence terminated

Press any key to continue ...

Hot and Cold Observables (i.e. publishers)


Cold - Sequences that start producing notifications on request (when subscribed to), and each subscriber gets a copy of the same notification stream from the beginning.
Hot - Sequences that are active and produce notifications regardless of subscriptions (just like c# events).

A 'Cold' Observable can be converted to a 'Hot' Observable by using the Publish() extension method. With a hot Observable it is necessary to use "Connect()" to connect to the published sequence. This returns an IDisposable which must be disposed of when the Observable sequence is finished with.

January 7, 2015

Reading Xml Using XDocument

The ideas behind this blog are revealed here
Wanted to read some objects in using XDocument.

Found that the best was to use the explicit cast operators exposed by XDocument. These can be used to protect against attributes and elements that are missing

Also found that the XDocument.Parse() method was an excellent way to use raw XML strings to test the parsing
Consider this class:
internal class Base 
{
 public int Id { get; set; }
 public string Name { get; set; }
 public string Faction { get; set; }
}
Here is some small test XML to parse:
string rawTestXml = @"
<Bases>
  <Base ID=""1""><NAME>Freeport 2</NAME><FACTION>Zoners</FACTION></Base>
  <Base ><NAME>Pacifica</NAME></Base>
</Bases>";
Here is a method to test parsing this XML:
public void TestXDocumentParseUnprotected()
{
    FreelancerData freelancerData = new FreelancerData();

    var xmlDoc = XDocument.Parse(rawTestXml);
    var basesTableRaw = new Dictionary<int, Base>();

    foreach (var bas in xmlDoc.Descendants("Base"))
    {
        var b = new Base()
        {
            Id = Convert.ToInt32(bas.Attribute("ID").Value),
            Name = bas.Element("NAME").Value,
            Faction = bas.Element("FACTION").Value,
        };

        basesTableRaw.Add(b.Id, b);
    }

    Debug.Assert(basesTableRaw.Count == 2);

}
This will throw an exception when parsing the second "Base" element in the Xml string, as this second object is missing an "ID" attribute a NullReferenceException will be thrown by the line to extract it. If the XML data can be guaranteed then this is not a problem. A more robust way to read in the XML is to use the XElement/Xattribute explicit conversion operators:
public void TestXDocumentParseProtected()
{
  var xmlDoc = XDocument.Parse(rawTestXml);
  var basesTableRaw = new Dictionary<int, Base>();

  foreach (var bas in xmlDoc.Descendants("Base"))
  {
    var b = new Base()
    {
      Id = (int?)bas.Attribute("ID") ?? 0,  // 0 when the attribute "ID" is not present
      Name = (string)bas.Element("NAME") ?? "",  // "" when the element "NAME" is not present
      Faction = (string)bas.Element("FACTION"), // null when the element "FACTION" is not present
    };

    basesTableRaw.Add(b.Id, b);
  }

  Debug.Assert(basesTableRaw.Count == 2);
}
However this is allowing malformed objects to be allowed as input. The developer must detect these and decide what to do with them.

Note that the XDocument.Load() method can be used to load a document from a local file system file or from a Url. So this method
public void LoadBases(string rootPath)
{
  string xmlFileName = "Bases.xml";
  string basesPath = Path.Combine(rootPath, xmlFileName);
  var xmlDoc = XDocument.Load(basesPath);
  
  ...
}
can work with a local file:
LoadBases(@"X:\Backup\Documents\flasp\");
or a url:
LoadBases(@"http://www.somewebserver.com/flasp/");