Yankovic 1, Gaga 0

So, Weird Al requested permission from Lady Gaga to parody her song “Born this Way”, and after giving up his family vacation and burning the midnight oil for days just to get a recording into her hands, he was denied. After which, in a move that basically proves his balls will need their own casket when he dies, Weird Al released his newly minted song for free on YouTube because parodies are covered under the Fair Use doctrine. The Internet became enraged, and Gaga got so much heat that she claims she never heard the parody—that her management made that decision without her input—and has granted Weird Al permission to officially release it on his next album. Awesome.

Nested classes

I’ve been programming against the .NET framework for a while, and I confess that I haven’t much used nested classes. I’ve just never had a compelling need. But today I was working on some unit tests, and found a nice example of how nested classes can be very helpful, and even make normal classes easier to use. It all began with DateTime.Now. It is very hard–no, impossible–to test objects that rely on DateTime.Now in a consistent way, because you have no direct control over the value of “Now”. It is actually an external dependency, and like all external dependencies it is best to abstract it away for testing purposes. So I created this rather mundane interface and a simple implementation class that I could pass to any object that needed to use the value of “Now”. [csharp] internal interface ISundial { DateTime Now(); DateTime Today(); } internal class Sundial : ISundial { public DateTime Now() { return DateTime.Now; } public DateTime Today() { return DateTime.Today; } } [/csharp] I know it smacks of over-engineering, but bear with me: this is just a preamble to real content. Anyway, so now I have a nice ISundial interface which I can use for creating a FakeSundial object in my unit test. [csharp] internal class FakeSundial : ISundial { private DateTime _now; public FakeSundial(DateTime now) { _now = now; } public DateTime Now() { return _now; } public DateTime Today() { return _now.Date; } } [/csharp] This worked great for a number of tests. I could guarantee that “Now” would be any value that I needed it to be, and then I could perform assertions to verify the results of date calculations, for example. Some tests failed, however, and I immediately knew what was wrong. The Now() method, by design, never advanced with the system clock, whereas DateTime.Now does. This caused me some headaches, so I added a few convenience methods to simulate the passage of time. [csharp] internal class FakeSundial : ISundial { private DateTime _now; public FakeSundial(DateTime now) { _now = now; } public DateTime Now() { return _now; } public DateTime Today() { return _now.Date; } public void FastFowardSeconds(int seconds) { _now = _now.AddSeconds(seconds); } public void FastForwardMinutes(int minutes) { _now = _now.AddMinutes(minutes); } //etc. } [/csharp] This worked great and gave me the flexibility that I needed, but I still had to implement FastForwardMilliseconds(), FastForwardHours(), FastForwardDays(), FastForwardYears(), and all the damn RewindXYZ() methods that I knew I was going to need as well. I began to alter my design a little bit. Perhaps I could just have generic FastForward() and Rewind() methods that took an int argument and some kind of enumeration to represent the various time measurements, and then build a glorious switch statement that would call the appropriate methods on the DateTime field. But that, too, was rather kludgy, and not at all befitting for an important class such as FakeSundial. Then I thought: wouldn’t it be nice if I could use a kind of fluent syntax to accomplish this same thing, something like: [csharp] var fakeSundial = new FakeSundial(DateTime.Now); fakeSundial.FastForward(60).Minutes(); [/csharp] Two things were immediately apparent:

  1. FastForward() and Rewind() would need to return an object other than FakeSundial (otherwise, what would calling fakeSundial.Minutes() do on its own?); and
  2. that other object would need access to the private field _now in the FakeSundial instance.

The second point was the impetus for a nested type, because in C#, a class that is declared within another class has full access to the parent class’s private and protected members (much like an instance method can access the private instance variables of its declaring class). A corollary to point #2 is that the nested class would need to be a private class, which would prevent other objects (whether children by inheritance or external to FakeSundial), from accessing the private state data of FakeSundial. This is a good example of Encapsulation in OOP. I decided to call this class SundialDuration: [csharp] private class SundialDuration { private readonly FakeSundial _fakeSundial; private readonly int _incrementValue; public SundialDuration(FakeSundial fakeSundial, int incrementValue) { _fakeSundial = fakeSundial; _incrementValue = incrementValue; } public void Seconds() { _fakeSundial._now = _fakeSundial._now.AddSeconds(_incrementValue); } public void Minutes() { _fakeSundial._now = _fakeSundial._now.AddMinutes(_incrementValue); } public void Hours() { _fakeSundial._now = _fakeSundial._now.AddHours(_incrementValue); } public void Days() { _fakeSundial._now = _fakeSundial._now.AddDays(_incrementValue); } } [/csharp] The class constructor takes an instance of the FakeSundial object, as well as an increment value, and performs the appropriate increment operation when the respective increment method is called. (If the increment value is a negative number, the value of “Now” will move backwards.) The FastForward() and Rewind() methods in FakeSundial cannot have a return type of a private nested object, so I created a public interface that defined the increment methods in SundialDuration, and made it the return type of FastForward() and Rewind(). The full class implementation looks like this: [csharp] internal class FakeSundial : ISundial { private DateTime _now; public FakeSundial(DateTime now) { _now = now; } public DateTime Now() { return _now; } public DateTime Today() { return _now.Date; } public ISundialDuration FastForward(int howMany) { return new SundialDuration(this, howMany); } public ISundialDuration Rewind(int howMany) { return new SundialDuration(this, -howMany); } private class SundialDuration : ISundialDuration { private readonly FakeSundial _fakeSundial; private readonly int _incrementValue; public SundialDuration(FakeSundial fakeSundial, int incrementValue) { _fakeSundial = fakeSundial; _incrementValue = incrementValue; } public void Seconds() { _fakeSundial._now = _fakeSundial._now.AddSeconds(_incrementValue); } public void Minutes() { _fakeSundial._now = _fakeSundial._now.AddMinutes(_incrementValue); } public void Hours() { _fakeSundial._now = _fakeSundial._now.AddHours(_incrementValue); } public void Days() { _fakeSundial._now.AddDays(_incrementValue); } } public interface ISundialDuration { void Seconds(); void Minutes(); void Days(); void Hours(); } } [/csharp] Now, my unit tests can simulate the passage of time with a few method invocations: [csharp] var sundial = new FakeSundial(DateTime.Now); var timeSensitiveObject = new TimeSensitiveObject(sundial); timeSensitiveObject.DoSomething(); //simulate the passage of time! sundial.FastForward(10).Days(); //operating in the future now Assert.AreEqual(10, timeSensitiveObject.DaysElapsed); [/csharp]

New STL ALT.NET business cards

Moo.com offers a fantastic business card service–upload your own images, tweak colors, add text, choose a quantity and checkout. I decided to order a small batch of business cards for STL ALT.NET, and I’m pretty happy with the way they turned out.

STL ALT.NET April meetup

Open source .NET micro web frameworks

Wednesday, April 27, 2011 at 7:00 PM

Micro web frameworks are very thin, very lean libraries for serving web pages that do away with high ceremony configuration and mountains of code in favor of simple templates and dynamic view models. The Sinatra micro web framework has become very popular in the Ruby community, and it has inspired open source developers in the .NET ecosystem to follow suit. We will be talking about two of these frameworks, Jessica and Nancy. Don’t forget, if you want to be eligible for the following raffle prices you must be a member of the meetup group and you must be present!

  • 1 license of EFProf from Hibernating Rhinos
  • 2 copies of Pro .NET 4 Parallel Programming in C# by Adam Freeman

Also, members who choose to give a 5-10 minute lightning talk on a technical topic of their choice will have their name entered into the raffle a second time. Please notify me at least two weeks in advance if you plan to give a lightning talk so I can adjust the schedule accordingly. For location details, visit the meetup page.

STL ALT.NET sponsorships

I am excited to announce that STL ALT.NET has some new official sponsorships, and you know what that means: SWAG. First, our meetup group has become part of the user group programs for APress and Manning Publications. These publishers both offer excellent technical books related to .NET development, and members of STL ALT.NET will receive the following discounts when purchasing books through the meetup group:

  • APress - 35% off printed books, 50% off eBooks
  • Manning - 36% discount on all books

In addition, Manning will occasionally provide books to be used as raffle prices, and both publishers allow meetup members to obtain free copies of their books for review purposes, subject to certain conditions (meetup members can ask me more about these programs if they are interested). EDIT: I just received confirmation that APress will be sending two copies of their new book Pro .NET 4 Parallel Programming in C# by Adam Freeman to be raffled at our April meeting. This book is an awesome follow up to our March presentation, Threading in C#. Second, Ayende Rahien at Hibernating Rhinos has provided our meetup group with three raffle licenses for his .NET profiling products. I use NHProf all the time when I develop with NHibernate – it has become an indispensable tool for me! These licenses will be raffled in the following order:

Trial downloads of each product are available on their respective websites, if you want to give them a spin and see how amazing they are! Finally, Hadi Hariri at JetBrains will be providing a few licenses for their .NET products that will be made available at various times throughout the year. Our first JetBrains raffle will be:

Rules for Winning (Sheen Style)

If you want to be a winner (and who doesn’t?) in one of our raffles, there are a few rules you have to follow:

  1. You have to be a member of the STL ALT.NET group
  2. You have to be present at the meetup when a given item is raffled

The raffles will happen towards the end of the evening, so for those of you who saunter in at 7:15 (you know who you are…), you’ll still have a chance. If you would like an additional chance to win, come prepared to give a 5-10 minute “lightning talk“ on a technical topic of your choice. Please let me know at least two weeks in advance if you plan to present, so I can adjust the schedule accordingly. If you plan to use PowerPoint or have materials to show, please either bring your own laptop or put your materials on a thumb drive, and you can just use my laptop. I want to extend a big thank-you to our sponsors! Shit is getting real now, folks!

Sad day for Borders

Borders Bookstore has long been close to my heart. With good coffee, free Wi-Fi, a relaxing atmosphere, and a great selection of books, it became one of my de facto hangouts for meeting with friends or studying in high school. Unfortunately Borders filed for Chapter 11 yesterday and will be closing 30% of its brick and mortar stores–three of which are in the St. Louis area.

  • Ballwin (1535S-A Manchester Rd.)
  • Chesterfield (2040 Chesterfield Mall)
  • St. Peters (1320 Mid Rivers Mall)

Fortunately my default Borders, the Creve Coeur location, remains open for business, but it’s sad to see the bookstore chain languishing in hard times. Of all the bookstore giants in the area (including Barnes & Noble and Books-a-Million), Borders has, by far, the best computer, philosophy, and literature selections–the three topics I am most interested in. If you want to check the status of Borders stores near you, the Wall Street Journal has published a comprehensive list.