Skip to content

toddb/DSL-Configuration-Sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

I spent the weekend writing a configuration domain specific language (DSL) for a client interface. This interface was lifted from TopShelf project on codeplex after listening to podcast on MassTransit. It was all pretty straightforward and expected as you would imagine. I want to document for myself the process of setting one up for the future and in the process map the code to the patterns that Fowler is outlining in his new DSL book. If you are wondering Fowler would call this an internal DSL or fluent interface. By the end of this, I should have got through where I did some method chaining, used a strategy pattern and how I think it has helped my in DDD. What I find though is that the configurator code is not simply about writing cleaner, prettier code.

Most importantly, I want to think through how this approach is good for TDD. What I have noticed the most is that by using a fluent interface for the configuration I found that it (a) showed up code smells because I tried to consume code more often and in different ways and (b) helped me refactor other parts of the code because they no longer had dependencies on configuration code.

The problem domain …

My application is minor application living in a windows service. Every five minutes, it polls a webservice to get a list of items that it must return information about. That external system has a key for the item and relates that key to the key for the same item in the internal system. This is a trivial system that then gets information from the internal system, collates it and then sends that back to the external system. The external caches this information and combines it with its own information. Quite simple – not if you looked at the early versions of the code.

For this explanation, the system needs to be configured with incoming and outgoing feeds which in turn need network credentials and proxy settings. Both feeds have the same domain, credentials and proxy settings. Early version worked well until you needed to test them – or extend. What I would also like to do is have tests around logging because this system must prove that when it isn’t receiving data that someone is getting notified (via logging framework)

What I also require are acceptance tests, integration tests and unit tests. To run each of these it can be hard if you can’t turn settings on and off. Unfortunately, Microsoft’s app.config isn’t really that good at handling all these situations. To be fair it is only XML. But I don’t see easy solutions to this and as such I find others and myself manually changing the XML at times to test edge cases. That might be fine at coding time but this leaves little opportunity for automated regression testing. My success criteria was to allow for default values (eg corporate proxy is http:/proxy.mycompany.com), default settings (proxy=on) and custom versions of both (eg no proxy or mock proxy) within the same test run.

The simplicity of the solution: using blocks for configuration

Let’s start with my first consumer code. This is from the code that creates the configuration for the application. This code is pretty simple, although looking at here, it also looks a little wordy. Hopefully you read it as I do. With the new configuration, I’ll have the base url with values from the dotnet config, I’ll run app with a proxy with the values in the dotnet config, I’ll use the network credential values from the dotnet config and finally I’ll use log4net. In short, that’s the configuration for a corporate network going out through a proxy to a external address which is secure behind credentials.

  var configuration = AppConfigurator.New(cfg =>
                          {
                              cfg.BaseUrlFromDotNetConfig();
                              cfg.RunWithProxyFromDotNetConfig();
                              cfg.UseCredentialsFromDotNetConfig();
                              cfg.UseLog4Net();
                          });

Just a quick note. For those intellisense-ers out there. Yes, when you hit control-space after cfg dot, you see a list of the configuration settings. Here’s a preview of the settings:

  BaseUrl(url)
  BaseUrlFromDotNetConfig
  RunWithProxyAs(username, password)
  RunWithProxyFromDotNetConfig
  UseCredentialsFromDotNetConfig
  UseCredentials(username, password)
  UseLog4Net
  UseCustomLogger
  OutgoingConnector
  IncomingConnector

Interestingly, this particular configuration is rarely run – this is the production configuration (and will require production values). All I tend to need from this configuration for development is a configurable BaseUrl. I’m generally doing work on the local machine, so I’m not going to need a proxy or credentials to the external service. In this case, I won’t tend to use logging yet. But what I will need is the ability to mock out the external service. But before I go there here is how the configuration is hooked into the application. I pass the configuration into the application. [what I will explain later is that at this point I hand in the configuration values having worked out what they are based on the configuration type.]

  TTLApplication.Run(configuration);

Now to the test. These are far more important for development. In the example below, I want to check that in running my application that the Outgoing connector calls the send method (ie that it sends out). In this case, I use Moq as a framework to mock-out the service. This service will is actually just an Http service makes web requests. The code sets up connector to respond to the Send method, runs the application and then verifies that Send was called.

  [TestMethod]
  public void CanMockOutgoingConnector()
  {
      var connector = new Mock<IOutgoingConnector>();
      connector.Setup(x => x.Send());

      Application.Run(AppConfigurator.New(cfg =>
                        { 
                          cfg.OutgoingConnector(connector.Object)
                        }));
      connector.Verify(x => x.Send(), Times.Exactly(1));
  }

You’ll note in both cases, you only setup the what you need. In the first example, the outgoing connector is setup by default. Whereas in the second, it is only the outgoing connector which is setup with a mock.

This configurator settings are in fact more than what you need. The configurator is really good for creating settings – defaults and combinations – but for unit testing it is easier to get down the setting values. Here’s a simple example of above. While there’s not a lot of difference, the important thing is that you are working through less layers because when it gets more complicated you’ll need a greater understanding.

  [TestMethod]
  public void CanMockOutgoingConnector()
  {
      var connector = new Mock<IOutgoingConnector>();
      connector.Setup(x => x.Send());

      Application.Run(new AppConfiguration
                          {
                              IncomingFleet = null,
                              OutgoingUtilization = connector.Object,
                              Url = "http://example.com"
                          });
      connector.Verify(x => x.Send(), Times.Exactly(1));
  } 

At this point, you should be starting to get the idea that this is an easier way to configure the system for multiple purposes. The next entry will look through the code that implements it.

This is the second entry on creating configuration DSLs in C#. The previous entry looked at the design of the interface. This one looks at the implementation. Just a small note that the configurator does not use an IoC container to solve the problem of dependency injection.

Separating out settings from values: the configurator and the configuration

This design has two main parts to the implementation. The Configurator and the configuration:

  1. Configurator: this wraps the different types of settings and combinations available
  2. Configuration: these are the actual values for a setting

It is important that there is this distinction. The configurator allows you to set defaults or set values based on other values. In many values this allows you to code implicit understandings – or even profiles – of the system.

For example, in the previous entry I talked about three types of profiles: production, acceptance/integration testing and unit testing. The production profile required logging, proxies and credentials to external systems. The tests profiles required stubs and mocks. Now for some code.

Structure of the project

Here’s a quick overview of the code for the basic test application with configuration. If you want a copy of src there’s one at http://github.com/toddb/DSL-Configuration-Sample. The main point is that there is a folder configuration within the core project. In this case, there is an application that accepts the configuration.

	\Core
	  Application.cs
	  ApplicationRunner.cs
	  IApplication.cs
	  IAssetService.cs
	  IConnector.cs
	  IHttpService.cs
	  LogService.cs
	  \Configuration
	    AppConfiguration.cs
	    AppConfigurator.cs
	    Credentials.cs
	    IAppConfiguration.cs
	    IAppConfigurator.cs
	    KnownDotNetConfig.cs
	    Proxy.cs
	\Models
	  Asset.cs
	\Tests
	  \Acceptance
	    ApplicationRunnerTest.cs
	    IndividualUpdateVisualisationTests.cs
	  \Configuration
	    ConfigurationTest.cs
		\Core
	    ApplicationRunnerTest.cs
	    ApplicationTest.cs
	  App.config

The configuration

The configuration class is as you would expect. It requires a number of values. In this case, it just has public getter/setters on each of them. At this stage, the idea is that any profile of settings requires all of these in some form or rather.

using System.Net;
using Core;
using log4net;

namespace Core.Configuration
{
    public class AppConfiguration : IAppConfiguration
    {
        public IAssetService AssetService { get; set;}
        public IConnector IncomingConnector { get; set; }
        public IConnector OutgoingConnector { get; set; }
        public NetworkCredential OutgoingCredentials { get; set; }
        public WebProxy Proxy { get; set;}
        public string BaseUrl { get; set; }
        public ILog Logger { get; set; }
    }
}

The configurator

The configurator is more complex so let’s look at the interface first. It is the list of items that we saw in the first entry. It is important that this interface is as expressive as possible for helping setting up a profile of settings. In this case, you might note that only have one method each for the incoming and outgoing connectors rather than defaults. That is because I would expect people to ignore setting this up explicitly unless they require it. This is implicit knowledge that may be better handled in other ways. There are some others I’ll leave for now.

	using System;
	using Core;
	using log4net;

	namespace Core.Configuration
	{
	    public interface IAppConfigurator : IDisposable
	    {
	        void IncomingConnector(IConnector connector);
	        void OutgoingConnector(IConnector connector);
	        void BaseUrl(string url);
	        void BaseUrlFromDotNetConfig();
	        void UseCredentials(string username, string password);
	        void UseCredentialsFromDotNetConfig();
	        void RunWithNoProxy();
	        void RunWithProxyFromDotNetConfig();
	        void RunWithProxyAs(string username, string password, string domain, string url);
	        void UseLog4Net();
	        void UseLoggerCustom(ILog logService);
	    }
	}

Now that you have seen the interface and hopefully have that in your head, now we’ll look at the implementation because this is the guts of the DSL. Don’t be surprised when you see that there really isn’t anything to it. All of the members which are implementing the interface in this case merely update the configuration. Have a read and I’ll explain the main simple twist after the code.

	using System;
	using System.Configuration;
	using System.Net;
	using log4net;

	namespace Core.Configuration
	{
	    public class AppConfigurator : IAppConfigurator
	    {
	        private NetworkCredential _credentials;
	        private WebProxy _proxy;
	        private string _url;
	        private ILog _logger;
	        private IConnector _incoming;
	        private IConnector _outgoing;

	        AppConfiguration Create()
	        {
	            var cfg = new AppConfiguration
	                          {
	                              IncomingConnector = _incoming,
	                              OutgoingConnector = _outgoing,
	                              Proxy = _proxy,
	                              OutgoingCredentials = _credentials,
	                              BaseUrl = _url,
	                              Logger = _logger
	                          };
	            return cfg;
	        }

	        public static AppConfiguration New(Action<IAppConfigurator> action)
	        {
	            using (var configurator = new AppConfigurator())
	            {
	                action(configurator);
	                return configurator.Create();
	            }
	        }

	        public void IncomingConnector(IConnector connector)
	        {
	            _incoming = connector;
	        }

	        public void OutgoingConnector(IConnector connector)
	        {
	            _outgoing = connector;
	        }

	        public void BaseUrl(string url)
	        {
	            _url = url;
	        }

	        public void BaseUrlFromDotNetConfig()
	        {
	            _url = ConfigurationManager.AppSettings[KnownDotNetConfig.BaseUrl];
	        }

	        public void UseCredentials(string username, string password)
	        {
	            _credentials = Credentials.Custom(username, password);
	        }

	        public void UseCredentialsFromDotNetConfig()
	        {
	            _credentials = Credentials.DotNetConfig;
	        }

	        public void RunWithNoProxy()
	        {
	            _proxy = null;
	        }

	        public void RunWithProxyFromDotNetConfig()
	        {
	            _proxy = Proxy.DotNetConfig;
	        }

	        public void RunWithProxyAs(string username, string password, string domain, string url)
	        {
	            _proxy = Proxy.Custom(username, password, domain, url);
	        }

	        public void UseLog4Net()
	        {
	            _logger = LogService.log;
	        }

	        public void UseLoggerCustom(ILog logService)
	        {
	            _logger = logService;
	        }

	        public void Dispose()
	        {

	        }
	    }
	}

All of that is pretty simple. The basis of the interface is to allow the profile either get settings for the App.Config with the methods suffixed with FromDotNetConfig or pass in their own (eg (username, password) or logService). The code in those classes is also straightforward.

Classes loading from the DotNet Config

This is very straightforward class which just gets the value through the ConfigurationManager or passes through the username and password. This implementation isn’t complex – so you may want to not call ConfigurationManager every time. I’ll leave that implementation to you.

	using System.Configuration;
	using System.Net;

	namespace Core.Configuration
	{
	    public class Credentials
	    {
	        public static NetworkCredential DotNetConfig
	        {
	            get
	            {
	                return new NetworkCredential(ConfigurationManager.AppSettings["Asset.Username"],
	                                             ConfigurationManager.AppSettings["Asset.Password"]);
	            }
	        }

	        public static NetworkCredential Custom(string username, string password)
	        {
	            return new NetworkCredential(username, password);
	        }

	    }
	}

The same approach is used in the Credential class. In this example, it only returns a webproxy if a url was given in the config. Again, here’s another example of implicit profile settings.

	using System;
	using System.Configuration;
	using System.Net;

	namespace Core.Configuration
	{
	    public class Proxy
	    {

	        public static WebProxy DotNetConfig
	        {
	            get
	            {
	                WebProxy proxy = null;
	                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["Proxy.Url"]))
	                {
	                    proxy = new WebProxy(ConfigurationManager.AppSettings["Proxy.Url"], true)
	                                {
	                                    Credentials = new NetworkCredential(
	                                        ConfigurationManager.AppSettings["Proxy.Username"],
	                                        ConfigurationManager.AppSettings["Proxy.Password"],
	                                        ConfigurationManager.AppSettings["Proxy.Domain"])
	                                };
	                }
	                return proxy;
	            }
	        }
	        public static WebProxy Custom(string username, string password, string domain, string url)
	        {
	            return new WebProxy(username, true)
	                       {
	                           Credentials = new NetworkCredential(username, password, domain)
	                       };
	        }

	    }
	}

By now you should have seen all the basic getter/setters for values. How now are profiles of settings done?

Consuming the configurator

The configurator initiation code is also pretty straightforward and this implementation uses C# actions. Let’s step through the code outline above in two methods Create and New.

When you call the configurator, if you remember, it was through AppConfigurator.New(configuration). This returns a configuration. This is a simple pattern of calling a static method that returns an class instance. In this, New creates a new instance of itself which on the method Create returns a configuration. So where is the real work done?

	AppConfiguration Create()
	 {
	     var cfg = new AppConfiguration
	                   {
	                       IncomingConnector = _incoming,
	                       OutgoingConnector = _outgoing,
	                       Proxy = _proxy,
	                       OutgoingCredentials = _credentials,
	                       BaseUrl = _url,
	                       Logger = _logger
	                   };
	     return cfg;
	 }

	 public static AppConfiguration New(Action<IAppConfigurator> action)
	 {
	     using (var configurator = new AppConfigurator())
	     {
	         action(configurator);
	         return configurator.Create();
	     }
	 }

The real work in done in the one line action(configurator). This line calls the action/lambda that you passed in to create values in the configurator and then when Create is called these are merged and returned.

So, back to the original code (see below) the action(configurator) will take each cfg.* and run the method. In this case, the first with run BaseUrlFromDotNetConfig. If that doesn’t make sense, think of it this way, at the point of action in the code, it executes each of the cfg.* lines in the context of that class.

	AppConfigurator.New(cfg =>
                          {
                              cfg.BaseUrlFromDotNetConfig();
                              cfg.RunWithProxyFromDotNetConfig();
                              cfg.UseCredentialsFromDotNetConfig();
                              cfg.UseLog4Net();
                          });

So that’s how to get the configuration available for the application start up. You can get a lot more complex but that should get you started. Personally, if you like this approach then go head off in the direction of libraries doing this: nBehave, NHibernate Fluent interface, StoryQ, codecampserver.

Good luck.

About

A sample application for writing a simple DSL in C#

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages