Implementing a basic Newtonsoft’s JsonConverter

Web programming
I was deserializing an object that requires to be a string at the client but it's actually a structure inside the database, a person's name, saved as a Name { FirstName:'', LastName: '', ... } type of object. By using Newtonsoft's deserializer into a model such model can't just have a string Name as Newtonsoft's deserializer won't know how to parse the object into a simple string, so instead I had to declare the property as the same type of object that the name is saved into the database: PersonName. Now I wanted to transform it into a string when re-serializing it to send it to the client, which could do with a mapper but decided to try Newtonsoft's JsonSerializer attribute to see if it works and to my amazement…
Read More

First steps into Angular 2 from Angular 1

Web programming
With the final version of Angular 2 just been released last month after the Release Candidate was released in May this year, we decided to get into it at our next project in my company and I decided to save some notes to myself in my blog as usual. First of: Angular's official Quick start guide. TypeScript? There are many changes in Angular2 compared with Angular1, one of those is the native possibility to use TypeScript, ES2015, Dart or plain javascript to code your app and transpile that later by just using Angular's config file to set so. This is part of the demo's config file at Angular's website where you can see how TypeScript has been configured to write the app: [js] (function (global) { System.config({ // DEMO ONLY!…
Read More

Setting up Selenium in C#

Software Development, Web programming
1. Download Selenium WebDriver for C# Go to SeleniumHG website and get into Downloads, scroll down until you see the different versions of the WebDriver and get the C# version. Extract the zip files into a folder. Create a project Create a Test Project using VS (File > New > Project > Test > Unit Test). Add the WebDriver libraries: References > Right click > Add Reference... > Browse > Take the right ones for your framework (35 or 40). Note: You can create a different type of project, like a Console App, but this other allows you to get test reports, config different suites of tests, etc. Start testing Now you can start testing, include the namespaces inside OpenQa. For example to test in Chrome: using OpenQA.Selenium; using OpenQA.Selenium.Chrome;…
Read More

Mocking BackEnd with AngularJS

Web programming
I found myself on the need of isolating my FrontEnd AngularJS app from the backend so the app can continue working without Internet access or without access to the backend services. I did not want, though, to add anything too aggressive into the app, but a piece of code that can be attached or unattached very easily and without the app having to know about it at all, like an app extension. To do so, I found myself installing angular-mocks, creating a file that requests the library, attaches the $httpBackend to the app on run time, uses $httpBackend to mock the responses with some json files and forces the requests to be synchronous. Step 1. Installing angular-mocks First of all, to get access to $httpBackend we need to install angular-mocks,…
Read More

Including Google Maps into your AngularJS app

Web programming
I already did this in the past but things change and the plugin I was using has changed too. I use AngularJS with Angular-Bootstrap directives as to save me some time and make the best of Bootstrap stylesheets I'm already using. angular-google-maps is another directive int he same club of bootstrap friends and this is how to include it into your project: Installing packages I'm using bower to manage packages at the front end, so first of all let's install the required packages to make this work. I need the plugin angular-google-maps, which requires angular (obviously), lodash and angular-simple-logger to work. So just make sure you install all required packages using bower: [sourcecode language="js" wraplines="false"] bower install --save-dev angular bower install --save-dev angular-google-maps bower install --save-dev angular-simple-logger bower install --save-dev…
Read More

Managing packages with Bower

Web programming
Installing Bower Just do it globally so you can access it from anywhere. You need to have previously installed Node.js in order to install it, and Git in order to use it. npm install bower -g Have in mind you will also need to install it in your TeamCity server as you'll probably want to get the packages from there too. Installing packages Installing a package with bower is as easy as moving to the root folder of your project using the console and executing something like this: bower install angularjs You can also install a concrete version using # bower install angularjs#1.4.9 Or install more than one package with one command: bower install angularjs jquery bootstrap angular-ui Saving installed packages The main idea of bower is to keep track…
Read More

AngularJS : Service vs provider vs factory

Web programming
Just copy-pasting this answer from Ben Clinkinbeard: Services Syntax: module.service( 'serviceName', function ); Result: When declaring serviceName as an injectable argument you will be provided the actual function reference passed to module.service. Usage: Could be useful for sharing utility functions that are useful to invoke by simply appending () to the injected function reference. Could also be run with injectedArg.call( this ) or similar. Factories Syntax: module.factory( 'factoryName', function ); Result: When declaring factoryName as an injectable argument you will be provided the value that is returned by invoking the function reference passed to module.factory. Usage: Could be useful for returning a 'class' function that can then be new'ed to create instances. Providers Syntax: module.provider( 'providerName', function ); Result: When declaring providerName as an injectable argument you will be provided…
Read More

WebApi and AngularJS: Mapping parameter objects

Web programming
Looks like the parameter behavior between MVC and WebApi is slightly different, mainly because WebApi can only get one parameter from the Body which forces us to use request models. I am working with Angular resources and I have found certain problems to connect to Web API services due to the different way of thinking or structuring the data. Let's see some workarounds: Sending an array of objects If you try to send an Array, in AngularJS you may assign the Body by forming it into parameters with something like this (just because it's the usual way we add them): [sourcecode language="js" wraplines="false"] myResource.post({ myItems: myItems }) [/sourcecode] Which results in a JSON like this: [sourcecode language="js" wraplines="false"] {myItems: [{"Id": 0, "Name": "Blah"}, {"Id": 1, "Name": "Bleh"}]} [/sourcecode] And this…
Read More

Using Google Tag Manager with AngularJS

Software Development, Web programming, Web statistics
One would expect that being Angular a Google product and Google Tag Manager another of their products combining them would be quite simple and straightforward, but I personally had my doubts and not much documentation on how are we supposed to make them work. Should I just paste the code as usual or will that get into conflict with Angular? Is it going to register "page views" whenever I change the routing or not as it's not a standard page view? I checked some things and here are some of my results: Adding Google Tag Manager snippet into the page First of all, I didn't find any problem with just copy-pasting the code before the /body closing tag, but I wanted to angularize it and have everything in one piece…
Read More

Angular directives and scope

Web programming
When creating directives with angular, you can choose to use the parent's scope, create your own isolated scope for the directive or have an inherited one. Let's see what types of scope we have and how to work with them: Let's start by considering this main controller and template: [sourcecode language="js" wraplines="false"] var app = angular.module("testingDirectives", []); app.controller("mainController", function($scope, $timeout) { $scope.MainValue = "main"; $scope.ValueToOverride = "notOverridden"; $scope.ValueToOverrideOn6 = "notOverridden"; $timeout(function() { $scope.MainValue = "main updated"; }, 1000); }); [/sourcecode] [sourcecode language="html" wraplines="false"] <div ng-app="testingDirectives" ng-controller="mainController"> {{MainValue}} <directive-one></directive-one> <directive-two></directive-two> <directive-three></directive-three> <directive-four custom-att="attribute"></directive-four> <directive-five custom-att="attribute"></directive-five> <directive-six custom-att="attribute" custom-att2="overrided"></directive-six> <directive-six custom-att="attribute" custom-att2="overrided 2"></directive-six> <directive-seven custom-att="attribute" custom-att2="overrided"></directive-seven> <directive-seven custom-att="attribute" custom-att2="overrided 2"></directive-seven> <directive-eight from-parent="{{MainValue}}"></directive-eight> <directive-nine from-parent="{{MainValue}}"></directive-nine> </div> [/sourcecode] Using the parent scope In this case, the directive doesn't have a scope of its own,…
Read More

HTML Emails: GMail Andriod App

Web programming, Web standards
While it looks like most email readers are moving into accepting standards as much as possible GMail has a bit of a weak point when it comes to its Android App (not the website though). I got myself in trouble when trying to add a logotype image centered on the header and with a background color... Centering an element Looks like the CSS property "align:center;" won't make it when trying to align an image to the center of the table cell, instead, using the HTML property "align" on the table/cell does it: [sourcecode language="html" wraplines="false"] <tr> <!-- Logo --> <td align="center"> <a href="http://www.gaydar.net"><img alt="Gaydar" src=".../logo.png" border="0" /></a> </td> </tr> [/sourcecode] But you may not want to center everything inside that cell, you can create a row just for this logo…
Read More

HTML Email tricks and howtos

Web programming, Web standards
Tips to write HTML Emails that I've found. Note this post can be bsolete as email readers are constantly changing and evolving. Background images It is just not possible to add an image as a background at the moment. While most readers will accept it Outlook Office seems to have issues and Hotmail/LiveMSN just doesn't support it. So unless you don't mind some of your users not getting the image at all this doesn't look like a good idea. As a tip: None of the big companies I checked use background images on their emails, this included Amazon, Google, Ebay, HSBC, Channel4, BBC, ... that should give you a hint. Solutions You can use plain colors on the background using a table's BGColor property and the CSS property. Instead of…
Read More

Difference between faking, mocking, and stubbing

Web programming
Quick answer Stub - an object that provides predefined answers to method calls. Mock - an object on which you set expectations. Fake - an object with limited capabilities (for the purposes of testing), e.g. a fake web service. Test Double is the general term for stubs, mocks and fakes. But informally, you'll often hear people simply call them mocks. From Elijah Manor Test stubs are functions (spies) with pre-programmed behavior. They support the full test spy API in addition to methods which can be used to alter the stub's behavior. Mocks (and mock expectations) are fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations. A mock will fail your test if it is not used as expected. Source. From Martin Fowler Fake objects actually…
Read More