Saturday, November 18, 2017

Keep a Changelog

Over the years developers have utilized several methods to keep user updated on the changes occurring with each application release. One approach is to email users upon each application update, this is intrusive and annoying for users. Another is to dump the git commits into a changelog.  Git commits aren't for user consumption :) The best approach is make the changelog an integral part of your application. The single source of truth that's ever present within the application.

Most developers are familiar with Markdown, as most of our repositories contain a file. Markdown is great at using minimal syntax to describe document layout. This makes it an ideal format to use for changelogs as well.

In a recent project we decided to follow the guiding principles from There are a variety of libraries in NuGet that can convert markdown to html, I utilized CommonMark. With a few lines of code I added an easy to follow changelog to my project.

Here is the core code. Note: the changelog.txt should be a, but Github converts markdown to html, making it render in this post as html. In your code, this file would be a .md file not .txt.

Saturday, November 4, 2017

Entity Framework Core - InMemory Db Names in Tests

The new in memory database provider that's available for EF Core is awesome. If you aren't familiar with it, it lives in the "Microsoft.EntityFrameworkCore.InMemory" NuGet package. One of the quirks of using this provider is that it requires a unique database name for each test. This ensures that you don't have state bleed over between tests. The creation of the in memory database usually looks something like this.
 var options = new DbContextOptionsBuilder()
                .UseInMemoryDatabase(databaseName: "Unique_db_name_here")
At first when writing tests I was utilizing a magic string as in the example above. I came back to some unit test where I'd used unique names such as "test1", "test2" etc. After deleting a test and seeing my magic strings get out of sequence, it became apparent that this was going to be a maintainability nightmare. Hmmm, did I use "test6" yet?

Unit tests methods already have unique names, so why not use the test name? With magic strings this approach would be terrible, but with the C# 6.0 nameof operator this approach works well.

This keeps the in memory names unique and keeps refactoring simple.

Saturday, March 11, 2017

Microsoft Surface Book

Late last year I started a new job where I was supplied with a Surface Book. My last job supplied me with a 10 pound Dellasuras, which I was more than glad to leave behind. With all the hype around the Surface Book I was excited to use one on a daily basis as my primary machine. It's now been close to 4 months with the Surface Book.


  • Weight - at roughly four pounds, its close to 5 pounds lighter than my previous laptop.
  • Build - way better than the Dell, and as other people have noticed, on par with an Apple product.
  • Charger - the charger is lightweight and small, includes one USB port on the brick.

  • Display - the display is amazing overall, but this is Windows, so all of those old applications that are programmed against fixed pixels...  I hope you own a good pair of reading glasses or a magnifying glass. It can get pretty ridiculous. 
  • Display & Table - because the display can split off the keyboard base and become a tablet, its heavier than on a normal laptop. This becomes awkward if you ever try to use the Surface Book as a laptop. It's top heavy.
  • Display Drivers - we've noticed at work that all of the Surface Books display some interesting issues when they are docked.  The main issue being that scroll bars in some apps are really fat. It's more annoying than you'd imagine.
  • Price - for the price you can get several laptops that have equivalent or better internal specs. 
  • Pen - it's there, but I never use it.  It gets lost in my backpack about twice a week. 
Overall, the Surface Book is working well for my needs.  Though I'm not sure I'd by one for a personal laptop.

Tuesday, January 31, 2017

Mocking base class methods using Moq

While working with MVC recently, I encountered a situation where my code interacted with the OWIN context. I wanted to avoid pain in my unit tests, but didn't really have time to create an interface and wrapper for this interaction.  I had already encapsulated the OWIN context interaction to a method in my base class.  When I wrote the code I wasn't exactly sure how I was going to test it.

Turns out its pretty simple to unit test this with Moq. Just remember to set CallBase on your mock object.

//Here we are using Moq to over-ride the base class check access method which calls into the owin context.   
var controller = new Mock<UserprofileController>(_userProfileService.Object, 
    _logger.Object) { 
        CallBase = true 
controller.Setup(b => b.CheckAccess(It.IsAny<string>(), It.IsAny<string[]>())).Returns(accessState);   

Friday, November 11, 2016

Running Web.Config Transforms on Build

Web application deployment typically involves transforming the build depending on the build target.  Typically Visual Studio only runs the standard transforms on publish of the web application (publish profiles).  This can be confusing and frustrating as many expect a different behavior.  It seems intuitive that transforms would be ran on build. 

The following is a simple recipe you can follow to ensure that MS Build will run the correct build target transforms on build.  In the steps below you'll notice it is suggested that the Web.Config in the project directory be removed from source control.  This is because each time you change configuration and build, the web config will be updated.   

  1. In Visual Studio, from the Build menu, open Configuration Manager.  At this point you can add build targets for each one of your environments.

Do what make sense, but a typical set of build targets may look something like this :
  • Debug
  • Staging
  • Pre-Production
  • Release

  1. In your Visual Studio Project, add a folder called Config 
    1. Copy your current Web.Config and its children Web.Debug.config & Web.Release.Config into the new Config folder.  Delete the old files and remove them from source control.
    2. Add a new file Web.($BuildTarget).config named to match your additional build targets by copying one of the existing transforms.

  1. Add a pre-build step in Visual Studio under Project > Properties > Build Events where project folder refers to the name of your project folder where the original web configs existed.
    1. xcopy "$(ProjectDir)Config\*.*" "$(SolutionDir)\ProjectFolder" /Y
  2. Unload your Visual Studio Project and edit the .csproj files xml.
    1. Add an AfterBuild target to the very bottom of the file right before the closing </Project> tag

<Target Name="AfterBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''"> 
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />

Now each time a build is triggered, the web-config will be properly transformed.

Friday, October 28, 2016

Software Testing Group Discussion Notes

At the last Duluth .Net User Group meeting we had a group discussion around the topic of testing.  As an extension of that meeting I want to present a collection of information for folks who are just getting familiar with software testing.  The following is an outline of our discussion including links to more in depth articles.

Common Types of Software Testing:
  • Unit Testing
  • Integration Testing
  • End To End Testing
  • Smoke Tests

After briefly discussing the types of test we focused in on Unit Testing.

Unit testing:
We loosely defined unit testing as “tests that can run on a small piece of the code base, isolated from external dependencies”. Every software decision has a trade-off so we tried to list out the benefits of unit testing weighed against its cost.  

  • Regression safety, provides a level of confidence in the code
  • Having tests gives you a tool to put more tests in place when you find bugs
  • Gives other developers a starting place to understand the code
  • Can be used as a part of the continuous delivery feedback loop to know if code is stable
  • Can save time debugging the code
  • Adds overhead to changing code
  • At least doubles the code base
  • Have to maintain test setup code and fake data
  • Adds upfront development time

Unit testing styles can be broken into a few categories as well, defined by looking at how a programmer approaches testing and what tools they use.

Styles of Testing
  • Mockist
    • Make no distinction between mocks and stubs, and use a mocking framework for everything
  • Traditionalist
    • Use stubs when necessary mostly avoiding mocks
  • TDD (Test Driven Development)
    • Follow the guiding principle of writing tests first before any code is written
  • BDD (Behavior Driven Development)
    • Believe that code should be tested focusing on the codes domain language and user requirements
  • Code First - Test Last Developers
    • These developers realize the importance of tests but shun the dogma of TDD & BDD, writing the tests after they solve the problem at hand.

The discussion turned to looking at how architecture of code affects testing.  We found that many of us had worked with code bases where testing was impossible without some level of refactoring.  The conclusion was that code must be designed to be tested.  

Frameworks or Patterns that Make Testing Easier
  • MVC - Model View Controller
  • MVVM - Model View View Model
  • MVP - Model View Presenter
    • All of these patterns separate out the view making testing simpler
  • IOC - Inversion of Control
    • A pattern that allows for easy substitution of code dependencies

The group consensus was that while testing can be tedious and add overhead, it provides a margin of confidence for development that cannot be ignored nor denied.

Dive Deeper!

Saturday, October 15, 2016

Flexible Forms with AlpacaJs

At work recently we decided to implement our own custom forms.  The requirement was that we needed to be able to switch out our standard forms with variants so we could do A-B testing.  We already make use of Formstack for one off custom forms, so we decided to look into what it would take to utilize FS.  As requirements matured we realized FS was not going to work for our needs.

We finally decided on AlpacaJs which is a jQuery plugin for dynamic forms.  It allows us to store a standardized JSON form schema in our database and dynamically then load these forms into our site.  The library is being actively maintained on GitHub check it out!  Alpaca is not without issues, but overall the concept is solid and it's working well for our needs.