Composite Decorators with StructureMap

While I was developing my Crispin project, I ended up needing to create a bunch of implementations of a single interface, and then use all those implementations at once (for metrics logging). The interface looks like so: public interface IStatisticsWriter { Task WriteCount(string format, params object[] parameters); } And we have a few implementations already: LoggingStatisticsWriter - writes to an ILogger instance StatsdStatisticsWriter - pushes metrics to StatsD InternalStatisticsWriter - aggregates metrics for exposing via Crispin’s api To make all of these be used together, I created a fourth implementation, called CompositeStatisticsWriter (a name I made up, but apparently matches the Gang of Four definition of a composite!...

October 4, 2017 · 3 min

Testing Containers or Test Behaviour, Not Implementation

The trouble with testing containers is that usually the test ends up very tightly coupled to the implementation. Let’s see an example. If we start off with an interface and implementation of a “cache”, which in this case is just going to store a single string value. public interface ICache { string Value { get; set; } } public class Cache { public string Value { get; set; } } We then setup our container (StructureMap in this case) to return the same instance of the cache whenever an ICache is requested:...

September 17, 2017 · 2 min

Serilog LogContext with StructureMap and SimpleInjector

This article has been updated after feedback from .Net Junkie (Godfather of SimpleInjector). I now have a working SimpleInjector implementation of this, and am very appreciative of him for taking the time to help me :) Serilog is one of the main set of libraries I use on a regular basis, and while it is great at logging, it does cause something in our codebase that I am less happy about....

July 28, 2017 · 4 min

Preventing MicroService Boilerplate

One of the downsides to microservices I have found is that I end up repeating the same blocks of code over and over for each service. Not only that, but the project setup is repetitive, as all the services use the Single Project Service and Console method. What do we do in every service? Initialise Serilog. Add a Serilog sink to ElasticSearch for Kibana (but only in non-local config.) Hook/Unhook the AppDomain....

July 17, 2016 · 4 min

Using StructureMap Registries for better separation

When it comes to configuring StructureMap, it supports the use of Registries. Registries support everything that the standard configure method does(new Container(c => { /* */});). There are two main reasons that I use the registries rather then doing all my configuration in the Container’s lambda: separation of concerns (one registry per area of code) and easier testing (which we will go into shortly). The only down side I can see to using registries is that it can scatter your configuration across your codebase - but if you have ReSharper, doing a ‘Find Implementations’ on Registry will find them all for you, so it really isn’t much of a down side....

May 19, 2014 · 3 min