Saturday, August 16, 2014

jQuery Ajax API retry mechanism with promises

This post depicts a simple mechanism for retrying Ajax requests in the event of a failure. The purpose is to give the service "class" a function that wraps a jQuery Ajax call and the number of times the request should be tried. The whole premise works off of jQuery Deferred Objects and Promises. When the service is first called, a general deferred object is created. This object succeeds on the first successful try and fails when the number of failures hit the maximum failures allowed. Additionally, a call back function can be provided which is executed on every single try. The use of deferred objects also simplifies the code required, since the success/failure paths will be similar to making regular Ajax calls, which are inherently deferred objects in jQuery.
 

Sunday, July 6, 2014

Kendo Grid - Drag and Drop to Sort

Example of how to use both the Kendo Grid and Sortable component of the JavaScript web library by Telerik, making a sortable grid that is also groupable by category.

In this sample, records can be rearranged only within their own group.

In a real scenario, this will be wired against a service that will update the ordinal values of records changed in the persistence layer. One advantage that we get if we use Kendo's built in Observable mechanism, is that only records affected by the rearranging will be sent back to the server for processing. For instance, if we consider having an ordered set of ten records, and we rearrange the last two, only those two will be sent across the wire.


Thursday, March 6, 2014

Avoid querying stale data when running integration tests against the database

A common pitfall in doing persistence tests occurs when entities are loaded into memory, persisted, then immediately queried back from the repository.
The default behavior of ORMs is to load the entity from the in-memory cache to spare a query to the database.
This behavior is fine overall but does not fully test the persistence of the entity. Therefore, it's important to evict the entity from the cache in order to force the ORM to re-load the entity from the db. This is the case for ORMs in either Java or .Net environments.

In Spring, if you have properly configured the JPA, you evict entities from the cache by doing the following:

((Session)entityManager.getDelegate()).evict(entity);

You can access the entityManager by getting it from the container as follows:

@PersistenceContext
private EntityManager entityManager;

Below you may find a simple, yet complete example of a persistence integration test with this technique applied:

Thursday, February 27, 2014

Handling multiple jQuery Deferred calls regardless of resolution

This post discusses a mechanism jQuery.whenAll() for making multiple Ajax calls (or really, any jQuery Deferred calls, since that is what the Ajax object is, and getting a resolution for all calls.

This is an alternative to jQuery's built-in Deferred.when(), which short-circuits on rejected resolutions (for example, jQuery Ajax failures resulting from a 400 or 500 level HTTP status) and will not give you subsequent responses.

So, for example a call such as: jQuery.when(jQuery.get('resource/404', 'resource/200')) will not give you a resolution for both requests, only the resolution of the rejected request (the 404 one).

Therefore, a mechanism like jQuery.whenAll() is very useful for situations when you may want the resolution to all of the requests.

The fiddle:

Wednesday, September 18, 2013

Integrating FluentValidation with ASP.NET Web API

In short, we dislike the Validation that comes with MVC and, now, Web Api. No need to go into why here, but things like Attribute dependencies and its clunky API are bad …. m’kay.
Fluent Validation is a popular, open-source, fluent-API based validation engine for .Net that we prefer over the default validation. There are several ways of integrating Web Api with Fluent Validations. 

Some of those are explained here and here.

Initially, we went with a custom implementation of the latter since it hooks right into the Model property of ActionContext of an ActionFilter. This is nice because the Validation action filter would only need to verify that the ModelState.IsValid is true, or else, return an HTTP Bad Request Response (400) with the error messages.

However, we cringed at bringing a custom implementation of this library that includes a lot of extra artifacts and seems to no longer be active.

At one point, we realized that we could hook right into the ArgumentsProperty of the ActionContext. In this case, we can ask the Dependency Injection Container (we use StrucutreMap) for the validator for the type, then execute the validation.
Although we lose the nice ModelState.IsValid hook, we make it up by having a less-magical, more explicit implementation for validation that is easier to maintain and debug.

In order to get the container to get us the right Validator for the type, we need to register all types that Implement the “AbstractValidator<>” interface.

By the way, these Validators are also easily unit testable.

All the necessary, simplified code is found below:

Thursday, November 8, 2012

Simple Dual Combo Box With Knockout JS

A very common web UI task that we are tasked with are dual combo boxes. These are very good if you want to manipulate or assign several items at once.

Below is a working sample of a Knockout JS way of doing one of these. I really like the result of using an MVVM framework to do these because the whole select -> option DOM manipuation is completely removed from us, Knockout takes care of that part. As a result, we are no longer using tons of jQuery magic; instead, we are dealing with the actual objects that will ultimately be sent over the wire.

 Enjoy
 

Monday, November 5, 2012

Simple Strongly Typed Application Settings using StructureMap (à la Fubu MVC)


In this post, I will describe a simple method of achieving strongly typed application settings from your app config source (app.config, web.config, appSettings.config). There are many ways out there to achieve this, but having dived into Fubu MVC for over a year, I grew fond of the way they handle application settings through a class instead of using ConfigurationManager or a custom config section. In addition, even though the code below is not what you would find in the Fubu source, it is inspired and based on it. So, the credit for the idea and the basic implementation belongs to the folks who actively contribute to it. Finally, this post is based on StructureMap but I am sure that you can achieve the same effect using your favorite DI tool.

Purpose: To rid your code of the soup of ConfigurationManager calls and Magic Strings that is all too common in code.

The composition of this feature is very simple and is exclusively handled by StructureMap.

By inspecting the console code, we see a bootstrapping call.  In this call, we see the initialization block for StructureMap, with an additional custom convention called SettingsScanner.

SettingsScanner registers all the types in the assembly that end with name ‘Settings,’ setting all of its public properties with their matching appSettings values.

Get the entire code from GitHub



Food for thought


One important feature missing from this simple implementation is support for addional types; specifically, integers. Currently, this will only support string types which is what we get by default from .Net.