I attended a Fluent Interface webinar by the inimitable Scott Lilly (see http://scottlilly.com/FIWebinar). 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: http://share.linqpad.net/w5gdub.linq
Wednesday, April 05, 2017
Thursday, March 30, 2017
Automapper in action
Automapper is an object to object mapper which helps to eliminate yak shaving. It can save you from the tedious task of creating custom code to convert data-store object models 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: http://share.linqpad.net/neu8bm.linq
Or see the code online here...
I put together a demo because I believe it when I see it in action.
Here's a working LinqPad version here: http://share.linqpad.net/neu8bm.linq
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: http://share.linqpad.net/xlrrfl.linq
Download the linqpad file: http://share.linqpad.net/xlrrfl.linq
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 (), f.Value, f.Language }) .Dump(); } 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; } else { id = GetChildGuid(part, parentId); if (id != Guid.Empty) parentId = id; else { String.Format("Could not find {0}", part).Dump(); break; } } } return id; } Guid GetChildGuid (string childName, Guid parent_guid) { return Items.Where(i => i.ParentID == parent_guid && i.Name == childName) .Select(i => i.ID) .FirstOrDefault(); }
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?
Download linqpad sample, here
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");
Friday, January 13, 2017
Assigning lambda expression to a variable
In the spirit of writing terse code, I was pleasantly surprised that you can assign a lambda expression to a local variable. A quick sample:
Results:
in Func<string, string>, the first string specifies the input parameter's data type, while the last string is for the return type. If you have more than one input parameter, you'd have multiple parameters specified like this <string, string, string, string> and the last one is always for the return type of the function.
void Main() { Func<string, string> sayHello = ((input) => { return input + "!!"; }); System.Diagnostics.Debug.WriteLine(sayHello("hello world")); }
Results:
hello world!!
in Func<string, string>, the first string specifies the input parameter's data type, while the last string is for the return type. If you have more than one input parameter, you'd have multiple parameters specified like this <string, string, string, string> and the last one is always for the return type of the function.
Wednesday, January 11, 2017
My FSharp patter matching code kata
Given a list of numbers, detect when the values 2,3,4 are present one after the other.
Thursday, January 05, 2017
Extension method to dump Highlighted search results in Linqpad
Here's an extension method which lets you output the found results as html.
For example, if you wanted to show the result of a search and replace, given:
The result would be:
See extension method below:
For example, if you wanted to show the result of a search and replace, given:
var text = "An apple a day keeps the doctor away\n you can say that any day of the year today or tomorrow."; var find = "day"; var replace = "week"; text.HighlightText(find).Dump("Original text"); text.Replace(find, replace).HighlightText(replace).Dump("updated text");
The result would be:
Original text |
---|
An apple a
day
keeps the doctor away
you can say that any
day
of the year.
|
updated text |
---|
An apple a
week
keeps the doctor away
you can say that any
week
of the year.
|
See extension method below:
Subscribe to:
Posts (Atom)