admin

Jul 072017
 

You may ask what is the purpose of this post. The title seems really confusing indicating that i may have used the same word twice and trying to explain the difference between it. The thing is these are two different things. One if the native class string and the other is the class object provided by the framework called String. Basically there is no difference in them as  far as the functionality is concerned but when it comes to coding convention then if you want to declare any string value you should use the class ‘string’ to declare it but if you want to use any functionality from the string class the you should use the class ‘String’ which is also the class the object the static class through which you class access method like concat or Parse.

 Posted by at 10:44 pm
Jul 062017
 

In this post i am going to explain two different ways to parse XML file. I am directly going to start with the code but before that here is my sample XML. In the code there are two ways of parsing the XML one is the simple way of doing it through which it is done using the XMLReader way and the other is to deserialize it to a class. Both are explained below

 

The code to parse it is

In the above code the first method called from main is ParseXML. In this method we are using the simplest way to parse the XML by using the XMLReader. So first we need to create the XMLReader by providing the content of the XML and once we have that then its really easy to go though the XML. The first method used is ‘ReadToFollowing’ this method will take you to the first requested node and each call will traverse to the next occurrence. It returns a bool type based on the result of the search. MoveToFirstAttrubute read the first attribute of the node.

The other way to parse the XML file is to serialize it in object. But you have to have an class object in which you can map the XML file and for complex object its a bit hard to make it. Luckily VS provides a really easy way to do it. All you have to do is select the XML and do the following process

xmltoclass

You will get the following file

In method serializeXML i am using the deserializer to deserialize the XML to catalog object and then i just loop around the list to display

 

 

 

 

 Posted by at 2:45 pm
Jul 052017
 

I was always getting confused in what to use until i came to know when should we use IEuqatable and IComparable. So lets say you have an object and you want to use it in a dictionary. Sometime we implement the ‘contains’ method from the dictionary on the list. Now this method wont work if the object is not implementing IEquatable. When you implement that method than the purpose of this method is to compare the object with another object. So indirectly we helped our ‘contains’ method which will use this to compare the object.

 

Now lets say there is another requirement in which you want to sort the dictionary then then object used in that dictionary should implement IComparable. By using this interface you will have to implement the compareTo method where you will mention the position of the two objects under observation.

 Posted by at 11:30 am
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

1

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’

2

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’.

3

In our directory there will be two files created

4

Step3:

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

5

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

6

Step 4:

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

7

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.

9

 

8

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

Snippet

 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.

Options:

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

Analysis

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.

Solution:

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 082016
 

Cross origin resource sharing means that when we try to access a from one origin from a different origin. To get more understanding we need to know what origin is or what exactly is considered while cors calls. The origin is determined based on the URL.If we have URL A (http://localhost:45376) and URL B (http://localhost:45376) and if URL A wants to access a api method on URL B than it wont be allowed under CORS restriction. now be advised that if you are running it on chrome you may get an Origin exception indicating that you are not allowed but if you are running on IE than the port is not considered so it will be allowed and you will be able to access the API method without CORS restriction.

 Posted by at 10:16 pm