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.

Monday, August 22, 2016

Entity Framework Code First - Don't Initialize Navigation Properties

At work we've been using Dapper.Net as our ORM.  For several months I felt that if we were using Entity Framework for our ORM our velocity would increase.  Recently my feelings on this have started to change as I've realized that Dapper doesn't suffer the restrictions of large ORMs.

Entity Framework is too much magic.  Let me provide the most recent example of that magic that wasted 3 hours of my life.

Code first gives us the ability to work with POCOs, which is nice.  Generally, when using EF I turn off lazy loading and proxy creation and I don't use virtual collections.  I new collections up in the constructor.  While I was doing that I also followed the law of least surprise.  Objects should be ready to use when created, so on a POCO I would also new up any child objects, such as the navigation property in the below example.  

//An example of what you should not do!
public class Contact  
   public Contact(){      
     PhoneNumbers = new List<PhoneNumber>();  
     Address = new Address();  
   public int Id { get; set; }  
   public string UserName { get; set; }  
   public List<PhoneNumber> PhoneNumbers {get; set; }  
   public Address Address { get; set; }  

Those of you that use EF all the time probably caught the above error.  Initializing the collection is fine, but initializing the navigation property is a problem.  It breaks EF's builtin relationship fix-up mechanism.  We've assigned an empty object to our navigation property and so now when trying to eager load the things we'll end up with an empty object instead of the expected entity from the db.

The rule then becomes, DO NOT new up navigation properties on your POCOs!  This is just a small example why I'm starting like Dapper more :)

Saturday, August 20, 2016

Pull Request /ˈedəkət,ˈedəˌket/

Over the years I've seen many unproductive threads on the net revolving around projects that are accepting pull requests.  In general when both project maintainers and contributors use their manners everyone stays happier.  When you read some project threads it often seems we aren't very kind to each-other.  Repudiate the code if you must, just don't offend the contributor.  That can be a difficult balance to strike!

The other day while reading in the Thinktecture.IdentityServer3 repo on GitHub, I found a list of links regarding pull request etiquette.  I found them so helpful I wanted to re-share them here.

Have you ever read a Linus Torvalds rant?  If you question whether or not you're interacting with your fellow coders correctly, read one of his rants and then NEVER do react like that!