Jul 042017

I realy want to get my hands dirty with the new .Net core hype so i thought may be not its the time. Before going crazy on the visual studio i decided to use the .net core using the command line. So lets start

To begin with you need to have .net installed. If you have VS 2017 then .Net core is already part of it but if you dont have it you can download it from here 

To verify if you have successfully installed .net core open command prompt and run the command dotnet you will get this


I will do it in certain steps so that it is more clear

Step 1:

Lets create a directory where we work in so i will create a new directory called ‘dotnetHelloWorld’


Step 2:

Lets create our first project. Now we can create differnt types of project in c#, F# and VB but lets start by creating a C# console project and for that we need to run the command ‘dotnet new console’.


In our directory there will be two files created



Now lets build the project so for that we need to run the command ‘dotnet build’ and when i did that i got some errors. if you see the error list it complains about the types not defined and it cannot find the library for those system types


To fix that we need to restore before build which will restore all the required nuget packages. So run ‘dotnet restore’


Step 4:

Lets give another try to build the project so run ‘dotnet build’ and you will get this


After building now we will try to run it and if you notice the file structure there is no exe file generated in the bin folder that you can double click and run its only the pdb and the dll as .net core will generate the libraries so how to run it? Well dotnet core have solved this problem by giving a command so if you run ‘dotnet run’ you will see the output.




The reason to use it via the command is that it could be used on any platform and could be really handy on build machines.



 Posted by at 4:40 pm
Jul 032017

A quick reference on how you can use tasks. I wont write much around here as the code is explaining it self. The name of the methods tell the action around the task, how they were created and what is the purpose of that code. This is really basic code snippet right now but i will write more around that and more detailed code


 Posted by at 12:59 pm
Apr 282017

So I had a problem where I need to use the sqlBulk to insert multiple records from a data table to database. Now we are using NHibernate but sometime we don’t always use it or not truly use the NHibernate entities instead sometime we use Sqls directly either in new ado connection or using the NHibernate session.

Problem: So we had a module which was using a mixture of both some read data was done using the NHibernate session and some bulk inserts were done in a new ado connection obviously that was a bug so I thought of fixing it to use NHibernate throughout.


Use the ado connection do everything in one transaction and commit.

Use NHibernate session and do everything in that so that if this module is called from another place it could use the same session


Now as you can see from option 2 you can easily spot that the if another module is calling this module using its own session (say NHibernate) the ado wont respect that and it will open a new connection. If the caller module is not using NHibernate than it should inject the connection down the line.


I am not saying that picked up the best solution but I decided to use NHibernate session and sqlBulk using connection form NHibernate connection. So, all the things went smooth until I hit two walls

  1. A piece of code using an ado connection and a transaction
  2. Sql bulk copy

So for the first one I need a connection from NHibernate Session and a transaction. To get ado connection from NHibernate session this helped me


And it’s perfectly ok if you don’t want any transient error handling but if you do want that then this won’t give you any as by using the connection this way you sacrificed the transient handling. But I fixed that using this code

Off course you have to define SqlAzureTransientErrorDetectionStrategyWithTimeout your self.

The other sub problem was to get a transaction now you can’t have a beginTransaction on the connection. As you can’t create parallel transaction using the NHibernate connection. So what you have to do is enlist your command in ITransaction from the session so this should work like this

I used the same strategy in the bulk which was like this before my fix

And after my fix it looked like this

 Posted by at 4:38 pm
Mar 032017

The override modifier is used to extend the functionality of an abstract or virtual method. To easy understand this consider the following example.

In this example the base class is abstract along with two methods one is abstract and the other is virtual. When we use inherit from this base class we are bound to override the ‘Act’ method which is tagged as abstract. But we really don’t have to modify the functionality of the virtual method. But if we want to modify the functionality of the virtual method we just either implement it or use a ‘new’ keyword to implement the new functionality.


If you use conventional inheritance and still want to override to modify the behavior of existing base class method you can do so by using the New keyword consider the following example.

 Posted by at 2:25 pm
Dec 022016

There are certain situations where you only want to use generics types for certain types. Mean you want to limit it to be used for certain types only. Consider the following example where we have a two class CurrentEmployee and ExEmployee and we want to limit our SuperPerson to use type that are only CurrentEmployee because in my company only those person can be super persons who are currently working with us :). We use the key word where to write the name of the class. Now this could be a name of the class, an interface implemented certain classes, a base class or a struct.


 Posted by at 11:49 am
Jul 112016
A few days back i got the idea of figuring our how much time was i spending on different application while working. So i made this small console application its not perfect but it can give you a head start. So i am using the following three classes there is no complex code but still i will try to explain it.
In this test app i am only five applications so the moment it collects the data for five applications it will exit but you can always extend it.


This is the main thread that is monitoring the application and checking if thee application is started and if it is it will log it and start a time against it.


This helper class is just collecting data for different application and adding the total time being used.


 Posted by at 9:13 am
Jul 022016

In the first post we have created a DB using c#. Now this time we will drop a DB I have divided the code in to two classes so lets see them in detail.

The first class is the control class through which we will call the helpers. In the code below first we will try to connect to the master DB. In dropDatabse method we need to check if the DB exists or not and if it does then we will need to set the DB online. The reason to set the DB online is when you drop the DB then the file on the disk will also be deleted.


The helper class ExecuteSql have all the helper functions. The class is described below.

All you need to do is change the name of the DB, user id and password.

 Posted by at 7:03 pm
Jun 152016
So i am back again to explain some of the flexilibilities offered by LINQ.
The seires is not really related other than the LINQ part but if you are still curious here is the link to part one.
So today i am going to talk on fluent apis. Now the terminology actually came from ruby rails where its easy to read the code and you dont have to add /read comments.
If you check the code here in the method ‘Speak’ we are create two variables and passing them to a ‘talk’ function. There is no way of telling here what the code is doing unless we read it closely. But we can re write the following class to
this ‘FluentAPI’. Now if you see the ‘Speak’ function you can easily see that we have written code to call the talk method from language ‘english’ to ‘swedish’. So if you want to write code like this “from().to()” then the method should return the same object type.
 Posted by at 3:30 pm
Jun 082016

So you can use look Linq for validations as well. In our example i am using an Email object and i am doing two types of basic validations on that so here is the code

So the conventional validations would be

Lets transform it to functional validation. If you see here i have converted the list of ifs to set of rules. By the end i am using the method All which will start varifying the rules and the moment it encountered with a rule that is voilated it will stop and exit.



 Posted by at 4:32 pm
Jun 032016

So this is first part of multiple blog series in which i will discuss the power of LINQ

To begin with i will discuss Extensions method and how we can use them and why we need them. But before we start have a look at the following code

Now in the above code if you read it you can say that this class with create a scheduled task which will run after every ‘duration’ minutes and its going to run for the next ‘run’ hour. We may get some idea from here though its bad naming convention but when we will use it we will have no clue of what we did when we see the code again. so when i call this i will write this

It will be really hard for the suppor engineer to read this. So to make it more readiable we use Extensions Methods. You can only use Extension methods with premitive type so after using extension method this class will look like this

Now dont use extensions where they are not needed. Every extension method should reside in the same namespace as the class where it is being used.

 Posted by at 12:36 pm