C# 6 Features – Private Auto Properties

C# 6 is on the horizon with a slew of new features being introduced. One of them that I find pretty cool is the ability to have Auto Properties with an inferred private setter.

It is very typical in my normal development process to create code just like below.

public class Mammal
{
    public Mammal(string type)
    {
        Type = type;
    }

    public string Type { get; private set; }
}

Anyone who has done any C# knows that I just created a auto property with a private setter. But why should I have to go through the ceremony of having the ‘private set;’ in code, why cannot I simply omit the setter and everything just works? Well in C# 6 you can do exactly this. Take a look at the code below.

public class Mammal
{
    public Mammal(string type)
    {
        Type = type;
    }

    public string Type { get; }
}

Notice I simply removed the ‘private set’ code, but I did not touch the code in the constructor which is setting the property and everything still worked. This is a nice little compiler enhancement to make your code a bit more terse. One thing I should point out is that you can ONLY set the setter inside the constructor of the class.

When you look at this feature, I would expect that under the covers the compiler would weave in code which adds back in the private setter. Yea, not so much.

Take a look at the decompiled code below.

Source for the ‘old’ way of doing things

    public class Mammal
    {
        public string Type
        {
            get;
            private set;
        }

        public Mammal(string type)
        {
            this.Type = type;
        }
    }

Source for the ‘new’ way of doing things

    public class Person
    {
        public int Age
        {
            get
            {
                return this.<Age>k__BackingField;
            }
        }

        public Person(int age)
        {
            this.<Age>k__BackingField = age;
        }
    }

If you compare the 2 blocks of code above they are not what I would have expected. I would have expected that the compiler to weave code into the ‘new’ style of doing things to make the code look exactly as the old way. Oh well, it is different but really who cares :)

This new feature is small, and is really just compiler candy, but honestly I like it. It will go a long way in helping to push more people to write immutable code.

Till next time,

Posted in C# | 2 Comments

How Passion Saved Windows

“Don’t worry about people stealing your ideas. If your ideas are any good, you’ll have to ram them down people’s throats.” – Howard H. Aiken

Look around today. There is so much that you can do on Windows with respect to automation that just wasn’t possible a few short years ago. It’s hard to see what has changed because our memories are sometimes so short about how it used to be, so let’s go back about 4 years ago to 2010. PowerShell was still young, there was no Chocolatey, and things like Puppet and Chef didn’t work on Windows yet.

Folks were leaving Windows left and right once they got a taste of how easy automation was in other OS platforms. Well versed folks. Loud folks. Folks that were at the top of their game heading out of Windows. Many others have considered it. You’ve all heard the “leaving .NET stories” from some of the best developers on the .NET platform. But what you may not have realized is that these folks were not just leaving .NET, they were leaving Windows entirely. Some of this was in part to limitations they were finding that just were not there in other OSes. What you are also missing are all the folks that were silently leaving. For every one person speaking out about it, there were many more of the silent losses. The system admins, fed up with GUIs and lack of automation, leaving for greener pastures. The developers who didn’t blog leaving the platform.

But a change has occurred more recently that has slowed that process. I believe it is better tools and automation of the Windows platform. Some people have shown such a passion that they’ve saved Windows as a platform for future generations.

So What Saved Windows?

PowerShellPowerShell – Arguably this could be seen as the catalyst that started it all. It came out in 2006 and while v1 was somewhat limited, v2 (Oct 2009) added huge improvements, including performance. PowerShell is prevalent now, but it had humble beginnings. When Jeffrey Snover saw a need for better automation in Windows, no one understood what he was trying to do. Most folks at Microsoft kept asking, why do you need that? But Jeffrey had such a passion for what was needed that he took a demotion to make it happen. And we are thankful for that because it shaped the face of Windows automation for all. Jeffrey’s passion brought us PowerShell, and it is continuing to bring us more things that have come out of his original Monad Manifesto from 2002.

ChocolateyChocolatey – In 2011 Chocolatey furthered the automation story of Windows with package management, something that other platforms have enjoyed for years. Rob Reynolds’ goals for Chocolatey in the beginning were simply to solve a need but it has since grown into so much more and is now making improvements to become a true package manager. It wasn’t the first approach to package management on Windows and it is certainly not the last. But it did many things right, it didn’t try to achieve lofty goals. It started working at the point of the native installers and official distribution points with a simple approach to packaging and very good helpers to achieve many abilities. When Rob first started working on it, most of his longtime technical friends questioned the relevance of it. Rob did not stop because he had a vision, a passion for making things happen. As his vision has been realized by many he is about to change the face of package management on Windows forever.

Puppet LabsPuppet (and other CM tools) – In 2011 Puppet started working on Windows thanks to Josh Cooper. He single-handedly brought Puppet’s excellent desired state configuration management to Windows (Chef also brought Windows support in 2011). Josh saw a need and convinced folks to try an experiment. That experiment has grown and has brought the last bits of what was needed to save Windows as a platform. His passion for bringing Puppet to Windows has grown into so much more than what it originally started out to do. While most CM tools are good, I’m biased and believe Puppet is arguably the best CM tool for any platform, as the Puppet Labs CEO stated at PuppetConf 2014, Puppet is becoming the lingua franca of infrastructure configuration.

The Effects of Passion

All of this passion for automation has really changed Microsoft. They have adopted automation as a strategy. They are moving to a model of openness, recently announcing that the entire .NET platform is going to be Open Source. They are getting behind Chocolatey with OneGet and getting it built into Windows. They announced PowerShell DSC last year and have made huge improvements in it since then. From where we are sitting, it appears Microsoft now gets it. The effects of passion have really turned the company around and has saved Windows. Windows is becoming the platform we all hoped it would be, it’s really bringing many folks to see it as a true platform for automation and that makes Windows a formidable platform for the foreseeable future.

Posted in chocolatey, puppet | Tagged , | 2 Comments

Hooray for Open Source .NET!

Thinking about this amazing news from Scott Guthrie today. It’s been a long and winding road for .NET. Many of us have moved on completely, most of us now are polyglots. I’m excited for the prospect of solving problems with my familiar .net class libraries in new and different ways.

When I think of my beloved .NET, I often ponder this question – Would I launch a web-oriented startup based on .NET today?

Of course there’s many many factors involved in the answer to this, admittedly unfair question. Web startups are only one very small class of new projects that are started every second, and there are many times when the decision to use .NET is a no-brainer. But to me, this is one of the most interesting questions to ask about .NET today, and one I recently had to ask myself.

Almost a year ago we began work on a new startup in the intermodal shipping space, it’s called Quick180. The stack is Nancy, SQL Azure, EF, and Angular.

So why .NET in the stack? There were many reasons, but to name a few, I’d say low risk because of our existing knowledge of the platform was a big one, and free Azure hosting from BizSpark was another. I also LOVE Nancy.. But I have a nagging feeling that a stack with node would have been an good choice, and the fact that I could easily run on my mac, and just about anywhere else is a big, huge win.

And I know, it’s possible to do .net development in Mono on Mac and elsewhere – Nancy runs on Mono and Mono’s compatibility statement – “Everything in .NET 4.5 except WPF, WWF, and with limited WCF and limited ASP.NET 4.5 async stack.” is a huge mouthful of cross-platformy goodness. But I’ve never come across .net code in the wild that runs cross platform without special care taken to ensure that goal. Other stacks just work de facto cross-platform.

So, perhaps this news gets us one step closer to being on par with other stacks, where I don’t have to boot in to Parallels just to work, and I can kiss my old Windows installs goodbye forever.

Posted in Uncategorized | Leave a comment

Herding Code On Chocolatey

Recently I talked to Herding Code about the kickstarter, package moderation, OneGet, and where we are going with Chocolatey.

Listen now – http://herdingcode.com/herding-code-199-rob-reynolds-on-the-chocolatey-kickstarter-chocolatey-growth-and-oneget/

Posted in chocolatey | Tagged , | Leave a comment

Making a BT Business Hub 3 let an Airport Extreme run the show

This entry will be very localised to the British isles (see what I did there?).

I’ve been a BT customer for a long time, and their hub is an absolute nightmare, a piece of crap, a useless chunk of equipment full of bugs, overheating issues and impossibility to manage even UpnP, across modems and across phone lines, not to mention security issues and random hijacking of DNS to show you some static html page about unpaid bills.

So to try and enjoy the internet for a little while every night, I’ve put together a user story, after a year of shoddy service.

User story:
In order to enjoy the internet reliably,
As a BT Business Customer and an Apple customer
I want to be able to not use the BT Business Hub as a PPPoE modem
and use my Airport Extreme for everything

After hours of going through forums with ridiculous, inaccurate or plain unhelpful advice, I’ve cracked it.

First, plug your airport extreme and configure it’s wifi as you want it. If you have an iOS device, this is dumb simple: Settings, Wifi, configure new airport. Set it to use PPPoE and use the network login username and password you received in a paper letter once upon a time. You’re done, leave the Airport to restart and get ready.

Plug yourself to an ethernet port on the BT Hub. Configure your network adapter to use a static IP in the same netmask (192.168.1.42 sounds reasonable).

Go to the settings (http://192.168.1.254/  usually), and do the following, in order:

  1. Deactivate wireless. That access point has terrible coverage, you’re better off without.
  2. Put the hub in Bridge mode (Settings > Broadband > Routing at the end of the page, Bridge mode enabled)
  3. At that point, you probably already tried that, and failed miserably to PPPoE. So instead go for the hidden page on the router, at http://192.168.1.254/thd_setup and set the encapsulation to LLC. Save

Now you’re all ready, connect the WAN port on your Airport Extreme to an Ethernet port on the BT Business Hub, and see the connection being made.

I’ll be making the same changes in my home flat. If all goes well, I shall finally manage to use the internet properly.

Enjoy!

Posted in Uncategorized | 3 Comments