#BlockChain-Etherum -DAPPS- Accessing Functions between contracts in Solidity.

I am using Remix-Solidity IDE for browser – https://ethereum.github.io/browser-solidity and have started learning solidity from udemy course- Ravinder Deol

In this post ,I will share how to access function between two contracts using solidity over etherum.

Step 1 : Creating a simple contract with Get/Set Function

Lets create a simple contract with a get and set function :

contract SampleContract1
{
uint myvar;
function SetVar(uint varvalue){
myvar = varvalue;
}
function GetVar() constant returns(uint)
{
return myvar;
}
}

Step 2 : Creating contract with constructor function that consumes other contracts

Lets create another contract where we got use function of contract1. In this contract we create constructor where we pass the adress of contract1.

contract SampleContract2
{
SampleContract1 instanceofsc1;
function SampleContract2(address addressofsc1) // constructor
{
instanceofsc1=SampleContract1(addressofsc1);
}

}

 

Step 3 : Accessing Function from other contract 

After we pass the address of contract1,we would be able to access the functions directly in contract2.

contract SampleContract2
{
SampleContract1 instanceofsc1;
function SampleContract2(address addressofsc1)
{
instanceofsc1=SampleContract1(addressofsc1);
}

function GetvarofSc1() constant returns(uint)
{
return instanceofsc1.GetVar();
}
}

Output in Solidity Browser :

Capture

 

Using TopShelf for windows service

Windows service runs continuously in the background and more often used as a back-end job. Its a pain if you want to debug it,even though you use logger to capture all events.we either attach the service as a process and debug it. But TopShelf lets you eliminate all of these and allows you to debug like console  application.

Topshelf has a start and stop method just like a service and also an separate command to install windows service with ease.

DocLink : https://topshelf.readthedocs.io/en/latest/installation/index.html

 

A simple use case with OData Web Api

Scenario :

Assume a case where we have an Employee object and we have an nullable  integer named Age (int? Age).  There are two cases we want to achieve,the first case is to not update the Age property and we could achieve this by passing null value to Age and ignoring the Age Property while saving and second case is to reset the Age i.e Accept whatever is passed by Age Property and save it in db.

Solution :

We want to only update properties of Employee object that is being passed to the controller and we call it as “partial update”(PATCH).  In this case we update only what is being passed and leave the system unchanged. Microsoft provides OData for partial update.

  1. Get Microsoft ASP.NET Web API OData package from Nuget.
  2. In your controller  HTTP verb Method,you could make use of Delta<T>  to map your Employee object and this will take care of partial update with Patch Method provided by ODATA .

public ApiResponse<Statuscode> Patch(Delta<Employee>; employee)

{

var employeebyId == dbcontext.FirstorDefault(x=>x.EmpId ==employee.EmpId);

if(employee !=null)

{

employee.Patch(employeebyId ); // Partial update by ODATA

}

}

 

 

 

Enabling Web Api Tracing

By default ,Tracing is disabled in web api , tracing incoming request might help to track issues in production environment.

Enabling Tracing :

Install the following package from Nuget :

Microsoft.AspNet.WebApi.Tracing

and have the startup .cs configured as below

config.EnableSystemDiagnosticsTracing();

Trace/Log of incoming request to a controller

Capture

Object Comparisons — Difference between Equals method and ==

When we compare two objects with same content,there is a difference between “Equals and  == “.

Equals :

The Equals method does the content based comparison between two objects.

== :

“==” compares only object references.

Scenarios :


Sample s = new Sample () { a = "Hey" };
Sample s1 = s;
Sample s4 = new Sample () { a = "Hey" };

 Console.WriteLine(s1==s); &amp;nbsp;// True --Compares the instances&amp;nbsp;

Console.WriteLine(s==s4); // False ---Eventhough content is same,still == looks only for reference.

Console.WriteLine(s.Equals(s4)); // True --- Compares the value in field "a" &amp;nbsp;in both object and returns true.

String is an exceptional case and its immutable. It behaves with == as Equals.

 

 

 

 

 

 

Dynamic Linq

There are cases where you might need to use dynmaic linq in your Linq Queries, for instance filtering of items based on multiple where clause.

Assume you have list of following Items :


var listofmobiles = new List<Mobile>()

{ 

new Mobile(){ModelNo=1,MobileType= "Android"}

new Mobile(){ModelNo=2,MobileType= "Android"}

new Mobile(){ModelNo=3,MobileType= "BlackBerry"}

new Mobile(){ModelNo=4,MobileType= "Symbian"}

new Mobile(){ModelNo=5,MobileType= "IOS"

}

 }

public class Mobile

{

public long ModelNo {get;set;}

public string MobileType {get;set;}

}

We would write a simple linq query if we where suppose to query only one of mobiletype,assume a case where there can be more than one mobiletype(Android,Ios) the list should still return the items under Android and Ios.

Expression Trees :

To write Dynamic Linq queries,we must construct expression tress that matches the where clause.I am not going to explain more about expression tree but instead usage of System.Dynamic.Linq feature provided by framework which uses expression trees.

Using Dynamic Linq in where clause :

Given different mobiletype (Ios,Android,Symbain ) filter out the above list that matches the mobiletype.


var filterresult =listofmobiles.where("mobiletye @0 or mobiletye @1 or mobiletye @2","Android","IOS","Symbain").Tolist();