Wednesday, April 05, 2017

Bulletproof fluent interfaces

I attended a Fluent Interface webinar by the inimitable Scott Lilly (see Below is my exercise file and some thoughts around what I think are the benefits of a fluent interface.

A fluent interface gives you more control over the way your methods are called. Scott explains it as a three step process, first instantiate, then chain your methods, and finally execute (ICE). And I will present an example of a report in the classic sense, and one built in the fluent way.

download linqpad file:

Thursday, March 30, 2017

Automapper in action

Automapper is an object to object mapper which takes can help you eliminate the yak shaving step of mapping your datastore object model to your view model.

I put together a demo because I believe it when I see it in action.

Here's a working LinqPad version here:

Or see the code online here...

Friday, March 03, 2017

FSharp XML Type provider in action

This is a working sample of an XML type provider. It's a simple scrip. Though Linqpad does crash when dealing with it. Saves a lot of time when writing a quick script to parse an xml document.

Download the linqpad file:

Thursday, February 09, 2017

A method to get a sitecore item id given a path

This method will give you a sitecore item id back if you give it a fully qualified path as input.

void Main()
 var itemId= GetItemId("/sitecore/content").Dump(); 
 //get child items
 //Items.Where (i => i.ParentID==itemId).Select (i => new {i.ID, i.Name}).Dump();
 //show field information
 Fields.Where (f => f.ItemId==itemId && f.Value!="")
  .Select (f => new 
   FieldName=Items.Where (i => i.ID==f.FieldId).Select (i => i.Name).Single (),

Guid GetItemId(string itemPath)
  var pathParts = itemPath.Split(new char[] { '/'}, StringSplitOptions.RemoveEmptyEntries ); 
  var parentId = Guid.Empty;
  var id = Guid.Empty;
  foreach (var part in pathParts)
   if (parentId == Guid.Empty && part.Equals("sitecore", StringComparison.OrdinalIgnoreCase))
     parentId = new Guid("{11111111-1111-1111-1111-111111111111}");
  id = parentId;
    id = GetChildGuid(part, parentId);
    if (id != Guid.Empty)
     parentId = id;
     String.Format("Could not find {0}", part).Dump();
  return id;
Guid GetChildGuid (string childName, Guid parent_guid)
  return Items.Where(i => i.ParentID == parent_guid && i.Name == childName)
   .Select(i => i.ID)

Wednesday, January 18, 2017

How to flatten a list of lists into a flat list in one line of code: SelectMany is your friend

Given that you have a list of lists, how do you flatten it?

how do you flatten it?

With the SelectMany LINQ method, it's easy peasy:

var listOfLists = new List<IEnumerable<int>>() { Enumerable.Range(1, 3), Enumerable.Range(20, 3) };
listOfLists.Dump("List of lists");
listOfLists.SelectMany(lol => lol).Dump("Flat list");

Download linqpad sample, here