technical musings of a caffeine converter


Separating Knockout Viewmodel from View

As mentioned in a previous post, I’m becoming a big fan of Knockout. I don’t favour MVVM over MVC per se, simply like the idea of being able to manipulate, and react to the model changing on the client side without the need for a return server trip every time.

Whilst it is perfectly normal for a Knockout view model to be declared inside the cshtml View to which it is bound, I have found that very quickly, the script can become quite bulky and difficult to maintain.

Take for instance the following script inside a View of mine:

   1: @section Scripts

   2: {

   3:     <script type="text/javascript">

   4:         /// <reference path="../jquery-1.9.1.js" />

   5:         /// <reference path="../knockout-2.2.1.js" />

   6:         /// <reference path="knockout.extensions.js" />

   7:         /// <reference path="../knockout.mapping-latest.js" />

   8:         /// <reference path="../jquery-ui-1.10.2.js" />

   9:         (function () {


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

  12:             viewModel.currentView = ko.observable('company');

  13:             viewModel.editMode = ko.observable(false);

  14:             viewModel.editMode.subscribe(detailsRendering.toggleModelEdit);


  16:             var btnActiveClass = 'btn-success';


  18:             var manageSelection = function () {

  19:                 removeSelection();

  20:                 viewModel.currentView($(this).data("view-id"));

  21:                 addSelection($(this));

  22:             };


  24:             var addSelection = function (element) {

  25:                 element.addClass("btn-primary");

  26:             };


  28:             var removeSelection = function () {

  29:                 $(".detailsSelector").removeClass("btn-primary");                

  30:             };


  32:             var toggleModelEdit = function (edit) {

  33:                 if (edit) {

  34:                     $("#lockModelBtn").removeClass(btnActiveClass);

  35:                     $("#unlockModelBtn").addClass(btnActiveClass);

  36:                 } else {

  37:                     $("#unlockModelBtn").removeClass(btnActiveClass);

  38:                     $("#lockModelBtn").addClass(btnActiveClass); 

  39:                 }

  40:             };


  42:             $(".detailsSelector").on('click', manageSelection);

  43:             $("#lockModelBtn").on('click', function () { viewModel.editMode(false); });

  44:             $("#unlockModelBtn").on('click', function () { viewModel.editMode(true); });


  46:             addSelection($("#companyDetailsSelector"));            


  48:             toggleModelEdit(false);


  50:             ko.applyBindings(viewModel);

  51:         })();

  52:     </script>   

  53: } 

It’s not very complicated but already I am finding it a little fiddly to follow. And, I’m not yet finished with the code, so it’ll only get bigger. I could separate out the areas responsible for initialising the view model from those responsible for reacting to user interaction with multiple script tags but a) that would add extra js code and b) I have a lot of overlap between the two areas of responsibility so scope is an important issue.

What I opted to go with is the Revealing Module Pattern (RMP) that provides a nice separation of concern in simplistic fashion. I created three RMP functions:

1. Initialise View Model

2. Initialise View

3. Handle User Interaction with the View

The resulting script is as follows:

   1: /// <reference path="../jquery-1.9.1.js" />

   2: /// <reference path="../knockout-2.2.1.js" />

   3: /// <reference path="knockout.extensions.js" />

   4: /// <reference path="../knockout.mapping-latest.js" />

   5: /// <reference path="../jquery-ui-1.10.2.js" />

   6: var details = function () {


   8:     var initialiseViewModel = function (data) {

   9:         var viewModel = data;

  10:         viewModel.currentView = ko.observable('company');

  11:         viewModel.editMode = ko.observable(false);

  12:         viewModel.editMode.subscribe(detailsRendering.toggleModelEdit);


  14:         ko.applyBindings(viewModel);


  16:         return viewModel;

  17:     }


  19:     return {

  20:         initialiseViewModel: initialiseViewModel

  21:     };

  22: }();


  24: var detailsView = function () {


  26:     wireEvents = function (vm) {

  27:         $(".detailsSelector").on('click', function () { detailsRendering.manageSelection(vm, $(this)); });

  28:         $("#lockModelBtn").on('click', function () { vm.editMode(false); });

  29:         $("#unlockModelBtn").on('click', function () { vm.editMode(true); });

  30:     },


  32:     initialiseView = function (vm) {


  34:         wireEvents(vm);

  35:         detailsRendering.manageSelection(vm, $("#companyDetailsSelector"));

  36:         detailsRendering.toggleModelEdit(false);

  37:     }


  39:     return {

  40:         initialiseView: initialiseView

  41:     };

  42: }();


  44: var detailsRendering = function () {


  46:     var btnActiveClass = 'btn-success',

  47:     btnPrimaryClass = 'btn-primary',


  49:     addSelection = function (element) {

  50:         element.addClass(btnPrimaryClass);

  51:     },


  53:     removeSelection = function () {

  54:         $(".detailsSelector").removeClass(btnPrimaryClass);

  55:     }


  57:     toggleModelEdit = function (edit) {

  58:         if (edit) {

  59:             $("#lockModelBtn").removeClass(btnActiveClass);

  60:             $("#unlockModelBtn").addClass(btnActiveClass);

  61:         } else {

  62:             $("#unlockModelBtn").removeClass(btnActiveClass);

  63:             $("#lockModelBtn").addClass(btnActiveClass);

  64:         }

  65:     },

  66:     manageSelection = function (viewModel, element) {

  67:          removeSelection();

  68:          viewModel.currentView("view-id"));

  69:          addSelection(element);

  70:      }


  72:     return {        

  73:         toggleModelEdit: toggleModelEdit,

  74:         manageSelection: manageSelection

  75:     };

  76: }();

The first module, “details” (named after the view), providers a function for setting up the view model and returning. It does nothing else and knows of nothing else. The only dependency it has is on the initial input of the model which has come from the server.

The second module, “detailsView”, expects the view model and sets up the components in view from wiring the click events, to setting up the default view by calling into the third module, “detailsRendering”, which again expects the view model as an input and provides the functionality for manipulating the view based on either the user’s input or manually in the case of the detailsView initial setup. These three modules are in the same js file, but could quite easily be separated out, in which case, I’d be wise to use something like require to handle dependencies and also minimise the load.

To get the ball rolling, the html embedded script now looks like this:

   1: @section Scripts

   2: {

   3:     <script src="~/scripts/app/page.details.js"></script>   


   5:     <script type="text/javascript">

   6:         (function () {

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

   8:             detailsView.initialiseView(viewModel);

   9:         })();

  10:     </script>    

  11: }

Rather than returning the view model from the initial function, I could have called the initialiseView method from within but this way, I have the option of making further calls if needs be without the need to chain. Having spent most of my time in C# and Xaml, I’d love to hear from html and javascript guys on what their preferred approach to client side MVVM is.

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.