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.

Thursday 31 December 2015

Paged result with EntityFramework ObjectContext

Paged results are possible using Entity Framework. Sometimes it is desirable to create a utility method to support paged results in a simple way as an extension method. Let's look at how to achieve this. First we download the AdventureWorks 2008 R2 Sales database from: Download AdventureWorks 2008 R2 Sales After downloading the MDF file, restoring it should be easy. Open up SQL Management Studio and create a new query window. Next up, insert the following SQL:
CREATE DATABASE AdventureWorks2008R2  ON (FILENAME = '{drive}:{Folder}') 
FOR ATTACH_REBUILD_LOG
Note that {Drive} here can be c:\ and {Folder} here can be:
 
c:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA
We should have now our data and we can create a new Console Project in Visual Studio and choose adding an ADO Entity Data Object Model to our project, located under Data. Choose to generate from database and choose the Production.Product table in AdventureWorks2008 database. We can then add an EntityFramework 6.x EntityObject Generator. If you cannot locate this kind of item, download the Extension for Visual Studio from here: EF 6.x EntityObject Generator for C# This is necessary as Microsoft has defaulted to DbContext in EntityFramework 6. To support ObjectContext, we choose to use this extension. Finally we can code our Paged result extension method! Let's define the extension method next:

   public static List<TEntity> PagedResult<TEntity, TKey>(
            this ObjectContext ctx,
            Func<TEntity, TKey> sortingfunc,
            int pageIndex,
            int pageSize = 20)
            where TEntity : EntityObject
        {
            var result = ctx.CreateObjectSet<TEntity>().OrderBy(sortingfunc).Skip(Math.Max(pageIndex - 1, 0) * pageSize).Take(pageSize);
            return result.ToList();
        }

Note that our method did not require that much code. We allow to pass in our ordering member or column of our entity and we choose a default page size of 20 which can be adjusted. We specify the entity type (table) and we specify the page index. You can choose a page index of one to this method and that means the first page. This in fact is technically page index zero and we use the Max method here to protect inputting negative indexes. Let's test this extension method out!

 static void Main(string[] args)
        {

            using (var ctx = new AdventureWorks2008R2Entities())
            {
                var prods = ctx.PagedResult<Product, string>(x => x.Name, 2);


                foreach (var p in prods)
                    Console.WriteLine(p.Name);
            }

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

And we get the desired result:
This may be readily inspected using the AdventureWorks2008 Sales database and select the content from the Production.Product table. So there we are, a reusable method to choose paged contents from a database using Entity Framework and ObjectContext. I prefer ObjectContext instead of DbContext because it provides more lowlevel functionality compared to DbContext. (Although DbContext is more convenient to use).