I’ve been so busy that I’ve forgot to mention that my ASP.NET 4.5 book (written in Portuguese) is out.
Book review: CLR via C#
•February 25, 2013 • Leave a Comment[Disclaimer: I’ve received a copy of this book for reviewing]
Yesterday I’ve finally finished reading Jeffrey Richter’s last book CLR via C#, 4th edition. I’ve been getting a copy of this book (buying or being lucky enough to get a review copy) since it was called Applied Microsoft .NET Framework Programming. Each new edition ends up adding new cool stuff and this last edition didn’t disappoint. Besides Garbage Collector chapter rewrite, this latest edition introduces the new Reflection API (introduced by .NET 4.5) and presents several new concepts associated with the new WinRT framework. If you’re a die hard C# programmer, you should be pleased to know that the book has been updated to illustrate the new C# 5.0 async features introduced by the latest .NET framework release.
So, this is really a book I love and fortunately for us developers, Jeffrey has been doing an excellent job of maintaining and updating it during these last years. In my opinion, this is still the .NET reference book every developer should buy and read. My score: 10/10.
Book review: The art of SEO
•February 18, 2013 • Leave a Comment[Disclaimer: I’ve received a free copy of this book for reviewing]
In these last couple of days, I’ve spent most of my free time reading this book. It’s a really thick book (I must confess that is was way more thicker than I was expecting initially). It does cover a lot of ground and it’s probably the best reference on the subject available on print. It’s a little dense and I wouldn’t recommend it to someone who is just starting doing web development. Even though it covers several topics in depth, I must say that chapter 7 is really a must for anyone interested in understanding how links are used for ranking pages.
Even though the book has lots of examples (I think it would be better to reduce them, specially the ones presented in the first chapters), this isn’t a platform specific book. In other words, don’t expect to get instructions on how to perform a specific task for server A or platform B. Instead, expect to find lots of examples that can be used across all platforms. Overall, I liked the book and that’s why I’m giving it an 8/10.
The–LiteralPath parameter
•February 15, 2013 • Leave a CommentIt’s 2013, we’ve got Powershell for a couple of years now, so it’s only natural to automate things, right? At least, that’s what I try to do. So, one of the things I’ve ended up doing some time ago was writing a simple script for copying files from an USB pen to a specific folder. As you can see, it’s really simple:
ls j:\ | foreach {
$itemName = $_.Name.Replace('.', ' ')
$destination = ls | where { $itemName -match $_.Name } | select -First 1
if( $destination -ne $null){
mi $_.PSPath $destination.PSPath -Verbose -WhatIf
}
}
After some time where everything seemed to be running ok, I’ve noticed that some of the files weren’t being copied. After further analysis, I’ve noted that files contained the [ and ] chars weren’t being copied. Fortunately, we have StackOverflow and I’ve ended posting my question over there. One of the answers suggested to use the –LiteralPath parameter for the move-item cmdlet. According to the docs, the –LiteralPath parameter :
Specifies the path to the current location of the items. Unlike Path, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.
Did you notice the wildcards part? Yep, wildcards…unfortunately form me, I had completely forgotten that [] define wildcards which match a range of characters (note to self: do read the PowerShell docs). For instance, and this is just copied from the docs, here’s a quick example:
[a-l]ook matches book, cook or look, but not took.
Aha! ok, now it makes sense…that’s why those files which contained the [ ] on its name weren’t being copied. PowerShell was translating the values defined within [ ] into ranges. Another note to self: don’t forget about wildcards when you’re writing PowerShell scripts.
And that’s it for now. Stay tuned for more.
Updated material for ASP.NET MVC book
•February 14, 2013 • Leave a CommentIf you’ve bought the ASP.NET book I’ve written, you’ll probably be happy to know that I’ve added a new online chapter that deepens the initial cover of the Web API presented in chapter 9 of the book. If you’ve bought the book, just head to the web site and download the extra material associated with the book.
Modules: overriding properties
•February 14, 2013 • Leave a CommentAs we’ve seen in the previous post, we can add properties to an object exposed by a module through a technique known as augmentation. Even though there are some advantages associated with the approach shown (ex.: no need for loading scripts in a specific order), the truth is that we’re out of luck if we need to “override” a property defined in one of the files of the module. As I’ve said, in this case order of loading is important and there’s no way around it (after all, if we’re augmenting a property, it surely must have already been defined before being augmented, right?).
In order to illustrate this approach, we’ll rewrite the previous example. We’ll remove the jQuery dependency and will change the code so that each file defines a single printValue (file 2 overrides and augments this method). The following snippet shows the code I’ve ended writing in each file (assume we’re using strict mode and that file 1 is loaded before file 2):
var obj = (function (baseModule) {
var someValue = "private";
function printValue(elemSelector) {
return someValue;
}
return {
printValue: printValue
};
})();
//file2
(function (baseModule) {
var someOtherValue = "private 2";
//save reference to overriden method
var otherModulePrintAnotherValue = baseModule.printValue;
function printValueOverride(elemSelector) {
return otherModulePrintAnotherValue() + "—" + someOtherValue;
}
//change reference to new method
baseModule.printValue = printValueOverride;
})(obj);
If you compare this code with the one we had before, you’ll notice several differences. The most interesting part happens within the anonymous function defined in file2. As you can see, I start by saving a reference to the printValue method which was defined by the module introduced by file1. This reference is used by another function which will end up overriding the initial printValue method. Without this reference, it would be impossible to call the function after performing the override. From now on, you can simply call the overridden function like this (assuming we’ve got an element with ID set to info):
"use strict";
$("#info").text(obj.printValue());
</script>
And that’s it for now. Stay tuned for more.
More about modules: augmentation
•February 7, 2013 • 2 CommentsIn the previous posts, I’ve introduced some basics aspects related to the implementation of the module pattern in JavaScript. Even though it introduces several benefits, there are some problems that might make you think that this pattern isn’t for you. For instance, if you’re working in a team where several members are contributing to the same module at the same time, then your best option is to spread the module across different files.
Before going on, I must confess that splitting modules across different files isn’t really something I love. Whenever I’m facing this decision, I always try to re-check my code and see if I can refactor it so that a module is defined in a single file. There are, however, some cases where that isn’t possible and in those cases we need strategies like augmentation. The best way to illustrate it is to show an example. We’ll augment the module introduced in the previous post by adding a new method. Here’s one way to do it (notice that this code should be placed into a different JavaScript file):
var someOtherValue = "private 2";
function printAnotherValue(elemSelector) {
$(elemSelector).text(someOtherValue);
}
baseModule.printAnotherValue = printAnotherValue;
return baseModule;
})(obj, jQuery);
obj.printValue("#info");
obj.printAnotherValue("#info2");
Notice that this second anonymous function receives a reference to the original module which is being augmented (besides the jQuery global reference). There’s a slight gotcha with the previous code: you need to preserve the correct order when importing the files of the module. In the previous sample, that is not needed because none of the files uses properties defined in the other module, so we should do everything in our power to remove that dependency. Fortunately for us, we can remove that restriction by slightly changing the code:
var someValue = "private";
function printValue(elemSelector) {
$(elemSelector).text(someValue);
}
baseModule.printValue = printValue;
return baseModule;
})(obj || {}, jQuery);
var obj = (function(baseModule, $) {
var someOtherValue = "private 2";
function printAnotherValue(elemSelector) {
$(elemSelector).text(someOtherValue);
}
baseModule.printAnotherValue = printAnotherValue;
return baseModule;
})(obj || {}, jQuery);
According to the standard, the operator || will return the first expression when it’s convertible to true. If that isn’t possible, then it will return the second expression. In my example, we can use an empty object ({}) to ensure that we’re always working with a valid object within the anonymous functions that define the module. Since there is no dependency between the files, then this is more than enough for removing the order dependency introduced by our first snippet.
Even though the previous snippet allows one method to call another (ie, if, for instance, we need to call printValue from within printAnotherValue at runtime, then we should be fine), if we need to access properties in order to override them, then we’re out of luck . In these cases, we need to change the and there’s no way to escape the need to preserve the loading order. That will be the topic for another post. Stay tuned for more.

