Posts

MapReduce in C# using Task Parallel Library

Back in August I starting playing with a C# implementation of Google's MapReduce algorithm. The implementation was based on something Stephan Brenner did, although I completely refactored it. Today I added a little bit of logic to split up the actual execution of Map & Reduce in this implementation using the Task Parallel Library in .NET 4.0. Check out the source code for MapReduce in C# on GitHub. Below is an excerpt from the Tests on how to implement the library. Counting Words in Files public static List<KeyValuePair<string, int>> Map(FileInfo document, string text) { var items = text.Split('\n', ' ', '.', ',','\r'); return items.Select(item => new KeyValuePair<string, int>(item, 1)).ToList(); } public static List<int> Reduce(string word, List<int> wordCounts) { if (wordCounts == null) return null; var result = new List<int> { 0 }; foreach (var value in wordC...

Require comment / message on all SVN commits for Visual SVN Server

(I've been posting a lot the last few days!) Something that drives me nuts is not having a comment or message when looking at SVN history. It's impossible to know what went on. Even a bad comment is better than no comment. SVN hooks are a great way to solve this. If we make a pre-commit.bat file that checks the commit data for a comment, returns an error code of 1 if it wasn't found (with some text) or returns 0 if the comment is there. Check out the pre-commit hook that I found online (thanks, Anuj Gakhar!) or check out the hook below. @echo off setlocal rem Subversion sends through the path to the repository and transaction id set REPOS=%1 set TXN=%2 rem check for an empty log message svnlook log %REPOS% -t %TXN% | findstr . > nul if %errorlevel% gtr 0 (goto err) else exit 0 :err echo. 1>&2 echo A log message or comment is required to commit 1>&2 exit 1 This works perfectly except for 1 downfall -- you have to distribute it in every freaking reposito...

Using ref Keyword with Reference Types in C#

One of our developers was using the ref keyword in a method that needed to replace the value of a property of the method's parameter. In other words... public class Person { public int ID { get; set; } public string Name { get; set; } public string Email { get; set; } } public static class ReferenceExample { public static void Main() { var person = new Person { Name = "Mark", Email = "none@none.com", }; Save(ref person); Console.WriteLine(person.ID); Console.ReadLine(); } public static void Save(ref Person person) { person.ID = 1; // Pretend this saves to a database or something } } The thinking is correct. Person needs to be passed by reference. So why not use the ref keyword, right? Well...Person is already a reference type . So if we are just replacing properties, we don't need the ref keyword. Removing it changes nothing with our code. T...

Get the name of a property as a string in C#

Another developer showed me something very useful with Expression Trees in C# (thanks, Pierre! Full credit goes to you). To show you the awesomeness of it all, let's start with a class called Person . public class Person { public string Name { get; set; } public string Email { get; set; } } Now let's say I have an instance of Person var person = new Person { Name = "mark kockerbeck", Email = "noneofyourbusiness@face.com", }; Imagine creating a Dictionary<string,string> of this particular Person's properties (e.g. "Name" and "Email") and the corresponding property values. We could easily do something like the following: var dictionary = new Dictionary<string, string> { { "Name", person.Name }, { "Email", person.Email }, }; But there is unnecessary redundancy and repetition in this. We have to say the property twice (and as a messy string no less!). Well, we could use reflection... var di...

Service Cloud - dynamic calls to a cloud of WCF services

Image
For awhile, myself and another developer have been trying to build a working prototype of an idea he proposed for a Service Cloud. Essentially the Service Cloud is a collection of WCF services hosted on a number of servers and environments that can all communicate with one another via a common interface. An "application" would then be built by orchestrating these services together. Instead of the application adding service references for each of the services it needs -- it would simply send a request to the "Gateway" service and it would figure out the rest. It would call the other services. Finally, when the execution is complete and your request has bounced all over the cloud, you get a response that many services have built together. I finally was able to create a working prototype (albeit completely contrived). The Service Cloud prototype is available on GitHub, see: http://github.com/xeb/ServiceCloud . Client The client is simple. The client adds a WCF Se...

New & Fun vs. Tried & True

I love new technology -- .NET 4.0 and the DLR are a dream come true. I cannot wait to convince other developers of the awesomeness of having IronPython for middleware supported by compiled C# on the backend. But before I go upgrading existing client sites to 4.0, I need to be sure it will work. I just want to see it be successful from start to finish. I am skeptical by nature and want results before jumping in bed with any new tech. Not that I don't think Microsoft did an amazing job with the latest framework -- its just that I'm not as familiar with the "workarounds" that I need to learn for things. And yes, there will be workarounds -- I have yet to see perfection in software ( only a few things come close). From my experience it seems that developers fall into two general categories when faced with new technology decisions. Hopefully they change from one category to another. If you're like me, you change on a project by project basis. New & Fu...

TwoRingBinder - a Custom ASP.NET MVC Model Binder

I haven't posted in a few months. Been busy at work and starting playing WoW (I know -- its awful but its fun). But a few weeks ago I made a Custom ModelBinder -- called TwoRingBinder -- for ASP.NET MVC. Ok, it's not an implementation but rather a class inheriting from DataAnnotationsModelBinder that allows for easier custom Binding methods. I put it on github but will explain how to use it below. Download or check out the meat of the code. Essentially, TwoRingBinder allows you to write Binding Extensions instead of writing your own Custom Model Binder every time you want to do something tricky or different with Model Binding. Example: We have a model called SignUp . SignUp has FirstName, LastName, Origin, IsFemale and FromRoot properties. Only the FirstName and LastName properties will be bound from a form via a ValueProvider; however, we need IsFemale to have a value (assume for a service of some type). Now, we don't want a checkbox on the site for people to se...