Nov.26

Building an App with Xamarin, Part 2

I’ve started a series about building Android apps with Xamarin tools. In Part 1, I covered the basics of an Android app – how to add buttons to the page, give those buttons text, how to build a new screen and navigate to it. Today, I’d like to talk about data storage.

Almost all apps need some type of data storage. As usual, there are multiple approaches you can take.

XML

If your data storage requirements are very straight forward, then you might just want to consider storing your data in XML files. Especially if the amount of data will be small and simply to query. Storing your data as XML would also help make it easy to export it since the data are in simple flat files.

SQLite

As simple as the XML approach is, SQLite is not much more complex. But SQLite brings some benefits, especially in the long run. If you’re not familiar with SQLite, it’s worth checking out. It is beneficial even outside of the mobile world. From the SQLite website:

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

The SQLite database a single file and so can be stored in any file system. It can be used on just about any system out there. No, you can use it to replace Oracle, SQL Server or MySQL, but it works create for local storage.

A benefit of going the SQLite route is that you can use an ORM.

Cloud Storage

Azure is an interesting choice, in my opinion. Azure offers support for both Android and iOS so if you were going to deploy an app on both platforms, you could use Azure as a single back-end to both apps.

For data storage Azure offers MS SQL Server, blob storage and MongoDB. So if XML was a basic option and SQLite a middle option, I think Azure would be a good option – if you need the flexibility and cross-platform support.

Other Options

These are the three data storage options that I’m familiar with. Are there other options out there that I should be aware of or consider? Let me know in the comments or give me a shout!

Programming,Mobile

Nov.25

Building an App with Xamarin, Part 1

So I started to play around the Xamarin tools recently and thought I would post my progress as I go along. I’m a late comer to the mobile app field. Most of my work has been web based to-date. However, I really like the concept of Xamarin because it allows me to use some current skills (C#) and merge it with mobile apps.

This post will be very basic so if you’re proficient with Xamarin you may want to wait until the future posts come out. Follow me on Twitter (@JeremyFoltz) or sign up to get the posts delivered directly to your inbox.

When you create an new project using the provided templates, your project will look something like this:

ProjectTree

Here’s how it all breaks down:

Screen Layout

The *.axml files under the layout directory are the screens or layouts (aptly named!) of your app.

MainAXMLYou’ll notice that this page is super simple. It only has a single button and the button’s text the record_message string. This threw me a bit at first because it didn’t make much sense for the button’s text to be “string/record_message”. Then I saw the Strings.xml file and it all made sense.

StringsXML

Ah ha! There is  a string called “record_message”. So this is were you set the text for the various controls and buttons. So how does this screen look?

MainAXMLContent

The Glue

So what holds it all together? That would be the MainActivity.cs file. It’s at the bottom of that project tree above. It handles (as the name would suggest) all of the activity for the Main page such as handling button clicks/tabs, opening new screens, etc.
MainActivity

Notice the code on line 27. It’s setting the button’s (myButton) click handler to count each time the button is tapped.

Run It!

MyFirstAndroidApp

MyFirstAndroid app shows up on the bottom row.

You can run the app in an emulator for testing purposes so you don’t even need to have a device handy for basic testing. Here’s what our super simple Android app looks like.

My app shows up on the screen at the bottom with a default Android image.

MainScreen

Remember that click even handler we added in the MainActivity.cs file? Well, if we click the button, here is what happens.

MainClickEvent

Opening a New Screen

So let’s tweak our little app to make the button open a new screen.

Create the axml file:NewMessage

Now, let’s create the class for NewMessage:
NewMessageCS

And voila! We have a second screen with a button.
NewMessageScreenCapture

Wrapping Up

I know this post was very simple but hopefully it helps someone just getting started with Android developement using the Xamarin tools. If you have any questions or things you would like to see in the upcoming series, please give me a shout!

Programming,Mobile

Nov.24

Easy Web Application Performance Tweaks

Happy Thanksgiving Week! As usual, the year has seemed to fly past. I trust it’s been a good year for you!

Yesterday, I wrote a post about JavaScript Minification using Combres and I spoke about low-hanging performance fruit. Just as you might easily pick the low-hanging apples from a fruit tree, these tweaks for performance are easily done. Sometimes so easily done you might ask, why doesn’t everyone use these tips? When I began explaining them to my team at work, I don’t think anyone had heard of them, except for possibly one of them.

So, here’s a brief overview of some of the easy things you can do to help your web application perform faster and more efficiently.

Expires Headers

Browsers today all have caching ability so why not start to take advantage of it? Expires headers basically tell the client, Hey this resource has not changed so don’t send it. This helps to reduce the number of HTTP (or HTTPS) requests that your user’s browser will have to make. Mads has a good post on how to configure expires headers for IIS7 here.

JavaScript/CSS Minification

Make sure that you are compressing (minifying) all of your JavaScript and CSS. If possible even combine your JavaScript to a single file and your CSS to a single file. This will reduce the number of HTTP requests and reduce file size which will, in turn, reduce network latency, etc. Oh and your mobile users on limited data plans will thank you! You can use tools like Combres or RequestReduce, YUI Compressor, etc.

HTTP Request Compression

Be sure to have your webserver compress the HTTP requests. IIS will do this for you if you turn it on. This helps to reduce response sizes pretty dramatically. If you’re still running IIS (!), here is a resource to help you: TechNet. You’ll have to edit some of the IIS metadata. It’s not as easy as with IIS 7 where you can just add a setting to your web.config file which is really nice for a shared hosting environment! Here’s the TechNet article for that. Do take note that you may need to deal with static content differently than you do dynamic content.

 

Reduce Page Size

Finally, you may just need to reduce your over all page size. On one of the applications I work on, we have a very large page that is central to the application. I’ve tweaked it many times – and improved it’s performance. But we’ve come to the realization that if we want to go smaller, we’ll need to break up the page into smaller pages. Sometimes you can tweak an issue and correct an architectural problem. But other times, you need to go back to the drawing board.

Tools

There are three tools that I can recommend if you’re getting started evaluating your apps performance:

  1. YSlow: Gives you a Firefox/Chome plugins that will test your site and grade it. It will point out the various areas where it thinks you can improve that page’s performance. There’s quite a bit of information on their website so be sure to check it out. YSlow is from the respected Yahoo Web Application performance team.
  2. PageSpeed: Gives you Firefox/Chome plugins that act much like YSlow but with different rulesets. PageSpeed is from Google.
  3. Browser Dev Tools: Firefox, Chome and IE all have Dev tools with network profilers. The profilers will show you how long each request takes and how long until the DOM ready even fires. So, you can monitor the page loads with this tool and dive into the question of which request is taking the longest.

Just remember one thing: software performance problems are not usually fixed in one step. Performance is improved by improving lots of little things and when you’re done, you’ll step back and realize that the application is running faster. So pay attention to those details!

Programming,JavaScript,Performance

Nov.23

JavaScript & CSS Minification

There’s a thought in the software industry that unless your software performs well it’s useless – even if it has many good features. This is especially true in the web world. I’ve heard it said that unless your page loads in less than 2 seconds, people will go elsewhere.

In web applications one of the low-hanging performance fruits is to make sure that your JavaScript and CSS files are minified and, if possible, combined into a single file each. There are various tools to accomplish this. As you might expect each tool takes a different approach. Some tools will automatically detect, minify and combine the files. Others require that you specify the files you want it to act on.

Recently, I started to use the Combres library (Github) and so far it works well. You can install Combres via Nuget (seriously, if you’re not using Nuget, STOP and go download the extension for Visual Studio!). The Nuget install script handles most of configuration tasks for you like updating your project references and the web.config file.

Once the library is installed, there are only two steps remaining:

First, configure the combres.xml file to reference the script and css files you with Combres to act upon.
CombresConfig

Second, you need to add place holders for the Combres output:
CombresRefernce

 

I had to add the PlaceHolder controls to eliminate an error message about having “<%” symbols in the <head> section.

And that’s it for a basic setup of Combres. When you run your application, the Combres handler will produce a minified and combined CSS file and a minified and combined JavaScript file.

I liked several things about Combres:

  • It only acts upon the files that I tell it to. If, for some reason, I don’t want it to include a specific file, it’s easy to do – just leave it out of the xml file.
  • I can specify which compressors are used. I can even specify a different compressor for JavaScript than CSS. Check out the combres_full_with_annotation.xml file that the Nuget package installs in your App_Date directory for all the options.
  • If I want Combres to not minify and combine, it’s as easy as setting my debug setting to false in the web.config file (obviously, you don’t want to do that on production!).

I did encounter one hitch in the setup. The project I was adding this too is a Website project – not a Web Application project. So the project is dynamically compiled instead of being compiled into a DLL before deployment. Because of this, I needed to add the following line to my Global.ascx page in the Application_Start method:

System.Web.Routing.RouteTable.Routes.AddCombresRoute("Combres");

If I didn’t add that line, my HTTP requests to the combres.axd handler would return a 404 error.

Combres also does some other things that I haven’t delved too deeply. It adds some caching capabilities such as expires headers. These is another easy performance change you can do but is outside of the scope of this post.

I’ve really liked Combres and recommend that you have it a try!

JavaScript,Performance

Nov.22

One Week of Daily Blogging

This morning I’ve hit my first milestone. It’s a small one but a milestone none-the-less. One week ago today, I began blogging on a daily basis. None of the posts over the past seven days have been written before hand and released automatically.

If you’ve known my history with blogging, then you’ll know that this is unusual. I’ve had two other blogs before this one; the oldest of which dates back to late 2011. So then, why the change? Quite simply, I wanted to begin to build a platform, something to help me distinguish myself from others. A number of my dreams and ambitions revolve around business and freelancing. In those areas a good platform is invaluable.

I have recently made a change to my morning routine. I used to get up very early for a job that both started early and also had a long commute. But since then I’ve been working a regular 8-5 job with a 25 minute commute (and sometimes not even that because of telecommuting). Needless to say, the incentive to wake up very early dried up quickly!

However, I’ve come to the conclusion that with 3 small children if I’m going to have quiet time on a regular basis, where I can spend some time focusing on things like personal reading and blogging, I’ve got to get up earlier. So that’s what I’ve done. To a bit of surprise, it’s worked really well so far.

My plan is to continue my morning schedule and improve my writing skills and platform by continuing to blog daily. Stay tuned: I’ve got some cool technical posts in the works!

Blogging

Nov.21

The Why

Dreams and Goals: Where are you going?

It’s a fair statement to make to say that we all have dreams and desires for the future. I think that’s just part of human nature. Sometimes those dreams come to fruition and sometimes they remain dreams. However, I believe it’s important to keep and follow those dreams. They can be a source of inspiration.

It’s also important to have goals. But, I hear you say, aren’t dreams and goals the same thing? The simple answer is no they are not. A goal is a more concrete implementation of a dream. It’s more actionable and specific.

I want to talk just a bit about a certain part of your dreams and goals: the why. Why you have a specific dream or why that one thing is your goal. Whenever you set out to do something – really anything of significance in life – you need to sit down and figure out why you that thing is a priority.

The idea of living intentionally is the root idea here. We only have a fixed allotment of years here on Earth. It would be a shame to live out your years and yet never achieve any of your dreams or goals – especially if you spent your life doing things that didn’t matter or didn’t bring you closer to achieving them.  It’s an entirely different thing if you try for your dreams and goals and it simply doesn’t work out (i.e. flying to the moon; but you did work for NASA).

So back to the concept of why. What are the whys of your life? Why are you working at that particular job: advancement, learning, etc? Does that job line up with the whys of your life? If you have been offered a different job, consider the whys. What if your goal is to start your own business: consider the whys.

This concept is not knew, I first heard it a while back from a WordPress developer in Canada, Curtis Mchale. It’s stuck with me because I believe that if you know the whys of your life and you consistently evaluate them against what’s actually going in on your life, you’ll be able to reach your dreams and goals. In short it helps you live intentionally and that is valuable!

Musings

Nov.20

Xamarin Tools

If you’ve worked on the ASP.NET community for any significant period of time, you’ve probably learned that ASP.NET doesn’t always need IIS to run a web application. If fact with frameworks like Mono, you can run ASP.NET on Linux or OSX. But what is more interesting to me is that Xamarin, the company behind Mono also builds a framework for iOS and Android platforms.

So what, you might ask? The very, very cool thing that Xamarin brings to the table is that you can build for iOS, Android, Mac and Linux all using the C# language. If you’re an ASP.NET developer, there’s a good change that you already use C# in your day-to-day work. So now, virtually all of mobile devices and iOS are within your grasp as a developer.

How does this work?

Basically, Xamarin allows you to build an app using C#. But when you compile it, the Xamarin tools will compile the C# code down to the target platform. This means that the app will act like it was written using the native tools like Objective-C or Java. I was listening to the Tablet Show podcast #111, and it was mentioned that while there is a some performance hit, it’s generally not noticeable at all.

Code

Here’s a screen capture of C# code in Xamarin’s ‘Tasky’ sample application. This app is targeting the Android platform.
AndroidCSharpThis is using Xamarin’s IDE called, aptly enough, Xamarin Studio.  If you shell out the cash for the business license, you can build these applications directly from within Visual Studio. However, if you only spring for the Starter or Indie versions, you’ll need to use Xamarin Studio.

All of this makes me excited because the sky is the limit for a good C# developer right now! The language itself is mature and robust, Visual Studio keeps getting better (in my opinion) and now you can target just about any platform out there all while still working in the same language. How cool is that?

Programming

Nov.19

Creating Habits

I have a confession to make: I’ve been a terribly irregular blogger. In the past I’ve had more days go by that had no blog post than did. Recently, I’ve altered my morning schedule and have started blogging daily. I’m a bit surprised how much I enjoy it!

So what lead me to changing my habits? I suppose the reason is multifaceted. First, I’ve admired folks like Chris Lema in the WordPress community who blog on a daily basis. Chris writes his posts on a daily basis, not in a batch and scheduling them to be posted. He’s mentioned that over the past eight years he’s had a number of blogs but they always died. Writing daily has helped build a quality blog and platform.

Platform is the second reason I wanted to start writing daily. The software industry is a noisy place, especially online. It’s my desire that through writing good articles each day, I can help myself stand-apart a bit in the industry. While searching for answers to various coding problems online, I’ve seen many, many blogs (often very helpful) but the posts are either all very old or very sparse.

The third reason to blog daily is that it helps me solidify my programming skills if I can explain something in clear and nonsensical way. The old saying is (something to the effect of) “you don’t really know a topic unless you can teach it to someone else.”

My final motivator to blogging daily was a podcast called “Giant Robots Smashing Into Other Giant Robots.” Great name, huh? Specifically it was episode 72. In that show, Nathan Berry talked about committing to writing 1,000 words per day. Out of that effort he has been able to write new books and update new books for things like iOS 7. These efforts have paid off for Nathan in a big way! I’d like to work toward a similar goal. Writing daily is something that will, and has so far, help me to work toward that goal. As they say in the personal finance world, just keep taking baby steps!

Blogging,Musings

Nov.18

JavaScript Grid Framework

In previous posts (here, here and here), I referenced that I was building a framework for JavaScript based grids. Today I’d like to begin to detail how this framework works. This post will provide an overview and coming posts will show sample code.

Background

I’ve been working on an ASP.NET web forms application that uses the GridView control frequently to display tabular data and provided editing capabilities for that data. Each GridView is wrapped in an Update Panel to prevent the entire page from posting back to the server when the user interacts with the grid. This approach is straight forward and has worked well for us.

However, as the application has grown, the performance around the grids has steadily decreased. Sometimes it was because business data validation rules needed to be put into place. Sometimes it was because requirements dictated that another grid be added to the page. Performance had decreased to the point were it took several seconds to open a row in edit mode or even add a row.

I began to explore using AJAX calls to WebMethods on a .aspx page. I’ve used this approach before for other functionality so I knew it was very responsive. After toying with some prototypes, I became convinced that it was possible to create a HTML table and interact with it via JavaScript.

JavaScript Grid Framework

After tweaking, hitting browser performance walls and rewriting bits, I now have a JavaScript based framework that provides us with grids to display and interact with tabular data.

JavaScript Grid Framework Process Flow

JavaScript Grid Framework Process Flow

I’ll briefly walk through the flow:

  1. At some point (usually on a page load event), the MyGrid_Config.js is executed. This creates a config object with all of the necessary details about the specific grid. For example, what methods to call for database interaction. What properties should be assigned to each column, etc.
  2. The RenderGrid method in the GridFramework.js is called which initiates an AJAX call to the GridFrameworkHandler.aspx.
  3. This page acts, in a way, like a network router. It looks at the config data and figures out which class to instantiate and which method to call.
  4. It invokes the method to perform the necessary database work.
  5. It calls the GridGenerator.cs to build a HTML table based on the config object and the data.
  6. The generated HTML table is passed back to the GridFrameworkHandler.aspx.
  7. GridFramework.js places the markup in a a DIV and calls the jEditable jQuery plugin. This plug in allows for inline editing of the grid values. Literally, you simply click on the value and you can edit it.

So how well does it work?

It works very well! Each grid initialized via lazy loading so no work is done unless the tab it’s on is displayed. Editing just takes milliseconds while deleting and adding rows is all less than a second. I’ve been very pleased with how it’s all turned out.

Programming

Nov.17

Visual Studio 2013

I downloaded Visual Studio 2013 (Express for Web) yesterday and began to play around with it. I had watched a small portion of the keynote at the launch so I knew of several of the features.But I had not downloaded any release candidates so this was my first peek.

At first glance, visually it appears very similar to VS 2012 with some tweaks to color and design. However, that’s not the most interesting part. VS 2013 has an online component (cloud, to use the current buzzword). You can sign into your Microsoft account directly from within VS. You can create a custom URL for your VS Online site such as mysoftware.visualstudio.com. VS Online is also supposed to sync your VS settings from machine to machine, although I’ve not tried that yet. That’s a nice touch if you sometimes write code on more that one machine like I do.

From this site, you can completely manage your projects. They give you several project management templates such as Scrum, Microsoft Agile and Microsoft processes for CMMI. Once the project is created, you can create sprints, tasks, record bugs, test cases, etc. You can have up to five collaborators/team members accessing your project for free. If you have an eligible MSDN subscription you can create team rooms for chatting.

VS Online also will host your source control. You can use Microsoft’s Team Foundation Version Control or you can use Git. So far, the Git option reminds me of Bitbucket’s service. You have a local repository and then you add the VS Online as a remote repo. Then you can simply push changes up to VS Online.

If you are a fan of continuous integration (in my opinion you should be), VS Online brings this option to the table as well. You can configure a build process based upon manual triggers or check ins. You can create test plans for each of your sprints in VS Online.

So far I think VS Online looks very nice, easy to use and convenient. If your project is already set up on GitHub or BitBucket, I suspect you’ll probably end up leaving the code there. But for new projects, it sounds very interesting. I like the built in project management features as well. I plan to giving VS Online further exercise this week so as I learn more or have additional reactions, I’ll post them here.

Programming