admin

Oct 142018
 

If you have created a table in Microsoft SQL and along with a table you have a column with a default value, then the name of the constraint will be generate by the SQL framework unless given. This will be will have some random GUID which will keep it hard to know when you want to drop a column. There could be certain sitations where you want to directly delete the column and there are situations where the column should be dropped without having the knowldge of constaint name.

You can acheive that by using the following sql

 

 

 

 

 Posted by at 10:49 pm
Oct 022018
 
featured image

If you are not familiar with the blockchain key concepts i would suggest this previous article.

Blockchain is a technology that enables us to move resources on the provided decentralize database. Now those resources could be anything that provides us value. Its is transparent among the parties or nodes that are involved which makes it more trusted system.

Concept of Blockchain:

The concept of blockchain was introduced in 1991 by a group of scientists who proposed  to timestamp documents digitally in order to validate them. Once a document was timestamp a hash of the complete document was calculated. This hash was used to validate the document. Any tiny change in the document will change the hash and it will invalidate the document. But unfortunately it was not practiced that much until 2009 by Satoshe Nakamoto.  He created the crypto currency Bitcoin using blockchain technology.  

concept of bc

Now that a very important point here about a very general and common misconception  between Bitcoin and blockchain that they’re one of the same things but they’re not.  Bitcoin is a digital currency where as blockchain is a technology used by the bitcoin. 

 

Case study:

In this case study we’re going to discuss a very basic example of money transfer.  The Scenario is where we  want to transfer money from one point to another. Lets say A is in Sweden and wants to transfer 5 dollars to B in Pakistan. As there is no direct line so A will have to use a third party service. The 5 dollars will be transferred via the third party who will transfer the money to B in  Pakistan. Lets say the third party charge some fee and the transfer is done in 2 days.  Now during those two days you don’t have any transparency on what the third part is doing with your money or is the money transferred or not.

case study 1

 

So we have three general problems here that we want to discuss the fee, the time that the third party took and transparency of the third party process. Now lets address these problem in more detail by consider another example. 

 

Long transaction time:

So we have three person A, B and C.  In the very beginning we say that A has 10 dollars. We are going to maintain this in a record which will be our open ledger.. The very first block of the blockchain is called the genesis block.  B has 7 dollars and C has 5 dollars.  Lets say A wants to transfer 7 dollars to B. The address of A B and C are basically the public keys on which the money is transferred. This will be added as a transaction to the ledger and the transaction is performed. B want to transfer 5 dollars to C which will also be added to the ledger and so will our last transaction which is C transfer 5 dollars to A. Now if you see this open ledger is actually the transfer which is done instantly. This open ledger here is called the blockchain  So we have fixed the problem with the long transaction time by instantly maintaining the record in an open ledger but our solution is still like the third party where our ledger is hosted an one place which could be a single point of failure.

 

Transparency:

To address this problem we will distribute the ledger to each node that is involved in the network.  So now we have a distributed block chain. By doing that we eliminated the problem of trust less ledger in fact now its more visible to all the parties and they can validate the ledger at any time. As we have solved the problem by distributing blockchain. we have now another problem of synchronizing the ledger so that is updated on each node on the network. The problem here is that A can say that C transferred 10 dollars to A and add it to its block chain so how will the other nodes know that it was an invalid transaction. 

To understand this part of the system where we will have to look at another example. Lets say this is our network and this the block chain for that network. Lets say A wants to transfer 2 dollars to C. When the transaction occurs a block in created but not yet validated. The difference between creation and validation of block is that anyone can create a block but it has to be validated before it could enter to all the blockchain.  When the block is created it will distributed to these special nodes in the network that will perform validation. These special nodes are called miner. Miner are just ordinary nodes except that have provided high computation speed to compute the validation of the block. Once they validate a block they are rewarded with a small amount. 

validation

For our block we have to calculate the hash of the block which some could say is a really quick operation so why this high computational power is needed. Usually there are some blockchain rules that are enforced on the block validation that the hash of the block should start with 5 zero. This is called the difficulty of the block. I will discuss these topics in detail in my later videos. Lets get back to our block so once our intended block is published to the nodes all the miners will pick up the block and start computing the hash the block. Let say B was able to generate the hash before D so B will add the key that it used to generate the hash, add it to its local block chain and broadcast it to the network.  When A receives the block it will validate the hash by using the same key in the block and once validated A will add the block to its block chain and wait for the next block. 

 

The reward system for the miners is very complex and very important part of the block because it contributes to your currency. I hope you all understand the concept. We touched these concept which I will explain in more detail in my later lectures. 

 Posted by at 2:15 pm
Sep 282018
 
Blockchain-Funds

As I mentioned more in my previous blogs about Blockchain lets take it apart and discuss it in a bit more detail. Consider this as a single block of BC. As you can see there are multiple dimensions of this block.  

block

A block in Blockchain:

One of the dimension is the data block which will hold the data part of the Blockchain. This could be any type of data it could list of transactions it could be a document that you could sign or it could be any other business related data.  The second part of the block is the hash part which holds the hash of the block data.  the hash of the data along with the timespan is calculated and the resulting hash is stored in the hash part of the block. The third part of the block is the hash of the previous block. By adding that we ensured that block is connected with its peer block and also it knows the authenticity of the previous block so if anyone tempers with the previous block the hash of the previous block will be changed and we will have inconsistent block hashes. 

In the process of forming a Blockchain the first block of the Blockchain is called the Genesis block Genesis block is the very first block in ever block chain. In the structure of the genesis block the  previous block field is empty as there is no former block.  

forming a blockchain

Genesis Block:

The formalization of the blockchain not only require the genesis block but it also need some rules to obey to add a block to the chain. This is called the consensus protocol. The consensus protocol of the Blockchain dictates how the block is processed which means how we are  going to calculate hash of the block. If any block is tempered than the rest of the blocks in the block chain will be invalid. The only way to invalidate a whole chain is to temper with a block and recalculate all the later blocks .While we are talking about genesis block  here is the Genesis block of Bitcoin as you can see this is the first block of Bitcoin there is no previous block with no transactions to calculate. There is also a reward to calculate the hash in this block which is 50 BTC. It’s a very good way of contributing to the currency that is more explained in concept of mining

 btc block

Now that we know about the BC what if someone tempers with the BC so there will be an invalid BC for all the nodes. To deal with this problem the concept of distributed BC is introduced. So each node in the network will have its own copy of the BC and every change in one of the BC will be authenticated and synced with all the BC within the network. The distributed blockchain is still vulnerable with one famous security problem. So if you invalidate more than 50% of the BC nodes than you have successfully performed the control of the network. You can rewrite the consensus protocol. You can manipulate the transaction history.  But to do that action is not that easy it is really performance intense to perform such an attack on more than half of the network at the same time so for now its really hard to achieve that. If you have control of the more than 50% of the network then you have successfully performed a 51% attack. 

51% Attack:

Now let’s discuss the 51% attack in more detail. Consider that you have a network with multiple nodes. Let us divide the nodes in two colors red and blue. The blue nodes are the good nodes and each of the good nodes have their own copy of the blockchain. The red nodes are the bad nodes or the malicious nodes and they also have a good valid copy of the blockchain as well.  The bad node  then publishes a malicious block to the network and synchronously do that will all the local of the bad nodes. This will in return publish the same block to all the nodes on the network. 

 

 Now the good nodes will also get the same malicious block and there is a chance that they will invalidate the block tagging it as malicious block. But if we have more number of bad nodes on the network so the major nodes in the network will accept the malicious block to be part of the valid Blockchain. The good nodes will think there block chain is invalid as they are less in number and they will sync with the bad Blockchain. So now the whole network is infected resulting in a successful 51% attack. 

Proof-of-Work:

The 51% attack looks really simple to conduct as all you have to do is re-calculate the hashes of the Blockchain and then you can invalidate the whole chain with enough CPU power. To address this problem there is something called Proof-of Work. The proof-of-work dictates that you can calculate the hash but there are certain rules that are applied on a hash. The hash should have some particular number of zeros or it should start with at least ‘x’ number of characters or  numbers.  By intruding such rules the process is generating the hash becomes more difficult and requires more CPU-time.  So you try different key combinations until you reach an acceptable block and then you publish that key with the block so that the other nodes in network can validate your work. The rules for proof of work are different in different currencies or domains. For example Bitcoin accepts 1 block in every 10 minutes, Intense coin accepts 1 block after every 30 seconds. There is another part call the difficulty that is included in the proof of work rules. The more the difficulty is the harder it is to conduct a 51% attack.  

Mining:

let’s say if I am one of the node contributing my processing power to calculate hashes what will I get in return for the  processing power  that I m contributing. There are special nodes who contribute in the processing of the blockchain called the mining nodes. The concept is called mining in blockchain where the nodes receives a published block, calculates hash on that block using a random key until it reach the difficulty and once it has successfully calculated the hash then we can say that we have  successfully mined the block and then its publish to other nodes in the network.  The node will get reward in return of mining which is also in the form of the currency which helps building the blockchain and also it contributes to the currency . Mining is  a process of running the blockchain rules which keeps the Network’s stable safe and secure by processing validating and invalidating Blocks. But the problem with that is that it doesn’t have bright future. In many currencies because it takes too much electricity it takes too much power to calculate the hash  so maybe mining is not the future. There are different solutions alternate Solutions available which I will discuss later.

Summary:

So in this blog i have touched the concept of blocks in blockchain, proof of work, mining and 51% Attack.

 Posted by at 11:29 am
Nov 102017
 

 

 

 

 

You can say that the bot framework can be classified in to the following concepts.

keyconcepts

 

Connector: The bot framework is basically a REST full API that is used to communicate between the Bot and the channel. Now here the channel could be anything like skype, slack kik etc. Basically the connector library is used to perform an interaction between the Bot and the user.

Activity: The means to facilitate communication between the bot and the user is called activity. This is used by the connector. Now an Activity is an object which could be a message or any other type.

Dialog: Dialog in bot framework is used to model a conversation and conversation flow. You can have a dialog within a dialog to model a meaning full conversation. The main purpose of having a nested is model like is because of the re usability. The Dialog in the Dialog Context is maintained in dialog stack that are in a conversation.

FormFlow: Sometime the input from a user is more complex than just a message in those cases a form flow is used. For example if you want to take multiple input from the user like customer review or some general information regarding a complex question. The formflow can automatically generate dialog to maintain a guided conversation.

State: As obvious by its name the state is associated with a user. The bot frame maintain the state of the user that is communication with the bot at any given time, If the user leaves you can always reload the state of the user and see where he left and continue the conversation from there.

 Posted by at 4:29 am
Nov 062017
 

Whenever i talk Bot i always think of those Sifi movies where you can have those super complex and intelligent digital beings. When I heard that the bot framework is out I wanted to try it and it wasn’t that easy to learn and develop. I know I am a bit late in this blog so the words might be new but the technology is not that recent. So lets start coding then.

So first off you need to follow this post to set up the environment and the template for the bot. So when you create a new bot project you will given some template files on which you can work directly and that is what we are going to do as well. I will directly use those templates and change them to more meaningful and understandable code.

If you see the template generate files you will observe that the code is divided into two main parts, Controller and Dialog. The Controller will send the message that will come from the user via the bot interface and it will send it to the dialog. The dialog will process the message and will reply accordingly. We can have multiple dialogs you can consider them as routes in a website.

How will we work

MessageController: No change here so we will use the same file as the template generate it.

RootDialog: Here we will instrust the dialog to get user profile information. Do remember that this class needs to be serializable.

EnsureProfileDialog: This dialog will get the user profile from the user. You can also notice that this class is serializable

UserProfile: This serializable datastrcuture will hold the actual profile information

This is how the bot will look like

bot hello world

 Posted by at 10:35 am
Jul 242017
 

I wanted to create a self hosting console application without going to the hassle  of creating a new web application project. So all i had to do was to create a console application and then do the nuget and OWIN do the rest.

Create a new console project and paste this code in the main procedure.

 

So the first thing that you will see if that you will get error as it is unable to identify the WebApp class. So the first thing that you need to do is to include the nuget package. Include the nuget package called ‘Microsoft.AspNet.WebApi.OwinSelfHost’ to the project and then try to compile and run the project. After that you will still get more error that Startup class is missing. So include this class.

Now compile the project and you will be able to compile. Lets see the code now

The WebApp.Start is taking a class called Statup as configuration. The configuration defines multiple configuartion paramatere  but here we are only defining the route though which the we will access the REST full API. Now when you run the application you will see a 404 as its still unable to find the values controller. So lets include that.

Snippet

Now run the app and you will see an output. Wohoo a successful self hosting app.

 Posted by at 10:22 am
Jul 132017
 

To create your first c# Bot you need to setup your environment you the Bot project template and the Bot framework channel emulator. They are not included in the standard VS installation package (at least not yet) so we have to fix it ourselves. Lets fix the project template first. You can download the project template from this link. After you download the template unzip the template and open this path “C:\Users\usernmae\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#” and paste you unzipped folder here. Once you have that you will be able to see the new Bot project template in your ‘Create New Project’ dialog.

bot new project

 

Once you have that you can just hit F5 to run but you will notice that it will only open your selected browser nothing else will happen. You need to communicate with your bot and for that you need an emulator. You can download the emulator from here. Once you have that you can run it and along with the hosted application. We will see the emulator in more detail in the next slide.

Bot Framework Channel Emulator

bot emulator empty

 

 Posted by at 2:59 pm
Jul 122017
 

Though the concept is really simple but there is always an edge confusion between the two. Lets discuss them one by one using example. To understand the concept of Abstract lets consider one of this easy case domain problem. Lets discuss the characteristics of an animal. An animal can have 4 legs if its a horse and two legs if its a Kangaroo (ok you can argue that the arms are legs as well but lets assume that its only two legs and two arms). When i define a object of such characteristics i cant really say directly that an animal has 4 legs. I mean in real life it could have 4 but not when its a Kangaroo. Let me define the two processes separately

If we have a domain case where we say that we cannot haven’t an object of Animal class because we don’t know if it would have 4 or 6 legs. but we could have Horse or Kangaroo objects. Then we define the animal class abstract. What it will do it one it could restrict the type animal to be instantiated and secondly it would force all the child classes to implement the base class abstract method.

If we have a domain case where we have a Passenger class which could travel with ticket. But if its European passenger then we need to check the passport only and if its Non European passenger then we need to check the Passport and visa. To implement that check the example below where the base class(Passenger) method is overridden by the child class based on their nature

 

 

 

 

 

 Posted by at 4:38 pm
Jul 112017
 

Structs are value types so their memory allocation is stored in Stack in the memory on your computer and the class is stored on heap which is a different location the logical memory. The reason behind that is structs are value types and classes are reference type. So if you pass a struct to a method and change the value of that the object you will see that the value is changed within that scope of the method. On the other hand if you do the same with a class and pass it to a method. In that method if you update the value of the object. You will see that the value of the actual object is also changed. see the example below

 

The output from that code is

values before passing Asad Mirza
values updated Ali Mirza
values after passing Asad Mirza
Class Object
values before passing Asad Mirza
values updated Ali Mirza
values after passing Ali Mirza

So you see for the class object the actual value is updated

 Posted by at 11:29 am
Jul 102017
 

In this example i will printing out the list of movies that we get in the form of Json and print them out.

Step1: Create a console project

Step2: Add newtonsoft nuget package

Step3: I will be using a open source api that i will call to pint the title of the movie along with the release year. In this example i will print the movies which have the keyword beast in the title. So here is the code.

Snippet

In the above code the variable text have the resulting json text. which looks like this

[ { “id”: “5628302”, “title”: “Beast (III)”, “year”: “2017”, “poster”: “N/A”, “type”: “movie” }, { “id”: “6414796”, “title”: “Beast (V)”, “year”: “2017”, “poster”: “N/A”, “type”: “movie” }, { “id”: “4261746”, “title”: “Beast (VIII)”, “year”: “2017”, “poster”: “N/A”, “type”: “movie” }, { “id”: “1365050”, “title”: “Beasts of No Nation”, “year”: “2015”, “poster”: “N/A”, “type”: “movie” }, { “id”: “1152398”, “title”: “Beastly”, “year”: “2011”, “poster”: “N/A”, “type”: “movie” } ]

To deserialize it I will cal the newtonsoft deserializer that will map the json values to my object MovieInfo and provide me with the converted list.

 Posted by at 10:38 am