This is default featured slide 1 title

You can completely customize the featured slides from the theme theme options page. You can also easily hide the slider from certain part of your site like: categories, tags, archives etc. More »

This is default featured slide 2 title

You can completely customize the featured slides from the theme theme options page. You can also easily hide the slider from certain part of your site like: categories, tags, archives etc. More »

This is default featured slide 3 title

You can completely customize the featured slides from the theme theme options page. You can also easily hide the slider from certain part of your site like: categories, tags, archives etc. More »

This is default featured slide 4 title

You can completely customize the featured slides from the theme theme options page. You can also easily hide the slider from certain part of your site like: categories, tags, archives etc. More »

This is default featured slide 5 title

You can completely customize the featured slides from the theme theme options page. You can also easily hide the slider from certain part of your site like: categories, tags, archives etc. More »

 

Pre-Process your images with promises

For this post, I’m using Windows 8 WinJS as the vehicle. However, this approach is applicable by any method that implements an XMLHttpRequest.

Here’s the scenario: you have an application with several images and those images are stored on a remote server. The sources of each image is something like http://someserver.com/myimage.png.

Looking at a Windows 8 store app as an example, the good news is that your page will display immediately. Your images may display immediately. Or more likely, they will begin to display after the page has loaded. You may see the images incrementally appearing. This does not make for a good user experience.

The good news is that WinJS implements the Promise Pattern and XMLHttpRequest via the WinJS.xhr object. This means we can pre-fetch images and resolve them to blob URL’s and then bind our UI to the blob URL instead of the raw image url.

Using a stock WinJS Navigation Project, the following code is placed in the app.addEventListener(“activated”, function (args) {} handler:

 var promises = [];  //Load the blob for the default image in the event a specified image URL does not work. WinJS.xhr({ url: "images/no-available-image.png", responseType: "blob" }).done(function  (data) {  var noImageBlob = URL.createObjectURL(data.response);   //The imageResults.json file is a static result from the Bing Images API.   WinJS.xhr({ url: "js/imageResults.json" }).done(function (data) {      var results = JSON.parse(data.responseText);      //Loop through the results to create the promise array.      results.d.results[0].Image.forEach(function (element, index, array) {      promises[index] = WinJS.xhr({ url: element.MediaUrl, responseType: "blob" })         .then(function (e) {            //When the promise executes, an imageBlob property is created            //holding the image blob URL            results.d.results[0].Image[index].imageBlob = URL.createObjectURL(e.response);         }, function error(e) {            //If an error occurs when trying to retrieve the image            //the No Image BLob URL created on line 28 is used instead            results.d.results[0].Image[index].imageBlob = noImageBlob;         });      });      //The join method kicks off the promises handed to it.      //The promises can run in any order and finish at any time.       //The done event for the join method fires after all of the promises      //in the promise array have been fulfilled.      WinJS.Promise.join(promises).done(function (e) {         //Need to create a global namespace to hold a reference         //to the images array. To facilitate data binding,          //the WinJS.BindingList method is invoked, passing the          //image array.         WinJS.Namespace.define("ImageList",            {               Images: new WinJS.Binding.List(results.d.results[0].Image)            });;            //Now that all of the pre-processing has occurred, we can now            //navigate to the home page that displays the images.            if (app.sessionState.history) {               nav.history = app.sessionState.history;            }            args.setPromise(WinJS.UI.processAll().then(function () {               if (nav.location) {                  nav.history.current.initialPlaceholder = true;                return nav.navigate(nav.location, nav.state);            }            else {                 return nav.navigate(Application.navigator.home);            }         }));      });   }); ); 

JavasScript, being a dynamic language, we can simply augment the object from the parsed JSON. In this example, I used a static file from the Bing Image Search API. You will have to conform the object references to the specific API you are working with. In the previous code, we wait until all of the images have been fetch. In an async world, this can be a challenge. We don’t know what order the requests will be processed. In some cases, a server status 500 could result. This approach affords us an opportunity to handle that error with a place holder image. If on the other hand, you deal with the raw URL’s directly, you have no such opportunity to intercept errors.

On the HTML markup side, I have this code to bind an image tag’s src to the newly created imageBlob property. For this example, the raw URL that is returned in the response is MediaUrl.

 <section aria-label="Main content" role="main">    <div id="ImagesTemplate" data-win-control="WinJS.Binding.Template">       <div style="width: 150px; height: 100px;">           <img src="#" style="width: 60px; height: 60px;"                data-win-bind="alt: MediaUrl; src: imageBlob" />       <div>        <h4 data-win-bind="innerText: MediaUrl"></h4>       </div>    </div> </section> <div id="ImageListView"     data-win-control="WinJS.UI.ListView"      data-win-options="{itemDataSource : ImageList.Images.dataSource,                        itemTemplate: select('#ImagesTemplate')}"> </div> 

In the markup, I have a WinJS ListView control that uses the ImageList.Images.dataSource as its datasource. The ListView also references a simple template to display the output.

Here is the result (looks a bit skewed due to this page’s dimensions):

images

When the page displays, because the images have already been pre-fetched, they will immediately display as well. This makes for a more pleasant user experience.

Looking at the first image, it has a raw URL of:

http://www.israbox.com/uploads/posts/2011-04/1303738133_john-coltrane.jpg

When pre-fetched, the blob URL is:

blob:354BAAC6-B8D8-4172-A283-4DF2F037FECD


CodeBetter.Com Posts

Orthodoxy vs. Pragmatism, or How I Became a Better Developer

Few indeed are the practices that can be recommended universally. Wisdom suggests the humility of knowing this and applying it liberally.
Dr. Dobb’s Design Articles

Acquiring Big Data Using Apache Flume

Data analysis is only half the battle; getting the data into a Hadoop cluster is the first step in any Big Data deployment. Apache Flume uses an elegant design to make data loading easy and efficient.
Dr. Dobb’s Database Articles

Detailed Profiling of SQL Activity in MySQL 5.6

MySQL’s latest update to the Performance Schema brings the ability to profile an statement’s activity, low-level wait events, and I/O impact. It is the easiest and most detailed way to identify what statements to tune and how.
Dr. Dobb’s Database Articles

The joy of being a programmer

I am programming since I am 10 and I am now 38. Today I measure how much good programming bring to my life, directly and indirectly. I’d like to give credit to aspects I love in this job. Hopefully some young people will read this and will consider maybe doing one of the most wonderful job on earth.

Getting in the flow: According to wikipediaflow is the mental state of operation in which a person performing an activity is fully immersed in a feeling of energized focus, full involvement, and enjoyment in the process of the activity. In essence, flow is characterized by complete absorption in what one does. Focus, immersion, being concentrated, involvement …  being everyday in the flow by coding hours and hours, contribute a lot to a solid positive state of mind.  These are moments where one can completely forget about minor everyday pesky stuff, but also forget for a while more serious problem in life everyone has to face. Being in the flow is the condition to solve challenging problems and to create beautiful pieces of engineering. Being in the flow can lead to addiction but it is not addiction. This is essential to control when to check-in in the flow and when check-out, making sure to not be disturbed meantime.

Being creative: Being a software engineer is one of the most mainstream way of being paid for being creative. Often, writing software is deemed to be an artistic activity. A programmer has to be humble, because this is a kind of art not understood by the mass. But being humble is a chance to become wiser and increase self-confidence. Also, knowing you are going to be creative for a while, is an excellent motivation to overcome the first step effort to jump in the flow. But the truth is that for every passionate programmer, there is a background thread in the mind in charge of creativity (often running at sleep-time), that makes it so that in the morning the envy to create what you have in mind is too strong.

Becoming an expert: It is common to hear that a programmer must know numerous technologies, that its skill is to learn how to learn new technologies. On this I disagree because what makes me really happy is to master completely a technology and exercise daily my expertise. I used to master all the tricky details of C++ and COM and it was fun. Before that I used to master some assembly level programming and it was fun, I was not even getting paid for that. In 2002/2003 and then  2005 I wrote two editions of a 1.000 pages book on .NET and C# and writing it has been one of the most blessed moment in my career. Since then I capitalize on this knowledge every single hour of coding, letting me focus my thoughts on problems to solve, and not on all the non-trivial things a complex platform like .NET is actually performing under my feet. Of course I am constantly discovering new details about surrounding technologies, like functional programming paradigms through the prism of functional paradigm introduced in .NET languages. But I know what is my core knowledge, both in terms of technologies and in term of program design skills. And as long as I won’t be forced to change my core skills, relying on my expertise to express my creativity and making a living on top of it is a source of personal achievement.

Meet inspiring people sharing the same passion: I imagine meeting peers is a source of happiness for every expert in something. This is also why investing in a solid programming particular set of skills is a positive thing. Not only respect from others programmers arise, but it lets have great exchange with smart people as passionate as you. The importance of flow, underlined above, also comes with the disadvantage of being often alone with your thoughts. Most programmers enjoy working alone anyway, but for those who need a bit of more social activity, having an expertise in something is also a great way to become partly a teacher (in professional or academic spheres), partly an architect and contribute to important decisions, partly a team-leader and be responsible for project progressing, partly a consultant, and be able to share your knowledge in a pleasant social environment. I put the word partly in italic because if your social activity make it so that you are not writing code meant to be run in production anymore, you shouldn’t consider you as a programmer anymore and you’ll loose a great deal of the points I am mentioning here. If you need social interactions all day long, programming is not for you.

Being involved in something that make sense: Here also my position might be a bit different of what is widely accepted. I agree that for juniors, it is important to multiply the opportunities to work in several different teams and companies, to get an idea of what they like and what they don’t, to be influenced by several inspiring mentors. But once you become seniors, working on the same application in the long term, where you feel well programming in,  polishing it days after days, seeing its evolution across years, maintaining it in a clean state by adopting modern paradigms like automatic tests, DbC or relentless refactoring, having your word to say about strategic decisions, personally I found this being a great source of daily happiness and a great motive to involve myself! In addition, working hard to achieving important milestones regularly, is an excellent way to give a sense to your professional career, which is (much) more the exception than the rule.

Work wherever, whenever you like: A 2KG laptop with the proper tools set installed, a few hours of electricity every 24 hours, this is all what a programmer needs to do his job well. A descent internet connection is often appreciated, and there are today only few points on earth where internet is not available at all somehow. Programming might be probably the less demanding working activity in terms of time and space requirement. Getting in a flow in a 12 hours plane flying across the planet, scheduling half a year to live and work in a paradise tropical island, avoiding traffic jam by staying at home for work (in your pijama), delaying programming in the night or early morning to take care of the kids and their education, all this is not only possible but pretty common actually. Most serious software companies let some of their skilled engineers work wherever they prefer. Did you know that many of the great minds behind Visual Studio didn’t actually moved with their family to Seattle, but still live in their country, sometime far away from the US?

Make a decent living doing something you like: Last but not least, everywhere, skilled programmers get paid above the average salary in their countries, and if we take the example of a developing country like India, good programmers get a much much higher income than the average. On top of that, a skilled programmer have pretty close to zero chances to remain unemployed for a long time. This also means that if you don’t like your current position, it is easy to find a new better suited one. This situation is made possible because less than two decades ago, the modern civilization realized that IT is the condition for its development. It is a fact that many of the richest persons in the world were originally programmers and every motivated programmers has the potential to create its own ISV business and become a millionaire. A programmer can also decide to make more money by coding for the financial industry, or even bet on a startup and potential makes millions in a few years.

All the next big things will consume even more IT, this includes human genome analysis for the mass, the entire medicine that will be deeply impacted by that, more prevalent portable devices, more sophisticated entertainments, augmented reality, robots and automated machines to do surgery, to assist the increasing growing number of old people, artificial intelligence in the long term, and certainly everything nobody hasn’t imagined yet. After all 20 years ago, nobody anticipated the impact of Google. 10 years ago nobody anticipated the impact of Facebook and smart phones.

Ok enough getting in the flow of writing my passion for programming, I have some code to write before the weekend :)


CodeBetter.Com Posts

Hadoop Tutorial Series

Dr. Dobb’s Database Articles

Xamarin Evolve 2013 Talk Video – How C# Saved My Marriage

My Talk at Xamarin Evolve

In case you haven’t heard, Xamarin is a fabulous company was amazing products and their Evolve 2013 conference was absolutely smashing.

The Visual Studio Solution Explorer now has Android, iOS, and Windows all in the same place!I was fortunate enough to speak at Xamarin Evolve 2013 in Austin last month, just after lunch one day. I wanted everyone to understand how excited I am about what they’re doing, how Azure and Visual Studio appreciates them and how great it is to be a C# developer right now. So, I did a fun little talk called “How C# Saved my Marriage, Enhanced my Career and Made Me an Inch Taller.

The general story of my talk is that, like VISA, C# is everywhere I want to be. I can create apps now on iOS (iPad, iPhone), Android (Phones and Tablets), Mac desktop apps, Windows Phone, Windows (all flavors), and Windows 8 Store apps. You can use Xamarin Studio, of course, but even better, Xamarin integrates with Visual Studio wonderfully. You can literally write iPhone apps in C# from within Visual Studio and compile them using a networked Mac as a build server. It’s amazing.

I teamed up with Greg Shackles and we ported the Pan Tilt Zoom client (I already have Web and Windows Desktop versions) to iOS and Android!

Then Dominique Louis from MonoGame helped me port Daniel Plastaid’s “Disentanglment” application to Android and iOS. Now it runs on Windows 8, iOs and Android.

disentanglement

We announced better NuGet support for Mono, took a SignalR pull request for iOS support and more. It was great fun.

Here’s a screenshot I took in the middle of the talk. I used Lync and the Xamarin-powered iPhone version of our Pan-Tilt-Zoom app using SignalR to call back to the Seattle Office. Then I turned the camera around so I could see myself watching myself watching me watching the keynote.

Lyncing back to the SignalR team

It’s a great time to be developing with C# and .NET. I hope you also join me at the MonkeySpace conference (formerly Monospace) in Chicago in July. I’ll be presenting, teaming up with a friend who is launching an amazing soon-to-not-be-so-secret .NET related product that will take C# to even more places!


Big thanks to our Sponsor this week. It’s Redgate! Check out Deployment Manager – app deployment without the stress. Deploy .NET code & SQL Server databases in one simple process from a web-based UI. Works with local, remote and cloud servers. Try it free.




© 2013 Scott Hanselman. All rights reserved.

     

Scott Hanselman’s Blog

Scripting ease with Script Packs

Script Packs are a really cool extensibility point we added into scriptcs. A pack delivers a bundle of functionality that makes frameworks more palatable to consume from script. They are available as nuget packages making them very easy to consume.

For example, if you look at our Web API sample, you’ll see there’s a bunch of fiction if you just try to get web api working from scratch.

  • You need to add using statements for each namespace you want to use. This is a lot more painful than one might thing when you don’t have intellisense.
  • You need to configure web API, this involves creating a host, defining default routes etc. Adding lots of object creation and such starts to make the script pretty hairy. Not impossible, but painful when there’s no template.
  • You need to teach Web Api how to resolve controllers in script by implementing a custom controller resolver.

Now pull in the Web Api script pack (scriptcs –install scriptcs-webapi) using the Require<WebApi>() and your boiler plate code evaporates to this:

id: file:

The script pack does all of the following to make the experience better

Removes the need for using statements for common namespaces. The script pack provides those which is why you don’t have to add the web api namespaces in your example above.

Adds dll and nuget package references that bring the dependencies the framework needs.

Removes general boilerplate code. In the previous sample you need to create a host, define routes etc as I mentioned. In this case the script pack creates the host for you and configure with the default routes. You can customize if you need to.

Provide APIs to fill gaps that prevent the framework from working well in script / supporting dynamically emitted assemblies. The Web Api script pack brings in and configures a custom controller resolver for you.

We’re just getting started with the work we’ve done with script packs, but they are a really nice extensibility point and really take advantage of nuget as a delivery mechanism. The community has been rising to the occasion and building out quite the gallery as well.

There’s some great posts about script packs covering topics like how to build them or even use them from the REPL that you should really check out.

Have fun exploring the new world of scripting in C# with scriptcs!


CodeBetter.Com Posts

Working with TypeScript in Visual Studio 2012

Using Visual Studio makes working with TypeScript easy and helps reveal subtleties of the language implementation and code generation.
Dr. Dobb’s .NET Articles

@model and beyond

In my previous post I had been fiddling with the html helper used in Razor views. Since then our custom html-extensions have been doing great things for our project. To mention some:

  1. Standardizing the look and feel. It is far more consistent and maintainable to set attributes (including a css class) and event handlers in one centralized place.
  2. Simplify the script. Often a part of the logic the script will follow is already known server side. Instead of writing everything out in javascript rendering the intended statements leads to a leaner client. There is an example in my previous post on building post-data.
  3. Decoupling the libraries used. At the moment we are using the Telerik MVC suite. In my previous post I described how our html helpers build standardized Telerik components for our views. In the not to far future we want to switch to the Telerik Kendo suite. Having wrapped up the library dependency in our Html helper will make this switch a lot easier to implement.

What has evolved is the way we work with the model. In MVC the implementation of the controller and the view is clear. When it comes to the implementation of the model there are almost as many different styles of implementation as there are programmers. In general the model can bring any data to the view you can imagine. Not only the source of the data varies, from plain sql to a C# property, also the use of the model’s data varies. It can be a customers name from the database. Or it can be the string representation of some html attribute needed for a fancy picker. Here data and presentation start to get mixed up. Our extensions needed information for the Html-Id. The original Html helper had a custom constructor to get that specific data from the model into the helper. Which required to create our own htmlhelper when starting the view and use that one instead of the standard @html. As seen in the eposHtml in the previous story. It would be cleaner if our extension methods could be satisfied with the default html helper. It would also be cleaner to keep a better separation between ‘real’ data and presentation.

The model is available in every HtmlHelper extension method.

public static PostDataBuilder<TModel> PostData<TModel>(this HtmlHelper<TModel> htmlHelper)

{

    return new PostDataBuilder<TModel>(Id(htmlHelper.ViewData.Model));

}

 

It’s a property of the ViewData.

In our case we needed something to give the control an unique Id. The Id method builds that Id. Previously we passed the Id-base in the constructor, which lead to the custom helper. A far more elegant solution is using a very basic IOC-DI pattern. As implemented By the Id method

private static string Id(object model)

{

    var complex = model as IProvideCompositeId;

    if (complex != null)

        return complex.CompositeId;

    var simple = model as IProvideId;

    return simple == null ? “” :  simple.Id < 0  ? String.Format(“N{0}”, Math.Abs(simple.Id)) : simple.Id.ToString();

}

 

The method queries the model first for the IProvideCompositeId interface, in case the model does not implement that it is queried for the IprovideId interface. Resulting in a string which can be safely used in an HtmlId. (A negative number would lead to a ‘–’ in the string, which is not accepted in an Html Id).

These interfaces are very straightforward

public interface IProvideCompositeId

{

    string CompositeId { get; }

}

 

public interface IProvideId

{

    int Id { get; }

}

 

In case the model is going to be used in a view requiring unique Id’s the model has to implement one of these interfaces.

public class FactuurDefinitie : IProvideCompositeId

{

    public readonly int IdTraject;

    public readonly int UZOVI;

    public readonly bool Verzekerd;

 

    public FactuurDefinitie(int idTraject, int uzovi, bool verzekerd)

    {

        // The usual stuff

    }

 

    public string CompositeId

    {

        get { return String.Format(“{0}{1}{2}”, IdTraject, UZOVI, Verzekerd); }

    }

}

 

Working this way:

  • We can use our custom html extensions in the default html helper
  • Specific data from the model is available inside our extensions
  • The model and the view do not get entangled

The code is no big deal. I know. But the model is something whose horizons are still not in sight.


CodeBetter.Com Posts