Wednesday, 4 May 2016

Creating a simple MD5 application using GTK# and Monodevelop

Let's look at building a simple application using GTK# and Monodevelop! I created this application using a Ubuntu 16.04 Xenial AMD64 Distribution of Linux running inside an Oracle VM VirtualBox on my Windows 10 Machine! First off, this article will show a very simple application written in Monodevelop IDE using GTK# to build a GUI. It resembles somewhat Windows Forms if you come from a Visual Studio background, such as I do. The application just takes some text input (plaintext) and computes a MD5 hash. Simple stuff. Defining the following form in MainWindow of the GTK# application: Moving over to the code bit, I define the following in MainWindow (Source of the form), which is the code behind:

using System;
using System.Linq;
using Gtk;
using System.Security.Cryptography;
using System.Text;


public partial class MainWindow: Gtk.Window
{
 public MainWindow () : base (Gtk.WindowType.Toplevel)
 {
  Build ();
  btnMd5.Clicked += OnBtnClick;
 }

 protected void OnDeleteEvent (object sender, DeleteEventArgs a)
 {
  Application.Quit ();
  a.RetVal = true;
 }

 protected void OnBtnClick (object sender, EventArgs args)
 {
  var md5 = MD5CryptoServiceProvider.Create ();
  byte[] plainTextBytes = Encoding.UTF8.GetBytes (tbPlainText.Buffer.Text); 
  byte[] hashBytes = md5.ComputeHash (plainTextBytes); 
  var sbuilder = new StringBuilder (); 

  sbuilder.Append(string.Join("",
  hashBytes.Select(x => x.ToString("x2")))); 
  tbHash.Buffer.Text = sbuilder.ToString();
 }

}



The code above instantiates a MD5CryptoServiceProvder instance, then computes a hash. We get a string representation of the MD5 hash using a StringBuilder and we use ToString("x2") - which assembles a hexidecimal string for us, which is the common way to represent a MD5 hash. A MD5 hash produces 128 bits = 16 bytes = 32 hexidecimal digits. A hexadecimal value can be 0-9 and A-F = 16 different values = one half byte.

We build up our GUI using the GUI designer inside Monodevelop. The GUI designer for GTK# in Monodevelop is called Stetic.
Stetic

Friday, 22 April 2016

Transfering a commit message from Git to Target Process

It is possible to transfer a commit message from Git to Target Process using the REST Api of TP. This is done using hooks in Git. The problem is that the hook is written using bash shell scripts (or perl) and unlike Mercurial - Git does not sport an obvious choice of tool to develop such hooks. I am going to present below a hook I wrote in bash shell to transmit your commit message to TP. I choose to do this in the post-commit hook. First off, go to the source repository you are working with and into the .git subfolder. Then go into the hooks folder. Now add a new file called: post-commit Ok, so now we add our hook:

#!/bin/sh
#
# Overfører kommentar fra Git til Target Process
# Bruk - Formater kommentaren som: 
# TP TASKID: Min innsjekkingskommentar her
#
# Dette vil overføre så kommentarer til TP 
# Merk: 
# Erstatt verdiene i scriptet som heter TP_LOGON og TP_PASSWORD
# med ditt pålogging til TP. Vil du ikke inkludere passordet ditt til TP kan du 
# ta bort passordet som argument til curl kommandoen 
#
# Merk at du må installere Cygwin først og installere curl og curl-lib. Nano anbefales som editor
# Cygwin - https://www.cygwin.com 
 
 
NAME=$(git branch | grep '*' | sed 's/* //') 
DESCRIPTION=$(git config branch."$NAME".description)
 
regex='TP ([0-9]+):*'
 
melding=$(git log -1 --pretty=format:%s)
 
echo "Viser melding her: "
echo $melding
 
tpnum=0
 
if [[ $melding =~ $regex ]]
then
 tpnum="${BASH_REMATCH[1]}"
        echo "TP number: $tpnum"
        curl -H "Content-Type: application/json" -X POST --data '{"Description":"'"$melding"'", "General": { "Id": "'"$tpnum"'"}}' https://someacme.tpondemand.com/api/v1/Comments?resultFormat=json -u TP_LOGON:TP_PASSWORD
 else 
  echo "Pusher ikke melding ut til Target Process. Bruk: Skriv TP TASKID: melding"
fi

Note that the curl command needs to be a one liner. To use this hook, just do some changes in your code and commit! git commit -m "TP 123: This is a checkin comment for the task with task Id 123 and will be shown in TP via a Git hook!" If you are working on a Windows system, you can download Cygwin (64-bits tested) and the libs curl and curl-lib. I like the Nano editor very much. Happy coding in Git and sharing your check in comments on TP! Pretty nifty to share progress with others!

Tuesday, 26 January 2016

Paged IQueryable ObjectContext EntityFramework

The following article displays how we can achieve retrieving data from EntityFramework using paged results with ObjectContext and sticking inside IQueryable<T> Let's review the extension mehod first:

 public static class EntityExtensions
    {

        public static IQueryable<TEntity> PagedResult<TEntity, TKey>(
            this IQueryable<TEntity> query, 
            Func<TEntity, TKey> sortingFunc, 
            int pageIndex = 1,
            int pageSize = 20)
        {
            var pagedResult = query.OrderBy(sortingFunc)
                .Skip(Math.Max(pageIndex - 0, 0) * pageSize)
                .Take(pageSize);
            return pagedResult.AsQueryable(); 
        }

}

And using AdventureWorks2008R2 database, here is some sample query that shows how we can use this query extension in Linq to Entities:

   using (var ctx = new AdventureWorks2008R2Entities())
   {
                var mountainStuff = from product in ctx.Products
                               where product.Name.Contains("Mountain")
                               select product;
                var firstMountainStuffPage = mountainStuff.PagedResult(p => p.Name, 1, 20);

                foreach (var item in firstMountainStuffPage)
                    Console.WriteLine(item.Name);
   }

            Console.WriteLine("Press any key to continue ..");
            Console.ReadKey();

Output

LL Mountain Frame - Black, 42
LL Mountain Frame - Black, 44
LL Mountain Frame - Black, 48
LL Mountain Frame - Black, 52
LL Mountain Frame - Silver, 40
LL Mountain Frame - Silver, 42
LL Mountain Frame - Silver, 44
LL Mountain Frame - Silver, 48
LL Mountain Frame - Silver, 52
LL Mountain Front Wheel
LL Mountain Handlebars
LL Mountain Pedal
LL Mountain Rear Wheel
LL Mountain Rim
LL Mountain Seat Assembly
LL Mountain Seat/Saddle
LL Mountain Tire
ML Mountain Frame - Black, 38
ML Mountain Frame - Black, 40
ML Mountain Frame - Black, 44
Press any key to continue ..

Conclusion

So there we are, we now have a query that we can reuse to get our paged result and we can pass in our sorting key. So now we can retrieve paged data from for example queries returning large result sets and only display a single page at a time, supporting quicker fetches from the server for clients, retrieving less data and support mobile clients better by getting data pagewise.