CloudStorageAccount.Parse – Azure SDK 1.8

November 5, 2012 12:56 by wjchristenson2

Just a quick heads up to anyone that is trying to code against their development machine’s storage emulator and recently upgraded their Azure SDK to 1.8 (October 2012).  There is a nasty bug with the development storage connection string “UseDevelopmentStorage=true”.  You will likely get the following error: “The given key was not present in the dictionary.” when trying to parse the cloud storage account connection string like below.

 

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

 

To get around this issue, add the following to your development connection string “DevelopmentStorageProxyUri=http://127.0.0.1”.  Here’s the full connection string: “UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://127.0.0.1”.  This did the trick for me.  Hope it helps!

 

Bookmark and Share

SQLite Bulk Insert

February 19, 2012 06:13 by wjchristenson2

“Bulk Insert” for SQLite does not exist.  However, there are some tricks to speed up bulk loading of SQLite data.  In this post, I am going to show you some techniques that I’ve learned to load data as fast as possible into a SQLite database.

First, every SQL statement ran on SQLite is ran under a transaction even if you don’t specify one.  When loading thousands of records, this can be very taxing and slow things down very fast.  As a result, you’ll want to load all records under one transaction so that the SQLite DBMS will not start/commit a transaction for every row.

Second, use prepared statements and/or reuse your SQLiteCommand objects.  What you do is dependent on what technology you are using SQLite with (e.g. Java for Android or ADO.NET).  Rebuilding objects for every record inserted is very taxing.  So you’ll want to build the object once, then pass values to it for each row.

In summary, do the following for quick “bulk inserts”:
1)  Use 1 transaction to load all records.
2)  Reuse command/prepared statements to insert each row.

Here’s a snippet on how to bulk insert in Java for Android:

db.beginTransaction();
try {
	SQLiteStatement insert = null;
	insert = db.compileStatement("INSERT OR REPLACE INTO \"MyTable\" ("
			+ "\"MyColumnName\") VALUES (?)");
	
	for (i = 0; i++; i < 10000)
	{
		insert.bindLong(1, i);
        insert.execute();
		insert.clearBindings();
	}
	
	db.setTransactionSuccessful();
}
catch (Exception e) {
	String errMsg = (e.getMessage() == null) ? "bulkInsert failed" : e.getMessage();
	Log.e("bulkInsert:", errMsg);
}
finally {
	db.endTransaction();
}

 

Here’s an example of how to bulk insert in VB .NET:

        Using sqliteConn As SQLiteConnection = New SQLiteConnection("Data Source=c:\test.s3db")

            Dim sqliteTran As SQLiteTransaction = Nothing

            Try
                Dim sql As StringBuilder = New StringBuilder()
                sql.Append("INSERT INTO ""MyTable""(""MyColumnName"") VALUES (:MyColumnName);")

                Dim cmdSQLite As SQLiteCommand = sqliteConn.CreateCommand()
                With cmdSQLite
                    .CommandType = CommandType.Text
                    .CommandText = sql.ToString()
                    cmdSQLite.Parameters.Add(":MyColumnName", DbType.Int64)
                End With

                Dim i As Integer = 0
                While i < 10000
                    cmdSQLite.Parameters(":MyColumnName").Value = i
                    cmdSQLite.ExecuteNonQuery()

                    i += 1
                End While

                sqliteTran.Commit()
            Catch ex As Exception
                'attempt to rollback the transaction
                Try
                    sqliteTran.Rollback()
                Catch ex2 As Exception
                    'do nothing
                End Try

                'rethrow the exception
                Throw (ex)
            End Try
        End Using

 

Both examples insert 10,000 records.  Notice that we explicitly kick off one transaction for all inserts and commit it after we are finished inserting.  Also notice that in both examples, one command/prepared statement is built and reused for all 10,000 inserts.  There are some other SQLite tweaks you can do to slightly speed up performance, but the 2 mentioned will give you the most gains without having to get too technical with the SQLite DBMS engine.


Bookmark and Share

WPF Data Binding Troubleshooting Tips

December 10, 2011 05:05 by wjchristenson2

This week I ran into a WPF data binding problem and spent about a day trying to figure out why it wasn’t working.  A day is a long time to spend on why a “Text” property is not binding to an object’s property which is of the type “String”.  Here’s some quick tips on identifying the source of the data binding problem.

1)  Ensure Data Binding Debugging Information shows in Visual Studio’s Output window.  Visual Studio has the capability of showing verbose data binding trace information.  This can help you get to the bottom of the issue quickly.  In my case, my output window was not showing any data binding information…  You’ll need to adjust some Visual Studio debugging options.  First, ensure that all debugging information is not being sent to the immediate window.  Tools >> Options >> Debugging >> “Redirect all Output Window text to the Immediate Window”.  Uncheck that.  Next, Tools >> Options >> Debugging >> Output Window >> WPF Trace Settings >> Data Binding.  Select an option here.  When I was debugging I wanted to see everything going on so I selected “Verbose”.  Unfortunately, the information did not tell my anything as to why my two-way binding was not updating the underlying object’s property…

 

2)  Understand Dependency Properties and the INotifyPropertyChanged interface.  In order for the UI to properly show object property changes or to perform two-way binding, the property which it binds to needs to be a dependency property or the object needs to implement the INotifyPropertyChanged interface so that the UI properly reflects the object’s property changes and the UI can also update the object’s properties (if two-way binding is enabled).  In my case, my UI control was bound to a dependency property which its type also implemented INotifyPropertyChanged and my two-way binding still didn’t work…

 

3)  Are you using a Converter in the data binding expression?  Sometimes converters can throw exceptions and break the binding.  Put breakpoints in your converter and ensure the conversions are not throwing exceptions and breakpoints are being hit.  In my case, the convert from was being hit, but the convert back (updating the object from the UI via two-way data binding) was not being hit.  So one-way was working, but two-way was not…

 

4)  Are you binding a Custom Control which overrides/overloads the property which you are wanting to bind to?  This was the problem for me.  I was wanting to two-way bind the “Text” property of a custom user control to my object’s property.  The custom control was inheriting from TextBox and they were overloading the “Text” property.  Apparently this broke the two-way data binding.  I figured it out by simply dropping a vanilla TextBox onto my UI and binding it’s “Text” property to my expression and two-way binding worked great.  So, when things get really hairy, simplify what you are doing and add more and more things back into the picture until something breaks.  Troubleshooting 101 for the win.

Bookmark and Share

The Present State of Computing and .NET

September 23, 2011 07:13 by wjchristenson2

I’ve been programming for the last 10 years predominantly with Microsoft technologies and languages.  I can develop windows, web, silverlight applications & services easily in .NET.  The .NET Framework provides a great foundation to develop applications which can span many computing technologies and platforms.  Handheld devices (phones & tablets) is where the future end-user computing is going to be.  Unfortunately, knowing how to program .NET applications doesn’t appear to be able to assist much here.

The 2 dominant mobile operating systems are Apple’s iOS and Google’s Android.  Microsoft has tried to get in the market, but has had difficulty doing so.  My .NET skills can make mobile applications on Windows Mobile, but the OS is rarely supplied on new phones.  As of February 2011, Apple, Android, and Blackberry comprise of over 90% of the smartphone market in the US.  So even if you could use your .NET skills in the mobile market, you would be reaching less than 10% of mobile users.

Yeah, Yeah.  I’ve read and seen Windows 8 and its “Metro” apps offer.  But penetrating a mobile market that’s already dominated by Google and Apple will be no easy task.  And if they beat the odds, I already know .NET and XAML… so I can easy kick out a metro app if I needed to.

After sitting back and watching what has been happening in the mobile market over the last few years, I don’t think my .NET expertise is enough to sufficiently program for mobile users.  So it’s time to step up my game and tackle programming for a new OS:  Google’s Android.

Why Android?  Well, I think both Apples iOS and Google’s Android are here to stay.  Both are attractive and legitimate mobile operating systems.  In the end, I believe Google’s Android will reach more users and devices than Apple’s iOS and some statistics show that it already has in the smartphone sector.  ComScore reported for February 2011, the Android platform now accounts for 1 in 3 smartphone users.  Even more interesting, recent trends show Android has been taking more and more of the smartphone market share at an increasing rate.  February 2011 had Android taking the largest increase in smartphone market share over 3 months and is #1 at 33%.  Obviously trends may change, but this is showing that the Android OS has continued to grow at a rapid pace and has finally past Apple’s iOS on smartphones.

If you factor in tablets with smartphones, Apple’s iOS takes the cheese with 53.04% market share.  However Google’s Android did not come out with tablet support until 3.0.  Hardware manufacturers have not released a competitive tablet device with Google’s Android OS on it to successfully compete against the iPad.  That will eventually change.  Amazon recently announced it is going to release its own Kindle tablet running Android and more devices are sure to come.

So why do I think Google’s Android will have the higher mobile market share in the end?  Let’s look back in recent history to the 1990’s.  Apple has always controlled the end-to-end hardware and software product offering.  This yields a less buggy and better optimized product.  On the flip side, Microsoft had Windows that could run on many hardware manufacturers’ products.  Developers took to Microsoft’s OS & development technologies for many reasons and propelled it to have the dominant market share.  Is the mobile battle going to end the same way?  This time, Google’s Android is Microsoft's Windows.  I don’t necessarily think it will win out on innovation or quality of products, but it will prevail when it comes to market share.

So why is market share so important?  Apple could release the next new iPhone or iPad which rocks Android devices and trends could favor Apple.  I would agree that this will happen and things will go back and forth, but when it comes to developers developing applications, they typically stick with the dominant platform which will reach the most users and will be around a long time.  They also migrate to the platform which is easier to develop for.  Android is showing that it is a dominant mobile OS, is trending up, and it will be difficult to dislodge.  It’s SDK’s, IDE’s, tutorials are all free, easy to learn, and run on Windows as well as Mac.  As a result, I think Google’s Android will have the majority of market share both with smartphones and tablets in the not-to-distant future.

Companies looking to create applications will obviously want to develop a mobile application and reach users where they are at.  Because Google’s Android and Apple’s iOS is the majority of mobile operating systems, that’s where the work will be.

So, like I said, I’m stepping up my game and I plan on learning how to program for Google’s Android.  As a result, I plan to blog on it occasionally as I progress.  I’ve just got my 1,000+ page Pro Android 3 book from Amazon today… So here I go.  Weeeee.

Bookmark and Share

n-Tier Architecture Introduction – Part 2

July 29, 2011 02:19 by wjchristenson2

In my previous article (part 1), I shared what an n-Tier architected application is and why you should familiarize yourself with the concepts behind them.  In this article, I will take this a step further and show you how you can create a simple n-Tier architected ASP.NET application using Visual Studio 2010.  I won’t create all the CRUD (create, read, update, delete) operations for the business objects, but it should give you a strong start to creating a simple multi-tiered ASP.NET application.

When we are finished, your Visual Studio solution will have the following layers:
1) SalesSystem.Web - ASP.NET website
2) SalesSystem.Presentation - Presentation Controllers
3) SalesSystem.BusinessObjects – Business Logic Layer (Objects)
4) SalesSystem.Core – Core Utility Objects

We will start by creating an empty Visual Studio solution and then adding projects to it.  Each project acts as a logical layer (tier).  Projects can reference each other via the use of project references.  When you build the Visual Studio solution, it will build each project for you automatically.  Before I show how to create an empty solution and add projects (logical layers/tiers) to it, here’s a brief explanation of what each project will do.

SalesSystem.Core – I used this project to contain generic utilities, functions, extensions, helpers, etc which can be used across any new application you may develop.  Think big.  You don’t want to reinvent the wheel if you don’t have to.  I thought I may be developing more web applications in the future, so a common task is reading connection strings from the web.config.  So I wrote a utility object for the web.config and put a base class to be inherited on all web pages in there too.  This project/assembly is used by each of our other tiers.

SalesSystem.BusinessObjects – I made this project to house all of my business objects.  I didn’t take the time to create an official DataAccess Layer… but business objects often are made to interact with one particular data source type.  In this case I am interacting with a SQL Server and the connection string to run the queries on is passed in via a connection string to still maintain a dynamic and flexible approach.  Regardless of merging the business objects in with data access routines, I am able to use these business objects going forward easily as long as the application (whatever type it is) supports CRUD operations on a SQL Server.

SalesSystem.Presentation – Okay, this project may be a bit confusing for most.  I made another tier/layer to act as the liason between the UI and business objects.   I have a couple of reasons for this.  First, it makes calls simpler from ASP.NET page code behinds.  An ASP.NET page can have one controller which does a myriad of tasks (page centric) on many types of objects.  Second, it will help with automated unit testing in the future if we decided to take it to that level.  Your presentation layer should essentially be presenting the UI and capturing data from the user.  The less “clutter” you can put in the presentation the better.  So using presentation controllers can assist here.

SalesSystem.Web – This project is the presentation layer and we are using an ASP.NET website to do this.  Our objective in this layer is to simplify the logic down to presenting and capturing data from the user.  Any business object acquisition or saving should be done through the controller.  This makes the UI code much cleaner and simpler.

Here’s what the final solution will look like:

Step 1: Create an Empty Solution – First we want to create an empty Visual Studio solution so we can add our projects (layers/tiers) to it.  Below is a screenshot on how to do this.

Step 2: Add Projects to the Solution – Next we want to add projects for each tier we want to our solution.  We have 4 layers that we want to develop, so we’ll add 4 projects to the solution which will facilitate logical layers.  Each layer may be a different project type.  For instance, the presentation tier (SalesSystem.Web) will be an ASP.NET web project.  However our core, presentation controller, and business projects will all be windows class libraries as they simply have .NET classes in them.  Below are screenshots to help facilitate how to add the projects to our empty solution.

Step 3: Add Project References – The final step to getting your ASP.NET n-Tier architected solution to work is to setup project references to each tier can communicate, consume, and/or interact with each other.  Think of project references as “inheritance”.  If you need to use a tier’s (project’s) functions or data types, you need to add a project reference (or inherit) from it.

SalesSystem.Web References - We know that SalesSystem.Web will need to make calls to the SalesSystem.Presentation controllers which will return objects from SalesSystem.BusinessObjects.  So the SalesSystem.Web will need project references to both projects.  It will also need a project references to SalesSystem.Core because that is our core project which we have our core objects & utilities that we can use everywhere.

SalesSystem.Presentation References – This tier doesn’t care about the UI.  It makes calls to the business object layer.  Therefore it only needs a reference to SalesSystem.BusinessObjects.  Again, add a reference to SalesSystem.Core as well.

SalesSystem.BusinessObjects References – This tier is only concerned with its own business objects and interacting with the database.  It doesn’t care about what presentation layer is employed to use them… so the only references you need is to SalesSystem.Core.

Here is how you add a project reference:

Step 4: Code – I’m not going to go into detail on how to code the tiers.  Instead I am attaching the solution which includes the code in each tier to acquire customer sales from the AdventureWorks database using our n-Tiered solution.  The end deliverable is an ASP.NET website which a page’s codebehind class makes a call to our presentation controller tier which makes calls to our business objects and back out again.

Summary - Imagine taking this application to the next level and offering a Windows WPF UI option.  We could reuse the same presentation controllers, core, and business object tiers.  We would simply replace the ASP.NET web presentation tier and most of our work would already be done.  If a call to the database was broke, we’d fix it in one business object and be done.  One place, one time.  We wouldn’t have to be searching through all code-behind files trying to locate every instance of the problem.  If you had a UI designer/coder, they could be designing the look of the web pages while you focused on the business objects and back-end tiers (parallelism).  There are many reasons why applications are constructed like this.

Hopefully this helps you get a good start to understanding n-Tier architected applications and how you can accomplish this in Visual Studio using logical layers with VS projects and project references.

n-Tier Architecture – Part 1
n-Tier Architecture – Part 2

SalesSystem_Soln.zip (792.10 kb)

Bookmark and Share