ConfigR: Look Mom, No XML

driving-nohandsI stumbled upon ConfigR a several months ago  on twitter from Glenn Block.  It uses ScriptCS (and Roslyn) and was developed by Adam Ralph.

I finally got around to take it for a test drive on a side project.  It’s really a simple tool that I’ve been waiting for.

Why ConfigR?

The purpose of ConfigR is pretty straight forward.  It allows you to write C# code to define configuration settings which you would normally place in a the appSettings section of a web.config/app.config.

If you have used the appSettings at all, you probably wished they could be typed instead of always having to be strings.

From Adam Ralph, the creator of ConfigR

ConfigR allows you to write your config in C# instead of XML – that’s really all there is to it. From that point on you can use your imagination to go wild in what you do in that C#.

Install via NuGet

Make sure your project is set to be using .NET 4.5 or greater

You need to get ConfigR into your project.  Install via the Package Manager in Visual Studio.

PM> Install-Package ConfigR

Or if you prefer via the NuGet UI

configr2

Web Application

This portion has been updated to reflect the proper way of sing ConfigR with a web application.  Thanks to creator Adam Ralph for clarification below in the comments.

The default naming convention ConfigR uses to automatically load your configuration file is to look for a file in your output directly named Web.csx.

  1. Create a new file in your project named Web.csx
  2. Make sure the Copy to Output Directory is set to Do not copy.

This is different than a console app in which you do want to copy to output dir.  In a web application, we want the Web.csx in the same location as you would normally see the web.config.

ConfigR

I’m going to create two simple configuration settings in our new Web.csx file

As you can expect the Add method is adding items into the ConfigR global configuraiton.  The two configuration settings I’ve created are of different types: boolean and string.

Now in code when we need to access these configuration settings, it couldn’t be any easier.

Loading from Custom Location

Want to load a config file not using the default naming convention? Easy.

Complex Types

You can use complex types defined in your assembly or another referenced assembly.  When using a referenced assembly, you must specify the ScriptCS reference directive to the DLL in bin/ directory.

Here is my type defined in another referenced project called AnotherProject.dll

Here is my new Web.csx.  Again, note the #r ScriptCS reference directive and the using statement.

Now we can get our our complex type.

 

How to get started with CQRS & Event Sourcing

hammernailWhen I first heard about CQRS & Event Sourcing concepts through various blogs and videos, primarily from Greg Young and Udi Dahan, I wanted to apply it everywhere.

It seems really natural to want to take the limited knowledge we have about a new concept or technology and try and apply it to any problem.

Most of us know this is a terrible idea, but we are so tempted to push the concepts on a problem that it just doesn’t fit.

I realize I’m grouping CQRS and Event Sourcing together in this post.  Generally, if you are applying Event Sourcing then you are doing CQRS.  The other way around isn’t always true.

The Problem

In the very beginning I had a difficult time seeing where CQRS and Event Sourcing were best applied.  Again, as a beginner with our new shiny new knowledge we have our blinders on.

We look at any new problem and try to make it fit what we think CQRS and Event Sourcing solves.

There are some pretty good examples scattered around the internet about different situations that CQRS and Event Sourcing have been applied.  You may be able to read these examples and get a better understanding about what the context and how it was a good fit.  Also, you may read horror stories about how it was a complete failure and why.

To be honest, I haven’t read that many horror stories as I assume people are too afraid to admit their failures, although it would be very helpful to the community.

Feeling the Pain

I could read all kinds of blogs about where not to apply CQRS & Event Sourcing, but I need to feel the pain first hand to really understand where it’s best used.

I needed to apply the concepts I read about in a meaningful project.

For me a meaningful project was a side-project I worked on after hours at home that was large enough in scale that wasn’t incredibly trivial.

The learning experience of failure (and success) gave me greater insights and a deeper understanding to the words I’ve previously read from others discussing CQRS & Event Sourcing.

Practice

Start using CQRS and Event Sourcing in a project that is meaningful to you.  Obviously, I’m not recommending it in a project at work.  Do it in on a project where you can fail.  Feel the pain of applying these concepts where they don’t provide enough value or are cumbersome (hint: CRUD).

With real practice, your understanding matures and not everything will look like a nail with your shiny new hammer.

Handling Async Await Exceptions

Async Await Exceptions

Async/Await

There’s a ton of information about async await support in .NET 4.5.  I don’t want to go over all the best practices and gotchas in this post, but I did want to point out one common trend I’ve seen lately, which is handling async await exceptions.

Await or forget about it

Any async method that returns a Task or Task<T>, you should always await the result.

If you do not await, any exceptions that occur in the calling method will be swallowed.  Essentially you are turning the method call into a fire and forget.

Async Await Exceptions Example

Below is a simple example that demonstrates two behaviors of calling await on an async.  

To run the example, just copy and paste in a new console application.  If you run with the debugger, you should notice the following:

  1. MethodCallWithoutAwait will be called, however no exception will be raised in the debugger because await is not called.
  2. MethodCallWithAwait will be called and the exception will be raised in the debugger because await is called.

Async Lambdas

There be dragons!  Next post I’ll explore of using async lambdas and how you need to pay close attention in order to avoid creating a void lambda.