technical musings of a caffeine converter


Script List missing in IE10 F12 Debugger Tool

Recently had a “where the flip has that gone moment” with the IE debugger tools when I wanted to set a break point on some JavaScript code inside a separate file. Clicking on the Script tag brought up the html code and all the script embedded within in but when I hit “Start Debugging”, I was expected a Script List drop down to appear beside it:

IE10 F12 debugger window

I knew I had seen it there before as a brief search on msdn revealed (for IE8). After wasting looking ‘everywhere’ for it, I finally found the blighter in plain view, just a little more subtle:


Obvious once spotted but thought this might save anyone else a few precious minutes!

Creating DTOs with less code – a good thing?

As the old adage goes, more code equals more bugs. Many OO principles are either based upon or include some basis in writing less code. The reasons are obvious. So when it comes to DTOs, therein lies a little conundrum: writing a class that represents another class (or multiple classes) seems like you’re breaking the rules a little. Take for instance the following three domain model classes (and their base class):


The Company class mostly comprises complex properties (not all shown for brevity), whereas the Address class is all primitive types plus an enum. Contact is mostly primitive types. Just for background info, I’m using these classes within an EF Code First approach so they are not just my domain model but by data objects as well.

Suppose I have a service, CompanyService that delivers up a company (or companies). This service is consumed by an MVC controller. I have a view that displays the following information:

Company Name; Company Created Date, Company Modified Date, Company Primary Address, Principal Contact Full Name, Principal Contact Email Address

I could grab the company, use the whole object as my model and return the view. MVC calculates the required fields on the server side, EF lazy loading ensures that only the properties required are retrieved and only the necessary information gets posted into the View returned. That’s nice as I haven’t had to create any extra DTO classes to flatten my data. However, there are a couple of snags:

1. What happens when I am using the model for editing and I want to place validation attributes on my properties. Most attributes (Required, MaxLength etc.) fall into the System.ComponentModel.DataAnnotations namespace and perhaps it would be fine to use these – especially if you are already using them in a Code First manner to shape the database. But what if you want to use some of the MVC model metadata attributes? Such as ShowForDisplay, Order or IsReadOnly. You do not want a dependency on System.Web.Mvc on your Domain Model.

2. You decide you want to use an API Controller at some point – perhaps to serve some data to a client-side model instead. You grab the same company object and bam, you hit an exception:

The type System.Data.Entity.DynamicProxies.SomeClass_SomeGUID was not expected…

That clever lazy loading, change tracking Entity Framework has caught you out with its dynamic proxy classes. Your options are to disable proxy generation and eager load the child properties you want but how do you know which children you want for each scenario? You don’t. You can’t.

People can use the DRY principle to justify not writing out separate DTO classes but the simple argument is that only a ViewModel can know what needs to be shown, what can be edited and how something may need to be displayed. Granted there is an overlap with validation and certainly there will be times, often in read-only scenarios where there is a 100% mapping between the Domain Object and the DTO. This would be true in the case of the Address class above – an AddressDTO class would have the exact same properties. But in edit mode, I may need more flexibility with validation and other attributes. I may also decide to make all but Name, NameNumber and Postcode read-only and insist on using a postcode address finder utility. But I wouldn’t want the other fields to be read-only on my domain model.

If you are in a scenario where you have multiple user interfaces you may want to consider breaking out your DTO objects into a separate project. If your front-end apps transcend different technologies, it may even be worth using a fluent approach with validators/ behaviours that are specific to that technology should there be one, but separating out a DTO object from its intended target may seem like a DRY principle but caution must be paid as to who is using it and for what as developers will always look to find something that closely matches there needs and often fall into the habit of tacking their own requirements.

In short, I would recommend keeping you DTOs separate for each app. Use a mapping tool like AutoMapper to minimise coding and don’t be afraid to ‘break’ the DRY principle a little – using one DTO for returning search result and another for viewing details means that your search will be faster. If you can use inheritance to enforce a little DRYness to your DTOS, great but don’t get hung up on it.

Best Approach? EF5, Design Patterns, MVC and Knockout

One of the things I particularly like about Silverlight is that its rich client side model enables easy manipulation of data in a decoupled, data-bound fashion without the need to perform projection and/ or switching between to two different languages such as C# and JavaScript. What I don’t enjoy is the fact that in any distributed system that involves Silverlight, you will have to use some sort of asynchronous data-grab architecture so any lazy-loading or change tracking that may have been available on the server side gets lost. When working with MVC however, I find that even though I may have the full capabilities of EF at my disposal within the controller, the likelihood that I will push the same EF domain model classes into my view and few and far between. The main reasons:

1. Views usually only require a subset of the data so I don’t want to bulk up the response with more than I need.

2. JSON serialisation does not handle proxy version of classes designed to handle change tracking.

With point 2, you may come across this if you use Web API controllers and hit the error,

The type System.Data.Entity.DynamicProxies.SomeClass_SomeGUID was not expected…

The reason why point 2 is so important for me is because, coming back to Asp.Net from Silverlight, the desire to mimimise not just the amount of return trips to the server but to minimise the amount of data sent as well is too strong to ignore. With SL, the view is already on the client and all that happens is the data that is required is asked for and given. With vanilla MVC, you can make a request that returns the same view just with different data and sometimes, the data can be a subset of what you already had. So I wanted to get to grasps with an MVC approach that would satisfy these wishes and give me a chance to get to know MVC as it is. To this end, I opted to take a look at KnockoutJs as a) it offers what I am looking for and b) it seems to be the most prevalent of MVVM js libraries out there right now. I’m not saying it’s the best or that there aren’t other approaches but as contractor I need to think not just about keeping ahead of the curve, but, in this instance, getting myself up-to-date with it.

I have spent many an hour looking at various approaches covering all aspects of MVC. From the vanilla flavour to a full blown SPA (courtesy of Mr John Papa), I have settled on something in between – I don’t want a clunky ‘return a view every time’ approach, but I don’t feel ready for the single page approach where both the view and data is dynamically grabbed using JavaScript. I love JavaScript for its flexibility but struggle with it because of its flexibility! We C# developers do enjoy the seat-belt constraints of our strongly typed language :)

Before I had even settled on a web approach, I knew that I would want to be able to target more than one front-end and not just via the web so with that in mind, I wanted to abstract my DAL and not just by using EF. I opted to use a POCO domain model with a Code First approach and encapsulate that within a set of Repositories that themselves were house within a UnitOfWork class. I then created a set of services to manage specific areas of logic (but could still overlap), i.e. a CompanyService responsible for basic CRUD ops as well as more specific methods. It is these services that are inject into my controllers via Ninject. Whilst this abstraction provides me with more testable code and allows me to control what operations are performed and how they are performed, it does mean that I lose the more finer points of some of the EF functionality such as Eager Loading etc. However, I have found in development, if something is available, sooner or later, someone will come along and decide to take it, regardless of whether or not it is always needed. Forcing developers through the route of declaring specific business functionality in the service for their needs or simply allowing them to delay load the required resources as and when required minimises the risk of data bloat where each developer adds on their own specific data needs until everyone is complaining of performance issues.

So my layers up to the UI look a little like this:

Domain Model Entities + other common elements (Interfaces, enums etc.)
EF DbContext DbSet<Company>, Fluent API Configurations
Repositories Using generic DataEntityRepository<T>
Unit Of Work Combines all repositories and provides Commit () method
Services CompanyService – i.e. GetAllCompanies()

note: Services is analogous to BLL, not a web or windows service

So, on to the MVC app. I have taken an empty application and applied the Twitter Bootstrap templating to it. I created my own authentication mechanism through the services, based on Forms Authentication. I added KnockoutJs through Nuget and set about creating two initial views – one to list all companies, and one to view details.

Standard controller approach in MVC would be to call the company service, grab all companies, then either inject the result as the model into the View to return or project the results into a DTO first. But as this means that my model is now static on the client side, if I want to manipulate it, I need to return trip to the server.

Take for instance the following scenario:

1. My Index() method in the home controller returns a list of all companies represented as DTOs.

2. My index view has a search text box to allow the user to filter the companies based on a name value.

3. To filter the companies, the user must enter a value, then hit the filter/ search button.

4. The home controller has an Index (string q) method that gets all companies where the name contains the string represented by q. It then returns the Index view again but with a subset of the original data as the model.

It’s a little clunky, isn’t it?

What I really want is for the filter text box to immediately filter the current list as the user types. And for this, I need my model to be available client side.

There are a couple of approaches to do this with Knockout but first off, I went down a web API with an Ajax call approach:

1. HomeController.Index() returns nothing but the view.

2. A new ApiController is created, CompanyController which contains the method, GetAllCompanies and returns an IEnumerable of the DTO representation of a company

3. Index view references a JavaScript file: vm.index.js, that contains the code to call the web API and set up the view model:

   1: $(function () { 

   2:  $.getJSON("/API/company", function (data) {

   3:     var viewModel = 

   4:     {

   5:          //data

   6:          companies: ko.observableArray(ko.toProtectedObservableItemArray(data)),

   7:          filterText: ko.observable(""),

   8:     }; 


  10:     viewModel.companyCount = ko.computed(function () {

  11:         if (this.companies() == null) {

  12:             return 0;

  13:         }

  14:         return this.companies().length;

  15:     }, viewModel); 


  17:     viewModel.filteredCompanies = ko.computed(function () {

  18:         var filter = this.filterText().toLowerCase();

  19:          if (!filter) {

  20:              return this.companies();

  21:          } else {

  22:              return ko.utils.arrayFilter(this.companies(), function (company) {

  23:                 return >= 0;

  24:              };

  25:          }

  26:      }, viewModel); 

  27:      ko.applyBindings(viewModel); 

  28:  }); 

  29: });

4. Set up Index View to use the Knockout databinding for both the company list:

   1: <table class="table table-striped">

   2:     <thead>

   3:         <tr>

   4:            <th>Name</th>

   5:             <th>Date Created</th>

   6:             <th>No. of Teams</th>

   7:             <th>Principal Contact</th>

   8:         </tr>

   9:     </thead>

  10:     <tbody data-bind="foreach: filteredCompanies">

  11:         <tr>

  12:             <td data-bind="text: name"></td>                                

  13:             <td data-bind="date: dateCreated, dateFormat: 'DD/MM/YY'"></td>                    

  14:             <td data-bind="text: teamCount"></td>                    

  15:             <td data-bind="text: principalContact"></td>                    

  16:         </tr>

  17:     </tbody>

  18: </table>

5. And the filter text:

   1: <input type="text" data-bind="value: filterText, valueUpdate: 'afterkeydown'" placeholder="filter by name" />


Nothing more is needed – the JavaScript grabs the initial data and populates the companies array which in turn populates the filteredCompanies array. The filter text then instigates the refresh of the filteredCompanies array.

This is a much better user experience, not to say more efficient. Granted, we must be careful about how much data we load on to the client side, but this then gives even more weight to using DTOs to minimise the overload on data that is not required.

An area I am not so certain of though is the best approach for getting the data to the client in the first place – here we are using an Ajax call to a web API but firstly, that involves a second trip to the server on top of the initial page request and what about actions where parameters are passed in? Take the details view for instance – it might look something like: /Home/Index/3. The id of 3 would be picked up by the method in the controller, i.e. Index(int id) but how do we get hold of this id were we to make a call through to the corresponding web API method (API/companies/id)?

We could scrape it out of the url I suppose but that seems a little nasty. So perhaps it would be better to allow controller methods to do the necessary work and return the model in the first call and then convert the model into JSON to be used within the client-side JSON:

   1: (function () {

   2:    var viewModel = ko.mapping.fromJS(@Html.Raw(Json.Encode(Model)));

   3:    ko.applyBindings(viewModel);

   4: })();

This allows us to have our client-side model to manipulate, minimises the trips to the server for the initial page load and handles the issue of controller action parameters.

The one downside I have found is that where previously I liked to house my view model code in a separate script file, this doesn’t work well with the above conversion code as it cannot process the server-side directive ‘@Html.Raw(….’ outside of the cshtml file itself.

I would love to hear from anyone else who has a preference on how best to approach this.

Format dates using Knockout JS Custom Bindings with Moment.js

I have spent the past 5 years predominantly within the Xaml arena and whilst I have appreciated the new skills I have learnt in terms of the specific technology, one of the greatest benefits to me has been the coupled approach towards design patterns synonymous with xaml. I’m talking about MVVM, composite applications, and how these, in turn, introduced me to the concepts of IOC, Repository and Unit Of work patterns. Whilst I realise these have been around for a lot longer than Xaml, the prevalence of them in the web forms arena was non-existent, at least for me at the time.

Fast forward 5 years and with MVC, I am fast rekindling my love of Not only is the base framework a significant improvement on web forms, but the explosion of complementary JS frameworks from JQuery to Knockout have really enriched the choice for web developers to pick the right tools for the job at hand.

Recently, I have been looking at Knockout and Web Api with view to minimising the number of repeated page or partial page refreshes when updating data. Using JQuery Ajax to grab data is a great way to remove the overhead of returning page details on top of the actual data itself. Coupled with knockoutjs to provide a intuitive workflow is a natural choice but my first pass demonstrated immediately the need for some formatting:


These came about from the following Knockout syntax:

   1: <tbody data-bind="foreach: companies">

   2:     <tr>

   3:         <td data-bind="text: name"></td>                    

   4:         <td data-bind="text: dateCreated"></td> 

   5:     </tr>

   6: </tbody>


In xaml, where databinding is (relatively) automatic, a simple StringFormat would provide the option here to mould the date required but using knockoutjs, one way I found to manipulate data bindings is using the custom bindings feature coupled with a suitable 3rd party framework if required. Moment.js is an excellent library for manipulating dates. Using the custom binding with knockout and providing the option to pass in a custom format string, I came up with the following:

   1: = {

   2:     update: function (element, valueAccessor, allBindingsAccessor, viewModel) {

   3:         var value = valueAccessor();

   4:         var formatString = allBindingsAccessor().formatString;

   5:         var date = moment(value());

   6:         if (formatString == null) {

   7:             $(element).text(date.format('DD/MM/YY'));

   8:         }

   9:         else {

  10:             $(element).text(date.format(formatString));

  11:         }

  12:     }

  13: };

This will look for the formatString parameter and will default to the english format of dd/mm/yy if it is not present. In my markup, I now have the following:

   1: <td data-bind="date: dateCreated, formatString: 'MM-DD-YY'"></td>                    

   2: <td data-bind="date: dateModified"></td>    

Which gives me the following output:

imageMuch cleaner, although just a start as the next step would be to think about auto-detecting current UI culture for the browser and being able to apply that as the default.

MS Test missing tests

Just a quick post to highlight a bit of a ‘doh!’ moment I had earlier today that may catch out anyone else whilst in half-a-sleep mode. In adding a new service to my solution that encapsulates CRUD functionality for a new entity, I went about adding my new tests to cover all elements – unit tests and integration tests covering areas from validation, to unit of work and repositories. When running through these tests and doing the work required to make them pass, I ended up with one test failing well before I expected to be left with just the one. Digging into the other tests I came across a bunch of tests that were being missed out.

I have JustTest installed from Telerik that gives me the option of testing specific tests via the context menu so I tried this out and, sure enough, the test sprang to life in Telerik’s Unit Test console and promptly failed as it should have done. So why couldn’t MS Test see it?

The answer was simple – I had not declared by class as public. I know my tests have to be declared in a public class but it was such an easy oversight, had I not had another Unit Testing framework to compare against, I may not have spotted it as easily as I did.

Whilst the improvements to the built-in testing framework in VS 2012 Ultimate are welcome, I am surprised that the compiler could not have at least picked up that I had a class decorated with [TestClass] attribute that was not public and given me a warning. Operations that silently fail to run are dangerous to the integrity of your code – it’s like an empty catch block – the silent code killer!

So, if you are using MS Test (this may apply to other frameworks but Just Test from Telerik managed to discover the non public test class), just keep an eye out for your test class decorations.