Tuesday, November 4, 2014

a static C# method for deserializing XML to a specific type

public static T Deserialize<T>(string xml)
{
   var xmlReader = XmlReader.Create(new StringReader(xml), new
         XmlReaderSettings());
   var xmlSerializer = new XmlSerializer(typeof(T));
   var deserializedObject = xmlSerializer.Deserialize(xmlReader);
   return (T)deserializedObject;
}

 
 

Use it like so:

var foo = Whatever.Deserialize<Foo>(myXml)

When you deserialize a string of XML in C#, try not to make a brittle implementation.

This...

List<FormField> formFields = new List<FormField>();
XDocument xml = XDocument.Parse(responsePacket);
foreach (XElement element in xml.Root.Elements())
{
   if (element.Name == "Fields")
   {
      foreach (XElement formField in element.Elements())
      {
         var xmlReader = XmlReader.Create(new StringReader(formField.ToString()),
               new XmlReaderSettings());
         var serializer = new XmlSerializer(typeof (FormField));
         var deserializedObject = serializer.Deserialize(xmlReader);
         formFields.Add((FormField)deserializedObject);
      }
   }
}

 
 

...for example, is a little bit better than this...

List<FormField> formFields = new List<FormField>();
XDocument xml = XDocument.Parse(responsePacket);
foreach (XElement element in xml.Root.Elements())
{
   if (element.Name == "Fields")
   {
      foreach (XElement formField in element.Elements())
      {
         string name = null;
         string value = null;
         bool? isToTokenize = null;
         foreach (XElement attribute in formField.Elements())
         {
            if (attribute.Name == "Name") name = attribute.Value;
            if (attribute.Name == "Value") value = attribute.Value;
            if (attribute.Name == "IsToTokenize") isToTokenize =
                  Convert.ToBoolean(attribute.Value);
         }
         if (name != null && value != null && isToTokenize != null)
         {
            formFields.Add(new FormField()
            {
               Name = name,
               Value = value,
               IsToTokenize = (bool) isToTokenize
            });
         }
      }
   }
}

 
 

...as in the first example the FormField type may change shape without breaking the implementation.

I saw a 3D Printer for the first time yesterday.

It was being used to make some promotional materials.

Other things I learned at the same outing:

  1. WOWZA is a place to host streaming video.
  2. A "press packet" in the traditional sense is sort of a resume for a professional company or music band. This will have a paragraph of copy on who the entity is and it will also have contact information in bullet points. Phone number, email address, web site, etc.

Monday, November 3, 2014

Change out a value in XML with Regex!

public static String ChangeStatusAtPayload(String stuff, String status)
{
   var regex = new Regex("<Status>.*</Status>");
   Match match = regex.Match(stuff);
   String piece = match.Value;
   return piece.Replace(statusValue, String.Format("{0}", status));
}

Redirect to another web site from an an MVC Controller.

using System.Web.Mvc;
namespace Foo.Ui.Controllers
{
   public class BarController : Controller
   {
      public ActionResult Index()
      {
         return Redirect("http://www.example.com");
      }
   }
}

Saturday, November 1, 2014

I saw Raif Harik give a talk called "selling CQRS to your team or boss" at this year's Code Camp.

This went a little deeper than what was covered in this other talk I saw at the start of the year which touched on CQRS as well. Immutable events are stored in an event store sequentially and incrementally and if you pool the history of a thing you may get a complete picture of it by building up the history of events. I don't really understand how this works without having actually seen the code for a CQRS project though. There was time when I was working at that place I should really stop dwelling on where this guy who I shouldn't mention by name did a project like this and told us all about it and this girl who I also shouldn't mention by name suggested that if you set enough pieces of tracing paper with different shapes upon them on top of each other that together they would add up to a picture and that in such a manner one could have the equivalent of what Eric Evans might consider an object. Everyone at the place I should let go of thought this was a good analogy and I still have this memory because I'm lost in the past. I digress. In terms of how sell CQRS, the argument is that, per force, an application will have more reads than writes and thus we should make it easy to read (minimizing the complicated SQL joins in reporting) and as easy as it is to write. The most interesting thing Mr. Harik said touched on the difference of opinion between Udi Dahan and Greg Young and their two schools of thought. He suggested that Udi Dahan is all about maintaining both normalized and denormalized schemas in CQRS applications while Greg Young would argue that you might as well not even worry about the normalized schema. In an app with only a denormalized schema, reading is easy and for writing one does have to do some painful stuff, but who cares because 80% of the queries, where performance counts, will be of reading. When I think of that conversation from another time that I should just forget about, that guy I shouldn't mention by name seemed to be doing things the Greg Young way. What follows is a chart on what would be more of an Udi Dahan approach to CQRS. It was in one of the slides that Mr. Harik showed off:

Rename all instances of a variable in a method by renaming the variable just once at the method signature in Visual Studio 2013.

  1. Highlight the variable.
  2. Type the new name. Just the last letter of the new name will be underlined with a little red accent.
  3. Click on the little red accent. A menu should appear giving you an option to "Rename 'alice' to 'bob'" so to speak.
  4. The act will occur!