Secret Santa for busy office

This Christmas is fast approaching and, as office tradition dictates, everyone puts their names in a hat to figure out who they will buy a gift for this year’s Secret Santa portion. But it’s also a busy year, everyone is off at customer sites, working from home or in meetings all day, so how do you all get your name out of the hat?

This year I decided to overcome that hurdle by writing a small dotnetcore application to take everyone’s name, randomly select one recipient for each Santa and then email that Santa to let them know who will pick them up . I needed some requirements for this.

Picking must be random

The person running the application (me) cannot know who is assigned to whom.
Participants must be fed from a file
Starting with dotnetcore
The programming language I use for most solutions is C #, so I knew that I wanted it to be a .NET application. But why don’t I want my application to be cross-platform and use all the nice new tooling. So, as I’ve been doing more often lately, I started the solution in DotNetCore. It is only a small and simple project, it is using new libraries, I do not need long term stability and I wanted it to be cross-platform.

Starting a new console application is as simple as typing inside an empty folder, it creates a new project with a single code file and restores the dependencies.

$ mkdir secretanta
$ cd secretasanta
$ Dotnet new console
Then what should be everyone’s favorite editor, Visual Studio Code, if it is in your PATH then you can type in “Code”. At the command prompt after creating the application, the single point is important because it means “this directory”.

Visual studio code

Visual Studio Code is Microsoft’s cross-platform code editor, you can do a lot of work and add new capabilities through the extension marketplace.

If you open the Program.cs file, you will see a simple Hello World program, which you can either run directly in Visual Studio code, or with “dotnet run” from the command line (or integrated terminal). This will create the program and execute the program. From here you can start building your application, add new category files, install packages from nugget and debugging on the go.

If you need to change the way your application is launched by the debugger so that you can pass in logic (like I need) then you can modify your launching. json file and tweak these settings. Unlike Visual Studio, you can also specify environment variable values ​​when debugging, which makes debugging much easier and you don’t end up messing up your development environment with stray settings.

Inside santa’s sack

So, the project started and a text editor opened and ready to go I was able to apply simultaneously. First, to ensure that a random recipient is selected for each Santa.

All that should have happened was that the people who could be picked up (hats), needed to exclude that pool from the person who is currently being selected and someone else who was previously selected.

Sending e-mail

I wanted the solution to send an email to each Santa telling them who they wanted to buy a present for. One reason for this was that not everyone was in office together, so it was important to ensure that everyone was informed. Although another reason was that I didn’t want to know whose pair it was, it spoils the fun to some extent.

Instead of relying on the 3 rd party library for this simple task I reverted to System.Net.Mail.

This is fine for small applications like this, but you need to be careful if you are working with parallel processes because the SMTP client must completely send an email before taking a request to send it to another. This is likely to create a bottle neck in your application if you are not careful.

Creating a client is very simple and I transferred the SMTP details to a configuration file so that they can be changed easily later (why more shortly). After this it is a matter of setting the subject, sender, recipient and message body and calling Send (or SendMailAsync if you are doing it right).

I wanted to test that the application was sending email, and without sending the email to the recipient. As I decided to use 2 services for email, this is one reason for transferring the SMTP server configuration to a configuration file.

Leave a Reply

Your email address will not be published. Required fields are marked *