Monday, August 6, 2012

TouchIt: jQuery Plugin for Touch events

Touch to mouse converter and callbacks for touch and pinch events

  • X, Y coordinates for touch events
  • Scale for Pinch events
  • Convert touch events to mouse events (move and click)
This easy to use jQuery Plugin is based on a 360 Zoom Plugin I wrote to work on desktop and mobile devices, I have pulled the mobile elements in to this Plugin.

I have hosted the jQuery Plugin for Touch events in Git Hub.

This includes two examples:

X, Y, Scale Callback Example

Demo 1: touchit-demo.htm

This shows the call backs from the TouchIt plugin:

The plugin will call back to the functions passed as parameters.

When a single finger is touched, the X, Y coordinates are returned to the caller.
When a single finger is moved, the X, Y coordinates are returned to the caller for each movement.
When a single finger is lifted, the X, Y coordinates are returned to the caller.
When two fingers are moved, the Scale is returned to the caller (1 = original position, > 1 = fingers move apart, < 1 fingers move together). 


<span id="touched" style="width:10px;height:10px; ">Touch and pinch the blue square...</span>
<div id="touch" style="width:250px;height:250px;background: blue; "></div>


$( "#touch" ).touchit({
onTouchStart: function (x, y) {
$("#touched").text('Touch Start ' + x + ':' + y);
onTouchMove: function (x, y) {
$("#touched").text("Touch Move " + x + ':' + y);
onTouchEnd: function (x, y) {
$("#touched").text("Touch End " + x + ':' + y);
onPinch: function (scale) {
$("#touched").text("Pinch " + scale);

Convert touch events to Mouse events

This shows how the TouchIt plugin can be used to convert touch events in to mouse events.

In the example you will see how the jQuery UI Slider can be extended with a simple call to the TouchIt Plugin to allow for control from a mobile device.

The only change needed to add mobile functionality is to add the .touchit() code to the slider:


<div id='zoomSlider'></div>


$( "#zoomSlider" ).slider({
orientation: "vertical",
min: 0,
max: 100

Where else can I use the TouchIt Plugin?

If your site has some functionality that doesn't currently work for mobile, you should be able to add the TouchIt Plugin to the DIV or other element on the page and get the feedback from the touch events.  See Demo 1 above how to implement this - you do not need to add the call backs.

Supported devices

This is written to pick up the ontouchstart event detection.  I have tested it on iPhone, iPad and Andriod devices.  As other devices come along that support this they will 'just work'.

The pinch callback works for iPhone and iPad, I don't believe it works for Android yet, although I'm interested to hear from your if you know.

Thanks for looking.

Saturday, March 5, 2011

Introduction to WCF

This blog shows how to create a Visual Studio 2010 C# console application that hosts a Windows Communication Foundation (WCF) service exposing string manipulation methods.

The purpose of WCF is to simplify the communication of distributed applications on Windows. Because WCF uses RESTful communication and implements SOAP it can therefore operate with multiple platforms.

Creating the service

In Visual Studio 2010:
File > New > Console Application > Name: Service

Open the project’s properties and change the namespace to: Glyde.ServiceModel.Examples
Change the Program.cs namespace to use the Glyde.ServiceModel.Examples namespace.

Add System.ServiceModel.dll to the project.
Create a new public interface: IStringManipulator.cs, this will expose methods to add two strings together, replace part of a string and remove a substring.

Add a reference to System.ServiceModel in the interface.

Add a service contract to the interface:

[ServiceContract(Namespace = http://Glyde.ServiceModel.Examples)]

And add [OperationContract] to the exposed methods:

string Add(string s1, string s2);
string Delete(string s1, string s2);
string Replace(string s1, string s2, string s3);

Next we create a WCF service contract.

Create a StringManipulatorService class to implement the interface methods.

public string Add(string s1, string s2)
return s1 + s2;

public string Delete(string s1, string s2)
return s1.Replace(s2, string.Empty);

public string Replace(string s1, string s2, string s3)
return s1.Replace(s2, s3);

In the Program.cs create a URI object to serve as the base address – this will be used by the client to call the service:

Uri baseAddress = new Uri("http://localhost:8000/GlydeServiceModelExamples/Service");

Create a service host for the StringManipulatorService

ServiceHost selfHost = new ServiceHost(typeof(StringManipulatorService), baseAddress);

Add a service end point

new WSHttpBinding(),

Enable the metadata exchange, for HTTP:

ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true;

Start the service
Add a
…to keep the service running.
Call close to stop the service from running:

Create a WCF client

Create a console application called Client, implement this in a separate application so the service and client can be run from a separate debug solutions – this will help your understanding and allow you to debug both.

Add System.ServiceModel.dll to the project and add the reference to Program.cs

How to use the service utility to create the client proxy

Switch back to the service solution and start the console application by F5

Start the Visual Studio command promptNavigate to the client projectRun the ServiceModel

Metadata Utility Tool to create the client code and configuration file (generated proxy):

svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/GlydeServiceModelExamples/Service

This will generate the files in the client project folder:

Add these to the client project by using ‘Add existing item’.

Remember, if you modify or add new methods in the service, the client proxy will have to be recreated.

The app.config specifies the location of the service, the binding and contract interface.
Call the service via the proxy

Create an endpoint address and a WCF instance client in the Program.cs.

StringManipulatorClient client = new StringManipulatorClient();

By adding the app.config and generatedProxy.cs the StringManipulatorClient is automatically available in the project.

Don’t forget to add System.ServiceModel to the project and add a reference in the Program.cs file.

Make a call to the client:

string value1 = "Hello Hannah, ";
string value2 = "how are you today";
string result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

value1 = result;
value2 = "Hannah";
string value3 = "Emma";
result = client.Replace(value1, value2, value3);
Console.WriteLine("Replace({0},{1}) = {2}", value1, value2, result);

value1 = result;value2 = ", how are you today";
result = client.Delete(value1, value2);
Console.WriteLine("Delete({0},{1}) = {2}", value1, value2, result);

Close the client after use:


So that’s it, I hope I have wept your appetite to learn more about WCF – As ever I have uploaded my source to GitHub click here to get the source from my introduction to WCF.

Tuesday, March 1, 2011

Agile Software Development

What does it mean to be Agile?

There are a number of strategies and working practices available for Agile software development, I have included the process that works for me; this is based on my real life experiences working with stakeholders (more about these in a moment), developers and testers.


What is Scrum? During a project the customers can change their minds about what they want and need (also called churn) these challenges cannot be addressed in a planned manner. Scrum uses a pragmatic approach, in most instances these problems cannot be fully understood therefore a focus on capitalising on the team's ability to delivery quickly and respond to emerging requirements.

Daily Scrum

A stand up meeting called a Daily Scrum takes place at precisely the same time every day - 10 am is a good time as the team members have had the time to know what they are doing by this point. If your team is distributed then use a video conferencing tool (e.g. Skype).

There are three roles in a Daily Scrum:

On the officials in the 2007 Tech-Texas game in Austin:

"It's a little like breakfast; you eat ham and eggs. As coaches and players, we're like the ham. You see, the chicken's involved but the pig's committed. We're like the pig, they're like the chicken. They're involved, but everything we have rides on this."


The developers and testers play the part of Pigs, they have no longer than one minute to describe what they did since the last Daily Scrum, what they are doing today and what blockers they think they may have to do today's tasks.

If anyone has an questions or comments they should take place outside of the Daily Scrum.


The stakeholders (customers) play the role of chickens - they can listen but not speak. If they have any questions they should go through the Scrum Master outside of the Daily Scrum meeting.

Scrum Master

The senior member of the software team usual a Team Leader will take the role of Scrum Master, they ensure the Daily Scrum starts on time and that everyone takes turns to speak. The Scrum Master can also be a Pig.


Sprints are a part of the Scrum methodology.

Sprint planning meeting

A sprint cycle lasts for two weeks.

Select the work to be done

User Stories

The Scrum Master meets with all the Stakeholders in a single meeting to gather the user stories.

In order to process the user stories quickly cards should be used to write a short sentence that describes the functionality in the form:

As a [role] I want [requirement] so that [business need].

The Stakeholder writes the card and passes it back to the Scrum Master, only one card is completed at a time after being discussed with the other Stakeholders.

The number of User Stories gathered in the Sprint Planning Meeting will vary depending on the size of the team for a small team of four developers, ten User Stories would be a fair number.

Each User Story will have a number of scenarios written on the back; these will form the tests.

Story Points and Planning Poker

After the User Stories are gathered the Software Developers and Testers go through each User Story and assign it a number of Story Points.

The highest number is the most complex and time consuming, the lowest is the simplest and shortest: 0,1,2,3,5,8,13,20,100. 100 alerts you to the fact that you have an Epic Story which should be split in to multiple stories.

A game of Planner Poker should be played, this can be done with Planning Poker cards or if your team are distributed - this can be done over an IM - The Scrum Master asks everyone to get their numbers ready and all hit Enter at the same time.

The Scrum Master records the Story Points against each User Story, no more than 5 minutes should be spent on each User Story.


The User Stories with Story Points should be reported to the Stakeholders in person, they then order the cards by relative priority.

Tasks and durations

The Scrum Master asks each of the Developers if they have any holiday booked for the next two weeks, this is subtracted from the number of hours in the Sprint.

A Sprint is always two weeks long, due to outside forces, answering emails, meetings, urgent issues (real life) I allow for six hours per day on a Sprint. So if a Developer is in then they are available for sixty hours for the two weeks. I recommend using Pair Programming so this will be the hours shared between your developers (i.e. sixty hours for two Developers).

Get the Developers and Testers together, take each User Story ordered by highest priority first, split in to Tasks i.e. Analysis, Design, Database schema changes, Development, Testing, Deployment. Speak directly to the individuals most likely to implement the User Story (Developers and Testers should work on a single User Story rather than a single Task), ask them to say how long they think the Tasks will take. The other Developers and Testers can verify this.

Create a table in a spreadsheet with sixty at the top and subtract the hours for each User Story from the Developers available hours, eventually this will get to less than zero, all User Stories in positive numbers will be what the team can commit to.

The Scrum Master meets with the Stakeholders and provides them with the list of User Stories that can be committed to in the two week Sprint.

Story board

A Story board is created for each Sprint, User Stories have a state during the Sprint:

Backlog > In progress > Code ready for review > Code reviewed > QA Ready > [QA Passed][QA Failed] > Deployed

There is another state called Waste, this is used for any duplicate or erroneous User Stories.

The Developers are encouraged to move their User Story to the next Sprint, this should be viewable by the Stakeholders, web based story boards are available this is useful for distributed teams.

Sprint review meeting

Once the two week Sprint is completed, the Developers and Testers meet with the Stakeholders to review the work that was completed and not completed. A demonstration of the completed work is shown to the Stakeholders. There should be a four hour limit to this meeting.

Any User Stories not completed in the Sprint go in to the Sprint Backlog.


The number of Story Points completed in the Sprint gets recorded against the User Story, this becomes the velocity of the team.

After three Sprints the velocity will start to even out and give the Stakeholders an idea of the amount of work that can be done in a Sprint.


There are two charts I have found useful, the first is a Daily Burndown Chart, this is where the number of hours the Developers spent on the Sprint each day is recorded against the number of available hours.

The second is the Release Burndown Chart, this shows the estimated number of Story Points in the Sprint against the actual Story Points completed, this chart can be extended to also include the velocity.

Sprint retrospective

This meeting is attended by the Developers and Testers, all team members reflect on the past Sprint, make continuous process improvements , what went well and what can be improved, there should be a three hour limit to this meeting.

Extreme Programming (XP)

There are a number of XP elements, the one I find most useful is Pair programming. Software developers sit at a single Development workstation they take it in turns to be at the keyboard - known as the Driver, as a Developer types they describe what they are doing to the Observer. The Observer reviews the code as it is being typed, therefore code reviews do not need to be done after a task is completed and the quality of the code is far superior than if done on their own.

One common issue is one of swapping, if this does not feel natural then switching every thirty minutes is a good compromise. Other benefits include a lack of fatigue, knowledge sharing, a boost in morale, not skipping unit tests. New Developers can pick up the workings of the team quickly and people are less likely to interrupt a pair of programmers.

Other distractions can be email, one solution to this is to have a separate PC that individuals can use to read their email.


By employing these Agile Software Development methods my team are able to work in a more efficient way, there is little or no context switching between tasks, the quality of work has improved and the implementation matches the requirements closely. The Stakeholders feel engaged with the development process and see results more frequently than through the waterfall model.

Sunday, January 16, 2011

Introduction to Ruby on Rails


I am a .NET developer and have been programming ASP.NET in C# for a number of years, I have been hearing some pretty wonderful stuff about Ruby on Rails and wanted to see for myself. This post is about my discovery of this framework from the unknown to creating an ecommerce site from scratch.


IDE: NetBeans IDE 6.8
Ruby Platform: Build-in JRuby 1.4.0
Server: GlassFish v3 Domain
Database: MySQL

The application is simple and one that followers of this blog will recognise. It is a simple eCommerce shop with the data structured in to Products, Categories and Basket.

The purpose is to show how quickly an application in Rails can be created and takes you through the steps to set this up.

You will have loaded in the Ruby and Rails plugin for NetBeans

Create the Project
In the NetBean IDE select File > New Project
Give your project a name e.g. ‘RailsShop’
See the environment section above for other settings.
Unselect ‘Add Rake Targets to Support App Server Development’
Database Configuration: Specify Database Information Directly
Database adapter: mysql (jdbc)
Database name: RailsShop_development (it will also create ‘_test’; ‘_production’
User name: localhost
Password xxxxxx
Install rails: 2.3.4

Create and set up the database
Right click on the project in the Project window > Run/Debug Rake Task> db:create

Add the tables
Right click on the project > Generate
Generator: model
Arguments: category name:string
Arguments: product category_id: integer name:string price:decimal url:string
Arguments: basket product_id: count:integer

Note that the model is created in the Models folder – this does not define the fields in the model, these are specified in the Database migrations folder. The database will not be updated until the database migration has been called.

The convention is to use the foreign key to be the model_id.
Primary key ids are created automatically as are time stamps.

Define the association between the models
A basket has products, edit the \models\basket.rb to:
class Basket < ActiveRecord::Base
belongs_to :product
and so on with the rest of the models

class Category < ActiveRecord::Base
has_many :products
class Product < ActiveRecord::Base
belongs_to :category
has_many :baskets

Update the database
Right click on the project > Migrate Database > To Current Version

If you get an error like this:
The driver encountered an error: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown database 'railsshop_development'
rake aborted!

The db:create failed silently in which case you will need to create the database manually – I did this in mySQL Workbench – this is a known issue and is discussed in the Knowledge base.

You should be able to update the database to the current version.

If for any reason you need to reset the database to its empty state run:

Right click on the project > Migrate Database > To Version 0 - Clear

Adding sample data
Right click on lib\tasks
Create db_load_dev_data.rake (using Other\Empty file) – these isn’t support for the .rake extension by default.
namespace :db_load_dev_data do
desc "This loads the development data."
task :seed => :environment do
require 'active_record/fixtures'
Dir.glob(RAILS_ROOT + '/db/fixtures/*.yml').each do file
base_name = File.basename(file, '.*')
#say "Loading #{base_name}..."
Fixtures.create_fixtures('db/fixtures', base_name)
desc "This drops the db, builds the db, and seeds the data."
task :reseed => [:environment, 'db:reset', 'db:seed']
The code reads the yml files and loads the data in the database:

Create a new fixtures folder under the \db\ folder and create a file for each table i.e. products.yml.

Note: in Files view \db\ equates to Database Migrations in the Projects view.

Right click on the project > Run/Debug Rake task… > [refresh tasks] > db_load_dev_data: seed

It is important to specify the id of using test data – this allows for the foreign keys to be specified otherwise the id will automatically generated.

Here is an example with two products:

Concorde Bikes PDM Team Replica Road Bike:
category_id: 1
name: "Concorde Bikes PDM Team Replica Road Bike"
price: 699.99
url: "concorde-bike-pdm-ind.jpg"

Kestrel Talon Tri 105 2010:
category_id: 1
name: "Kestrel Talon Tri 105 2010"
price: 147.99
url: "kestral-talon-tri-ind.jpg"

To remove the sample data right click on the application > Run/Debug Rake Task… > db_load_dev_data:reseed

To utilise shorthand like:
<%= link_to, category%>

Ensure you have entries in the \Configuration\routes.rb:
map.resources :category
map.resources :product
map.resources :basket

More on this later.

Create the controllers
The scaffold generation will create controller methods for CRUD application, as we are displaying the site in read only we will not create with a scaffold.

Note, do not delete the application_controller.

RailsShop > Generate > controller > Category. Views: index, show

This creates:

Edit the controller index – this is used to display all the categories

def index
@categories = Category.all

respond_to do format
format.html # index.html.erb
format.xml { render :xml => @categories }

Show is used when a single category is selected:

def show
@category = Category.find(params[:id])
respond_to do format
format.html # show.html.erb
format.xml { render :xml => @category }

We also need to create a controller for product and basket.

Product views: show
Basket views: index, update, delete, add_to_basket

Partial Views
As we want to show the categories, products and basket summary on the same page we need to use partial views.

In rails the partial views need to be located in the same folder as the containing page.

So for our example the Category show view is set out as:


As you can see the convention in rails is to name partial views with an underscore prefix.

These are included in the show.html.erb as:

<%= render :partial=>"category_list", :locals=>{:categories=>Category.find(:all)} %>
<%= render :partial=>"product_list", :locals=>{:products=>@category.products} %>
<%= render :partial=>"basket_list", :locals=>{:baskets=>Basket.find(:all)} %>

The Category.find(all) does not call the controller but gets the list of categories and sets the partial’s categories variable with the result of the find call.

The product_list is interesting to us, and really sets out as a great example of Rails at work making our lives easier:

The show.html.erb has a variable @category, if we refer back to the model definition, Category ‘has_many :products’ and this along with the fact that the database definition defines that Product has a category_id we can access the list of products from the @category variable and pass it to the partial as a products variable.

Partials can also be accessed if they are not in the same folder – the same partials are included in the \Views\product\show.html.erb like this:

<%= render :partial=>"category/basket_list", :locals=>{:baskets=>Basket.find(:all)} %>

Link to
Rails provides a nice short hand to call controllers from views to call an action the first I touched on earlier:

In the \Views\category\show.html.erb we set the products local variable in the _product_list.html.erb this is then used in a ‘for loop’ like this:

<% products.each do product %>
<%= link_to, product%>
<% end %>

So the product is the instance of each iteration of the loop, so in the link_to what actually renders is: http://localhost:8080/RailsShop/product/2107859366, this will automatically call the product_controller show action passing the 2107859366 as a parameter id which is accessed like so:

def show
@product = Product.find(params[:id])
respond_to do format
format.html # show.html.erb
format.xml { render :xml => @product}
In certain cases we need to explicitly identify the controller, action and parameter we are passing, this can be seen when adding a product to the basket:


We have access to the local variable @product the id of which is used as a parameter to the basket which is called:

<%= link_to 'Add to basket', :controller => "basket", :action => "add_to_basket", :id =>>

If we take a look at the ‘add_to_basket’ action:

def add_to_basket
#how many of this product is in the basket?
@product_id = params[:id]
@basket = Basket.find_by_product_id(@product_id)

if !@basket.nil?
@count = @basket.count + 1
@basket.update_attribute(:count, @count)
@basket =
@basket.product_id = @product_id
@basket.count = 1

redirect_to :controller => 'category', :action => 'index'

The code above takes the id parameter, uses it to find the basket, if the basket does not exist a new one is created with a single product otherwise the current number of products is obtained, incremented and stored in the data layer using the update_attribute method.

Another call on the basket allows us to remove the product, this is done by:

def delete
@basket = Basket.find(params[:id])
redirect_to :action => 'index'

If we compare the last two controller actions we can see the redirect can be both explicit in describing the controller and implicit in that the index will call the Basket for the Basket delete action.

Submitting data in a form
We need to be able to modify the quantity of products in the basket, so how does it plumb together?

We call the basket index from an edit button on the _basket_list.html.erb:

<%= link_to 'Edit basket', :controller => "basket", :action => "index" %>

This calls the basket_controller index action:

def index
@baskets = Basket.all

respond_to do format
format.html # index.html.erb
format.xml { render :xml => @baskets }
This renders the \Views\basket\index.html.erb

<% @baskets.each do basket %>
<% form_for(basket) do f %>
<td class ="productname"><%= %></td>
<td><%= basket.product.price %></td>
<td><%= f.text_field :count, :class => "qty" %></td>
<td><%= basket.product.price * basket.count %></td>
<td><%= f.hidden_field :id %></td>
<td><%= f.submit 'Update', :class => "updatebutton" %></td>
Note the hidden id field and form placement within the ‘for loop’, the submit button will call the basket_controller update method, more on the class settings in the style section later:

def update
@basket = Basket.find(params[:id])
redirect_to :action => 'index'
And then re show the basket with the new line totals.

Making it look good

To maintain consistency across the site we need to create a page template. We do this by creating a new erb in \app\views\layouts\ - as ever the naming conversion is important here. We can either create one for the site named ‘application.html.erb’ or for each controller i.e. ‘category.html.erb’ the <%= yield %>is used on the page to donate the location to load the views.

We set up styling in a CSS file in \public\stylesheets\default.css – this is referred to in the layout page as:

<%= stylesheet_link_tag 'default.css' %>

If we want to style the delete link in the basket and add a confirmation question we do the following:

<%= link_to 'x', {:controller => "basket", :action => "delete", :id =>}, :class => "delete", :confirm => "Are you sure?"%>

Note the curly parenthesis; without it the class and confirm will be added to the link URL.

Load images on the page using the image_tag, this automatically looks up images in the \public\images folder in the web application.

<%= image_tag(@product.url) %>
Run the application
I want the first page to load with the categories displayed; this is done by adding the following to \Configuration\routes.rb:

map.root :controller => "category"
To run the application from NetBeans:

Right click on the Project > Run


Firstly the structure is all about creating a real world environment the segregation between Development, Test and Production make sense.

When changes are made to the database it is easy to rollback changes and recreates the tables using the rake tasks.

The fields are not specified in the models only in the database migrations, this allows for encapsulation in code from the data layer.

I like the convention over configuration methodology; this has saved time creating rules for each instance.

So in summary then I like rails, it has a very structured approach to development and database schema changes as you can see things can be developed quickly without code bloat using the DRY methods.

I have uploaded the code to GitHub.

Sunday, November 14, 2010

MVC 2.0 with DB4O

Object-Oriented Databases

The aim of this blog is to firstly give an introduction of what an object-oriented database is, showing how to install DB4O, any shortcomings and finally to give an example in a C# .NET 4 MVC 2 shopping cart project.

What is an Object-Oriented Database?

An object-oriented database is a database model (OODBMS), unlike relational database management systems which hold data in relational rows and columns the OODBMS holds information as objects in the same way as object-oriented programming.

OODBMS is useful when used with object-oriented programming as no translation needs to take place between objects in code and objects in a database, the same objects can be referenced in each.

Why choose OODBMS over RDBMS?

OODBMS are faster than RDBMS and can be programmed without making changes to the full system.

As navigation with OODBMS is done with pointers data access is done very efficiently.

On the negative, OODBMS are optimised for specific search routes and will tend to be slower for general-purpose queries than relational however direct object references may be maintained to allow for both types of access.

So what OODBMS should I choose?

This depends on the language you are programming in, I’m pro open source and the language I am writing for this example is in .Net I have selected the highly rated db4o:

This o-o database has libraries for both Java and .NET, for a full list and comparison of OODBMS see this wiki link:

Installation guide of db4o for .NET

Download the production release from the following location:

The following steps are for db4o-7.12-net35.msi

Setup type: Typical
Accept the default installation location
Go though the defaults and click Finish to complete the installation

If you have more than one version of Visual Studio installed you get the choice of plugin to install.

‘To proceed with the installation of the Object Manager Enterprise plugin for VS please choose the appropriate shortcut from your db4objects Start menu’

The plugin does not work with Microsoft Visual Web Developer – you will need the fully blown version of the Visual Studio.

The tutorial automatically loads on completion of installation from here code examples can be obtained for processes such as opening databases to CRUD to LINQ and SODA queries.

The only DLL you need to build against is C:\Program Files\db4o\db4o-7.12\bin\net-3.5\ Db4objects.Db4o.dll.

Db40 has its own caching, the default is ‘CachingStorage’, db4o uses a LRU/Q2 caching strategy.

Load balancing


Serialized objects
As soon as objects are serialized the object-identity is lost, this is typical in web-scenarios or web-services, there are a number of different ids, one of which is “ID-Field On Classes With a ID-Generator”.

The project

I developed this MVC 2.0 project using Visual Studio 2010 Ultimate.

The source is available on GitHub.

It is assumed you have working knowledge of the MVC framework.

The only DLLs I referenced from my application are:


The second was included as I used Linq queries on the object database.

To keep this example simple I have not addressed the issues of load balancing and isolated storage.

I have created a new class in the Models folder: StoreDB4O

In the class I define the yap file – this is the file DB4O uses to store the database.

   1:  readonly static string StoreYapFileName = Path.Combine( 
   2:    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), 
   3:    "store.yap"); 

...and obtain it using the following method:

   1:  private static IObjectContainer GetDB()
   2:  {
   3:    returnDb4oEmbedded.OpenFile(Db4oEmbedded.NewConfiguration(), StoreYapFileName);
   4:  }

In order to get test data in to the project I have created a method called LoadTestData(), this is called from Application_Start() in Global.asax and removes the content of the database:

   1:  File.Delete(StoreYapFileName);

And stores the sample data in Category and Product objects:

   1:  IObjectContainer db = GetDB();
   2:  try
   3:  {
   4:      Category category = new Category { CategoryId = 1, Name = "Bikes" };
   5:      StoreCategory(db, category);

   1:  private static void StoreCategory(IObjectContainer db, Category category)
   2:  {
   3:      db.Store(category);
   4:  }

There is nothing special about the objects that are stored in the database.

   1:  namespace MvcDB4O.Models
   2:  {
   3:      public class Category
   4:      {
   5:          public int CategoryId { get; set; }
   6:          public string Name { get; set; }
   7:      }
   8:  }

In addition to storing data in object I have implemented methods that return data using Linq queries as example of which returns a list of products by category name:

   1:  public static IEnumerable<Product> GetProductsByCategoryName(string name)
   2:          {
   3:              IObjectContainer db = GetDB();
   5:              try
   6:              {
   7:                  return new List<Product>(from Product p in db
   8:                                            where p.Category.Name == name
   9:                                            orderby p.Name
  10:                                            select p).Distinct(new ProductByNameEquality());
  12:              }
  13:              finally
  14:              {
  15:                  db.Close();
  16:              }
  17:          }

One important thing to mention is to always return a copy of the object from the database otherwise the connection of the database will be required to be open when accessing data from the objects.

Another gotcha is that when updating existing data, the same object will be required from the database within the same session i.e. In this example, the basket has the count decremented and the basket is then placed back in the object database, if this was done in two separate sessions with the database a second basket will be added to the database and the existing one will also exist.

   1:  public static IEnumerable<Product> GetProductsByCategoryName(string name)
   2:          {
   3:              IObjectContainer db = GetDB();
   5:              try
   6:              {
   7:                  return new List<Product>(from Product p in db
   8:                                            where p.Category.Name == name
   9:                                            orderby p.Name
  10:                                            select p).Distinct(new ProductByNameEquality());
  12:              }
  13:              finally
  14:              {
  15:                  db.Close();
  16:              }
  17:          }

The MVC structure here is nothing new, the Home\Index.aspx shows the categories and a link to the StoreController Browse method:

   1:          <% foreach (MvcDB4O.Models.Category category in Model.Categories)
   2:             { %>         
   3:          <li>
   4:              <%: Html.ActionLink(category.Name, "Browse", "Store", new { category = category.Name }, null)%>
   5:          </li>     
   6:          <% } %>
   8:          public ActionResult Browse(string category)
   9:          {
  10:              // Retrieve Category and Products from database     
  11:              var storeBrowseViewModel = new StoreBrowseViewModel()
  12:                  { 
  13:                      Category = StoreDB4O.GetCategory(category),
  14:                      Products = StoreDB4O.GetProductsByCategoryName(category)
  15:                  };
  16:              return View(storeBrowseViewModel);
  17:          }

The View Model is populated and used in the Store\Browse.aspx page.

   1:      public class StoreBrowseViewModel
   2:      {
   3:          public Category Category { get; set; }
   4:          public IEnumerable<Product> Products { get; set; }
   5:      }
   7:          <% foreach (MvcDB4O.Models.Product product in Model.Products)
   8:             { %>         
   9:          <li>
  10:              <%: Html.ActionLink(product.Name, "Details", "Store", new { id = product.ProductId }, null)%>
  11:          </li>     
  12:          <% } %>

I hope this has served to show how to connect to an object database in particular DB4O using MVC.

Please ask any questions you may have and I welcome your comments.

Tuesday, October 19, 2010

How to download, install, configure and upload content to GitHub


This post describes how to download, install, configure and upload content to GitHub.

What is GitHub?

Git is a distributed version control system. GitHub provides a way for sharing content in Git with other people

How do I get GitHub?

Sign up for a free account here:

Once you have your account, create a new repository:

After setting up your account you will need to Download and Install Git on your PC.

msysgit is the Windows version, available here:

At the time of writing the latest version is:

Accept the defaults when installing

So how do I set it up?

Once Git is installed, run the application Git > Git GUI > Create New Repository > [enter a name] > Create

This will create a new folder in your Documents and Settings folder with the name of your repository

Copy or create your new project in the folder.

In Explorer, right click on the new top level folder > Git add all files now

Run the command prompt: Git > Git Bash

git config --global "Your Name"
git config --global "Your email address"

Next set up a SSH public key on your PC that links you to the repository:

Change to the location of the new repository:
cd MvcDB4O

git init
ssh-keygen –t rsc –C "Your email address"

You will be prompted for a location for the key, hit enter for the default
Enter a passphrase

You will be told the location of the ssh public key

Create an SSH public key from the website: > add another public key

Give it a title and the key you created in Git Bash

So how do I push my files to GitHub?

In Git Bash
Change to the directory of the repository

git remote add origin

git push origin master (you will be asked for your passphrase)

(ensure the .ssh folder exists in the repository you are uploading)

Switch to GitHub to view the uploaded files


Hopefully you have found this information useful - good luck with your Git friends.

Friday, July 30, 2010



I hold the post of Project Technical Lead at Wiggle Limited.

I will be posting my thoughts, ideas and comments on technical and managerial solutions I come across on a day to day basis.

Please feel free to make any comments to my posts and I will get back to you as soon as possible.

Check out my linkedin and twitter accounts or contact me via email.