NServiceBus Workshop – Invites Sent and then Sold Out!

NSB

Well a few short days after sending out the invitations for the private limited seating NServiceBus workshop to be held on 10 May at the Kaleidocode offices we must report it’s sold out already. Registrations came in thick and fast, and over the Easter weekend all the seats were taken. For those who did not get a chance to get a ticket, we really apologise – we can only accomodate 9 attendees. Our best suggestion is to go along to the Eventbrite event page and get yourself added to the wait list – possibly there may be a cancellation.

Eventbrite - Workshop - Scalable Distributed Design
To give you an idea about the format of the workshop (our first), take a look at the agenda/promo below:

Introducing an Instruct private workshop to deep dive into the recent content presented by Chris Tite, entrepreneur, architect and speaker on Scalable Distributed Design.

A key element of distributed system design was presented as Service Orientated Architecture (SOA). Chris discussed patterns and demonstrated a basic NServiceBus implementation.

In this workshop Chris will guide as you personally get hands on with NServiceBus and work through some samples and objectives.

During the workshop you will get exposed to:

  1. Setting up the environment
  2. Working through the demo examples used in the presentation
  3. Writing your own SOA empowered application with NServiceBus

For more information about Chris please see christite.net. For more information or for additional invitations please contact Rory Clarke on 031 303 2299, or rory.clarke@kaleidocode.co.za. To catch up with past Instruct events go to https://www.youtube.com/user/instructchannel/videos.

Advertisements

NoSQL Explained Instruct Event Video

The complete video recording of the recent NoSQL Explained lecture presented by Chris Tite has been posted to the Instruct YouTube channel. You can find more instruction videos on the channel.

Recorded on 4 February 2014 at the Benjamin Hotel in Durban. Hosted by Chris Tite, entrepreneur, software architect and speaker (http://www.christite.net).

With the onset of web and mobile applications that require high availability and scalability, web developers have started turning away from using relational databases because of scaling limitations. NoSQL technology is gaining momentum and is supported as either commercial or open source solutions.

In this session Chris introduces NoSQL and contrasts it with SQL technology, giving insights into both technologies to help you select the correct tool for the job at hand. Topics covered are:

  • NoSQL compared with SQL – Pros and Cons
  • BASE vs ACID
  • Brewers CAP theorem and how it affects your choice of DB
  • SQL Architectural patterns
  • NoSQL Architectural patterns
  • Hands-on with Mongo DB:
    • an introduction in to Mongo DB,
    • setting up the environment,
    • writing Mongo queries
    • writing a Mongo DB application in .NET

Question and Answer session from the NoSQL Explained Instruct event:

About the author and presenter, Chris Tite:

Chris is a software architect with 20 years experience. He is passionate about quality software and great architecture, while forging the growth of this generation of software developers. Chris holds a B.Sc. Electronic Engineering degree with over 18 years of development experience spanning several platforms and industries. He sports extensive experience with Microsoft tools like ASP.Net, C#.Net, VB.Net and SQL, as well as with ETL and Data Warehousing tools.

Chris is a true software professional, well versed with open source tools and languages, including such staples as PHP, Perl, Postgres, and MySQL hosted on Linux. He has a passion for software architecture and processes. Since 2005, Chris has been enthused by Agile development methods, seeing first hand the delight of clients experiencing progressive and effective delivery of a shared vision.

Session Resources – NoSQL Explained – Demo 3: Connecting to MongoDB using .Net

Using .Net to insert a document into MongoDB

To run this demo please ensure that your MongoDB instance is running before starting. If you are unsure how to get an instance of MongoDB running please view the Session Resources – NoSQL Explained – Running an MongoDB instance page for details on how to get an instance of MongoDB running on your machine.

This demo was prepared using the 10gen MongoDB driver, ensure that your project references MongoDB.Bson and MongoDB.Driver.

Use NuGet to install the correct MongoDB supporting dlls.

I am going to start off the Demo code by defining an Employee class that I will use as my document structure in MongoDB. Recall that MongoDB is a document database so all data entities are referred to as documents which are found in collections. A document in MongoDB often has nested documents thus creating a fairly complex document structure. In this demo we are not going to create any complex structure we are going to work with a very simple flat document which will define an Employee in our system.


    public class Employee
    {
        public ObjectId Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public DateTime DateAdded { get; set; }
    }

Adding your first document using .Net

Now that we have a class that defines our document we can look at the code to insert the document into the MongoDB. Below I have added a few lines of code which will connect to an instance of MongoDB and insert the Employee document into the Employees collection.


	private string AddEmployee(string name, string surname, DateTime dateAdded)
	{
		//using the client connect to the server
		string connectionString = "mongodb://localhost";
		MongoClient mongoClient = new MongoClient(connectionString);

		//using a server connection get a hook to the database
		MongoServer server = mongoClient.GetServer();
		MongoDatabase database = server.GetDatabase("Demo3");

		//Create an employee object
		Employee employee = new Employee {Name = name, Surname = surname,
                   DateAdded = dateAdded};

		//Get a reference to the Employees collection
		MongoCollection employees = database.GetCollection("Employees");

		//Add the document to the collection
		employees.Insert(employee);

		//return the MongoDB unique identifier
		return employee.Id.ToString();
	}

So what is actually happening in the code above?

The code breaks down as follows.

The first four lines of code establish a connection to an instance of MongoDB using the database Demo3. Next we create an new Employee document setting the attributes to the correct values (name, surname and date added). We then get a reference to the Employees collection and finally insert the Employee document into the Employees collection.
We now have an employee document in the Employees collection of the Demo3 database.

Finding the Inserted document

Now that we have an Employee document in our database we would need to find it to be able to provide any form of useful business application. Finding a document is equally as simple as inserting one, the code below shows just how simple it is.

</pre>
     private string FindEmployeeByName(string employeeName)
     {
         //using the client connect to the server
         string connectionString = "mongodb://localhost";
         MongoClient mongoClient = new MongoClient(connectionString);

         //using a server connection get a hook to the database
         MongoServer server = mongoClient.GetServer();
         MongoDatabase database = server.GetDatabase("Demo3");

         //Create a query, find an employee whose name is equal
         //to that supplied in the variable employeeName
         var query = Query.EQ(emp => emp.Name, employeeName);

         //Get a reference to the Employees collection
         MongoCollection employees = database.GetCollection("Employees");

         //Apply query, using the findOne command
         Employee employee = employees.FindOne(query);

         //Return the result
         if (employee != null)
         {
             //Return a concatenated version of the record
             return employee.Name + " " + employee.Surname + " - Added on "
                  + employee.DateAdded;
         }

         return "Not Found!";

     }

The code breaks down as follows.

The first four lines of code establish a connection to an instance of MongoDB using the database Demo3, much like we did when we were inserting a document. For purposes of demonstration I have included these four lines again, when writing actual production code this connection logic would be managed in a separate connection manager class to encourage code reuse.

Next we create a query, telling MongoDB that we wish to find an Employee document where the name is equal (EQ) to the name supplied. We then get a reference to the Employees collection and run the query against the Employees collection, using a findOne command.

Note that we could have used the find command to return a collection of documents which met our search criteria.

We should now have an Employee document, we check if we have a returned document, if so return the required information, in this case a “flattened” version of the record.

Session Resources – NoSQL Explained – Demo 2: MongoDB Performance Monitoring and Indexes

This blog post covers demo 2 which was done as part of the NoSQL Explained inStruct session.

In this demo we cover MongoDB indexes and Performance Monitoring.

An excellent reference for MongoDB is Kyle Banker’s book MongoDB in Action from Manning Publications

To run this demo please ensure that your MongoDB instance is running before starting.
If you are unsure how to get an instance of MongoDB running please view the Session Resources – NoSQL Explained – Running an MongoDB instance page for details on how to get an instance of MongoDB running on your machine.

We are going to run this demo from the MongoDB command prompt, run the following command in the windows command prompt to start the MongoDB command prompt.

c:\data_mongo\bin\mongo

Once the MongoDB command prompt is up and running we are able to start the demo.
Run through each of the commands step-by-step to see just how simple it is to create an index as well as perform profiling on a MongoDB.


//Set context to a new database called Demo2.

use Demo2

//For the purpose of the demo add a large number of documents to a
//collection called numbers.
//Notice that this can be performed by using the simple javascript
//for loop

for(i=0; i<200000; i++) {
db.numbers.save({number: i, numberDescription : "Number " + i});
}

//To check that we have inserted 200 000 documents perform a count
//on the numbers collection

db.numbers.count()

//Now find the last four records in the document set

db.numbers.find( {number: {"$gt": 199995 }} )

//To understand what MongoDB is doing under the hood
//use the explain command for more detail

db.numbers.find( {number: {"$gt": 199995 }} ).explain()

MongoDB has built-in performance monitoring tools, it allows you to turn on database wide profiling with the flick of a switch.

Using the command db.setProfiling(2) you can turn profiling on for all queries run against MongoDB.

Using the command db.setProfiling(1, 100) you can turn on profiling for all queries which take longer than 100 ms to execute. Note in this case 1 is the profiling level and the 100 indicates that queries executing longer than 100ms should be logged.


use Demo2

//Set database wide profiling on

db.setProfilingLevel(2)

//Perform another find for the last four documents

db.numbers.find( {number: {"$gt": 199995 }} )

//Now view the last 5 entries in the profile collection (system.profile)

db.system.profile.find().sort({$natural: -1}).limit(5)

Creating an index in MongoDB is done using the ensureIndex command on the collection in question.


//Using the ensureIndex command create an index on the numbers
//collection.
//The index is created on the number parameter

db.numbers.ensureIndex({number: 1})

//Note compound indexes can be created by specifying more than one
//column in the ensureIndex command

db.numbers.ensureIndex({number: 1, numberDescription: 1})

//To find out which indexes exist on a collection use the getIndexes
//command

db.numbers.getIndexes()

//Running an explain on the same query as above will show
//that we are using a more optimized method of searching for our records

db.numbers.find({number: {"$gt": 199995 }}).explain()

Now check out demo three to see how to connect to Mongo DB from .Net.

Session Resources – NoSQL Explained – Demo 1: MongoDB Database create, data insert and data find

To conduct the first demo in the Instruct session on NoSQL you can follow along with the code below.
This MongoDB demo creates a database, inserts data and finds it.
All these commands are to be typed in sequentially into the command prompt. Please ensure that your MongoDB instance is running before starting.
If you are unsure how to get an instance of MongoDB running please view the Session Resources – NoSQL Explained – Running an MongoDB instance page for details on how to get an instance of MongoDB running on your machine.

We are going to run this demo from the MongoDB command prompt, run the following command.

c:\data_mongo\bin\mongo

This code will start an instance of the MongoDB command prompt which will connect to an instance of MongoDB server running on localhost and listening on the default port 27017.

To connect to an instance of MongoDB running on another server include the machine name and port as command parameters like this.

c:\data_mongo\bin\mongo mumbai:40001

Now for a few quick demo items, type the following commands in sequence to create a database, insert a record and find it.


//Set context to a new database called Demo1.
//Note the database hasn't been created yet we have just told MongoDB
//that we wish to use the database called Demo1

use Demo1

//Create a users collection and insert a document entity into the
//database.
//Note MongoDB will now create a database called Demo1 and include the
//users document collection.
//A new document will be created in the users collection with the
//following parameters:
//username, password, salt, isLockedOut, failedPasswordAttempt

db.users.insert({ username : "Chris.Tite",
password : "FslPhcKizgtFNt42HM+CpLSI3wk=",
salt : "V7BcIDv6T0M=", isLockedOut : false, failedPasswordAttempt : 0 })

//To check that the document was created run a find command on the
//users collection.
//Note that the find command will list all documents created in the
//users collection

db.users.find()

//inserts a second entity into the database
//Notice that the structure for this insert has an extra parameter
//called "pin".
//MongoDB has no problem inserting this slightly different document
//structure.

db.users.insert({ username : "Sally.Burnett",
 password : "R29evUC0dGwkLzCd5rT91b8n+xU=",
 pin : "a+ZRoz4kD+4jRx8MG8QcMQv2hIA=", salt : "VlMIEUalxZU==",
 isLockedOut : false, failedPasswordAttempt : 0 })

//View both new documents by doing another find

db.users.find()

//To find an entry in the users collection simply add the parameter
//you wish to search by.
//Note the json structure

db.users.find({ username : "Chris.Tite" })

//To find a single entry use the findOne command

db.users.findOne({ isLockedOut : false })

//To update a document use the update command.
//In the case below we are updating all entities in the users
//collection setting the isActive attribute to true
//Notice the empty braces {} as the first parameter in the update
//statement)
//The second parameter is the value of the new or existing parameter
//The third parameter (optional) tells Mongo if it needs to upsert
//i.e. create a new document when no documenst meet the criteria
//The forth parameter (optional) tells Mongo that it should update
//multiple documents

db.users.update({}, {$set : {isActive : true}}, false, true)

//Here is another example of an update statement this time using a
//save command.
//This command updates all entities in the users collection setting
//the isApproved attribute to true

db.users.save({}, {$set : {isApproved : true}}, false, true)

//If you are interested to know what the save command does simply run
//the command with out the brackets
//You will notice the command prompt shows the javascript for the save
//command, it uses an update
// or insert depending on whether or not it finds a record.
//It is always handy to remember that running a command without the
//brackets shows the actual javascript for the command.

db.users.save

//To remove a document use the remove command.
//The code below removes the entity in the users collection with the
//username attribute matching "Chris.Tite"

db.users.remove({username : "Chris.Tite"})

Congratulations you have successfully created a new database which contains a users collection.
You have also successfully inserted two users into the users collection, done a few finds, updated the user documents and removed a user document from the users collection.

Proceed to demo two to walk through working with Mongo indexes.

Session Resources – NoSQL Explained – Running an MongoDB instance

Before working on any of the demos which were prepared for the NoSQL Explained session it is important to have in instance of MongoDB running.

If you are familiar with MongoDB and/or already have an instance running on your machine then skip this post and head straight to the demo posts if not stay close and we’ll cover setting up an instance of MongoDB in a few easy steps.

Getting MongoDB running on your machine

Step 1: Download a version of MongoDB form the MongoDB website http://www.mongodb.org/downloads.

Note: Version 2.0.9 was used for the demos, but since the demos cover very rudimentary functionality a newer version of MongoDB can be downloaded.

Step 2: Once the MongoDB has been downloaded, unzip the zip file to a working folder and find the bin folder, this folder contains all the exe files required to run MongoDB.

Step 3: On your C drive create a folder called “data_mongo” and copy the bin folder from step 2 into this folder.

Step 4: Run the following command to start MongoDB.

c:\data_mongo\bin\mongod --dbpath c:/data_mongo/db

Looking at the command above the following can be noted:

  • The executable mongod.exe is the server executable
  • The parameter –dbpath defines the path where the databases files reside. Note if the dbpath parameter is not supplied MongoBD will default to the path “c:\data\db”
  • The parameter –port can also be included if you with to have MongoDB run off a different port. By default MongoDB will run off port 27017 if no port parameter has been inlcuded

You now have an instance of MongoDB running on your machine. The instance is listening on port 27017 and will write data to the path “c:\data_mongo\db”.

Proceed to the first demo to start creating a database and inserting data.