Monthly Archives: November 2015

dargon-root, an aggregate repository

Time for a short update. I just pushed the-dargon-project/dargon-root which aggregates all of our repositories into one pullable entity. It’s sort of nice.

More importantly, it solves a pain point regarding NuGet, .net’s de-facto package manager. Until we get our hands on dnx (google it, it’s awesome), we’re stuck with HintPaths that break builds if you use a project in more than one solution. Exactly a year ago, I moved us over to a system where every major project shipped as a NuGet package and other projects depended on those packages. I think the benefits of project-to-project references heavily outweighs anything we’d gain from NuGet, so as a next step I’m going to work on overhauling the way we handle packages and CI (probably have a NuGet project in every solution, sort of like Fody).

Weaving with Fody – Creating A Lombok-Style RequiredArgsConstructor in .NET

Dargon’s codebase extensively leverages constructor-based dependency injection.

As a result, much of our codebase looks like this:

using ItzWarty.IO;
using System.IO;

namespace Dargon.Nest.Daemon.Hatchlings {
    public class NestDirectorySynchronizer {
      private readonly IFileSystemProxy fileSystemProxy;
      private readonly DaemonConfiguration daemonConfiguration;
      private readonly NestDirectoryImpl nestDirectory;
      private readonly NestContextFactory nestContextFactory;

      public NestDirectorySynchronizer(IFileSystemProxy fileSystemProxy, DaemonConfiguration daemonConfiguration, NestDirectoryImpl nestDirectory, NestContextFactory nestContextFactory) {
         this.fileSystemProxy = fileSystemProxy;
         this.daemonConfiguration = daemonConfiguration;
         this.nestDirectory = nestDirectory;
         this.nestContextFactory = nestContextFactory;
      }

Yuck, that’s six lines of boilerplate! I once worked on a team that leveraged a tool named Lombok which let you write Java code like….

import lombok.*;

package io.dargon.nest.daemon.hatchlings {
   @RequiredArgsConstructor
   public class NestDirectorySynchronizer {
      private final IFileSystemProxy fileSystemProxy;
      ...
      // no constructor!
   }
}

Nifty, eh? A year ago I started looking for this feature in C#. It didn’t seem to exist… but, over the last few hours, I built it! Those lines of code now look like:

using Fody.Constructors;
using ItzWarty.IO;
using System.IO;

namespace Dargon.Nest.Daemon.Hatchlings {
    [RequiredArgsConstructor]
    public class NestDirectorySynchronizer {
      private readonly IFileSystemProxy fileSystemProxy;
      private readonly DaemonConfiguration daemonConfiguration;
      private readonly NestDirectoryImpl nestDirectory;
      private readonly NestContextFactory nestContextFactory;

The code’s pushed to GitHub at ItzWarty/Constructors now. I’ll write a more in-depth post sometime soon, but the tl;dr is that I leveraged Fody and Mono.Cecil to add the constructors as a post-build process. The code is extremely simple, though it touches on basic CIL (.NET Bytecode) and requires understanding a bit about how C# code gets compiled – it’ll be a fun post!

I’ve also pushed Constructors.Fody to nuget.dargon.io, for those of you who wish to try it out :P

That’s all for now!

Michael.