Saturday, October 24, 2015

Implicit Vs Explicit Data Types Conversion & Boxing Vs Unboxing

Implicit Conversion: Type safe conversion that not required any special syntax. No data lose because its type safe conversion.

//Implicit conversions
int i = 10000;
double d = i;
           
DerivedType objD = new DerivedType();
BaseType objB = objD;

Explicit Conversion: Converting or casting one datatype to another data type using same casting operators is called Explicit conversion.
This conversion may succeed or not.
This conversion may lose the data.

//Explicit conversions
int j = 1;
bool b = Convert.ToBoolean(j);
           
float f = 232.32f;
int k = (int)f;
                       
BaseType objBase = new BaseType();
DerivedType objDrived = (DerivedType)objBase;

-------------------------------------------------------------------------------
Value Type Vs Reference Type
When a variable is declared using one of the basic, built-in data types or a user defined structure, it is called a Value type. string is an exception because its basic data type but it is reference type.

Instances, arrays kind of types are Reference type

Lets checkout how .net stores these types in memory. How below given variables will be stored.
    //Boxing (Implicit conversion)
    int i = 2;         //1  
    object obj = i;    //2

  1. i is value type so stored in memory allocated in managed stack
  2. New reference type created and stored in stack, in stack memory it contain address(1221) of actual data. Memory allocated in managed heap for new created reference type 'obj' to store actual value. assigned value from i that is 2 is stored here.

    int j = 5;         //3
    obj = j;           //4

    //Unboxing (Explicit conversion)
    int k = (int)obj;  //5
  1. New value type j stored in stack
  2. j is assigned to 'obj', so new memory location(3421) allocated in heap to store new assigned value from j that is 5. Now address contained by obj is changed to new location 3421.
  3. Memory location 1221 is still there and it will be reclaimed by GC later.
  4. New value type k is created and allocated in stack, and assigned value from obj that is 5.
     k = i;  //6
  1. Value of k is overridden by value of i
Point to be noted: 
  1. When assign new value to a value type, value overridden and not required new memory allocation.
  2. As execution of memory block completed where the value type is declared, memory get released. As closing parenthesis executed memory used by ID will be released.
            private void SendSMStoClient()
            {
                int ID = 50;
            }
  3. When we assign new value to a reference type new memory allocated, and old one still exist there that is reclaimed by GC later. that is memory & performance overhead with reference types.
  4. Reference types are useful to pass them one function to other. While when we pass value type new copy passes of value type.
-------------------------------------------------------------------------------

Boxing: Converting value type to reference type. This is implicit conversion.
Unboxing: Converting reference type to value type. This is explicit conversion.

    //Boxing (Implicit conversion)
    int i = 2;
    object obj = i;

    //Unboxing (Explicit conversion)
    int k = (int)obj;

Why Boxing and Unboxing downgrades performance?
There is specific storage system for value types and for reference types.
Value types are stored in stack.
Reference types' reference pointer (address) is stored in stack but actual data stored in heap.
So whenever we perform Boxing or Unboxing .net internally changes the storage of data that is overhead so it downgrade performance.

String Vs StringBuilder

string is Immutable it means when we initialize a newly created string value it get stored in memory then if we assign some new value to same variable new value stored on new memory place and variable now refer this new memory place, old value still exist in memory.
If again assign a third value to same variable, again new value get stored on new memory location,
and variable refer it now, still previous two have existence.
Conclusion: value of string can't be update actually
string s = "Hello";

StringBuilder :  Value of stringBuilder can be updated. So it is recommended to use for better performance.
StringBuilder s = new StringBuilder();
s = s.Append("Hello");

Useful data types in .Net



S.No
Data Type
Size
Min Value
Max Value
Description
1
Byte
1 Byte (8 bit)



2
Char
2 Bytes


Each character contains 2 Bytes
3
String
Assigned chars * 2 Bytes



4
Int16 / Short
16 bit (2 Bytes)
-32768
32767

5
UInt16 /ushort
16 bit (2 Bytes)
0
65536

6
Int32 / int
32 bit (4 Bytes)
-2147483647
2147483648

7
UInt32 / uint
32 bit (4 Bytes)
0
4294967296

8
Int64 / long
64 bit (8 Bytes)
Neg (2 pow (64) / 2 -1)
2 pow (64) / 2

9
UInt64 / ulong
64 bit (8 Bytes)
0
2 pow (64)

10
bool




11
DateTime




12
float
4 Bytes


7 deccimal places
13
decimal
8 Bytes


16 decimal places
14
double
16 Bytes


29 decimal places

Tuesday, October 20, 2015

GAC - Global Assembly Cache

GAC - Global Assembly Cache

This is a central repository for assemblies in computer. Only strong named assemblies can be registered in GAC.
Location of GAC in any computer is C:\Windows\assembly\
Command to register
gacutil -i assemblyname.dll

Use & advantages of GAC:

  1. Multiple applications can refer same assembly from GAC
  2. GAC can maintain multiple versions of a assembly, so different assemblies can refer their required versions
    Example :
    Our exe is required Myassembly.dll version 1.0.0. Now there is new version available of Myassembly.dll that is 2.0.0 in GAC. We can place a config file with exe mapping old dll to new dll, to  call new dll in exe.
    <bindingredirect oldversion="1.0.0" newversion = "2.0.0" />
  3. Assemblies are secured with windows security, any user of system required admin rights to update or delete assembly 

Strong Name: Strong name contains name, version & public, private or both token keys. A signed assembly is called Strong named assembly

Weak Name: Weak Name does't have public or private key token

Delay Singing: Whenever we sign a assembly there is a option 'Delay Sign Only', if that is chosen later we can again assign a new key file (.snk file) to the assembly
Use of Delay signing : Many times you required strong named assembly in development, so you can sign assembly with public key with delay singing and provide to developers.
At the time deployment on production you can re - assign new key.

Use of IDisposable

IDisposble is used for writing object cleanup functionality for our class.
  1. Implement IDisposable and define Dispose() to cleanup managed object
  2. Implement Destructor (Finalizer) to cleanup unmanaged object, do not use Destructor if do not have at least one unmanaged object in class

How to implement?

1. Write Dispose(bool) method
        public void Dispose(bool disposing)
        {
            if (_dispose) return;

            if (disposing)
            {
                //Free managed code
            }

            //Free unmanged code
            _dispose = true;
        }
2. Call Dispose(bool) with true method from actual Dispose() method of IDisposable, to release managed object.
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

Below statement suppress destructor of the class for managed object. 
If we do not write this GC puts some objects in generation 1 and generation 2 also that degrade performance
With this statement GC puts objects in 0th generation that improve performance
GC.SuppressFinalize(this);

3. Call Dispose(bool) with false from destructor to release unmanaged objects.
        ~clsSecurityCritical() //Finalizer
        {
            Dispose(false);
        }
4. GC.SuppressFinalize(thisRequests that the CLR not call the finalizer for the specified object.

Code :
    public class clsSecurityCritical : IDisposable
    {
        public clsSecurityCritical()
        {
   
        }
        public void CallCheck()
        {
            Console.WriteLine("Security Critical code executed");
        }
        ~clsSecurityCritical() //Finalizer
        {
            Dispose(false);
        }

        bool _dispose = false;
        public void Dispose(bool disposing)
        {
            if (_dispose) return;

            if (disposing)
            {
                //Free managed code
            }

            //Free unmanged code
            _dispose = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

    }

Garbage Collector

What is Garbage Collection?
Garbage collection is a service to freeing up memory space occupied by dead objects or objects not in use from a long time.
It is performed by 'Garbage Collector' that is nothing but a background thread. Garbage Collector is part of CLR.
Garbage Collector maintain a heap of created object for your application, it periodically checks and de-allocate memory occupied by objects no longer required (dead or not used by long time)

When it perform collection?
  1. Periodically
  2. There is predefined threshold(memory limit) for heap of your application, if memory occupied cross that limit  
  3. When physical memory become low
What are Generations?
GC maintain a heap of objects for your application in that it categories objects in three generations,
generation 0, generation 1 & generation 2.

Newly created objects marked with generation 0. Big size newly created objects placed in Generation 1 & 2 with the assumption that they will have long life.

Newly created objects get placed in Gen 0.

GC performs collection on generation 0, it releases memory from dead objects & moves alive objects to generation 1.
GC performs collection on generation 1, it releases memory from dead objects & moves alive objects to generation 2
GC performs collection on generation 2, it releases memory from dead objects.

0 Generation objects are short life object
1 Generation objects are middle life object
2 Generation objects are long life object
All short live objects resides in 0th generation so GC performs collection on 0th generation more than 1st generation, and so on..

Sunday, October 18, 2015

Managed Vs Unmanaged Code

Managed Code: Code written in any .net language run under CLR environment is managed code.

CLR provides it's services to manged code
  1. Garbage Collection
  2. Error Handling
  3. CTS (Common Type System)
  4. CAS (Code Access Security)
  5. Code Verification 
  6. Performance Improvement: Decides which JIT to use & assigns to JIT. JIT compiles IL to native m\c code.
Unmanaged Code: Code that does not target CLR environment for execution is UnManaged code.

Language Compilers checks below items at compile time but these are again rechecked at runtime by CLR because 
MSIL can be modified by external tools like ILDASM or ILASM.

  1. Types 
  2. Stack usage 
  3. Method calls 
  4. Memory safety 
  5. Control flow 

CAS & Changes in .Net Security Modal in .Net 4.0

CAS (Code Access Security): CAS Identifies code, allows only resources & operations to do by the code for those code have permission, based on evidences.
There is set of permissions, policies can be seen with ".Net Framework Configuration" tool. Using this tool we can assign/revoke specific permissions to any code (exe).

This modal has deprecated in .Net framework 4.0


Change from .net 4.0 in security modal is granting permission depend on host, not on CAS modal.
Now, host decides permissions for any code & assign it.
Like if we are executing any assembly, windows identify, categorizes & assign permissions to it.

Code is divided in three types with respect to security
SecurityCritical: Trusted Code, have access to your system
SecurityTransparent: Untrusted Code, should not have acces to system, It must not have any direct call to Security critical code
Security Safe Critical: Used to create bridge b\w transparent and critical code, if want to call critical code from transparent code

How can we mark our code with any of above given category?
Add attributes to AssemblyInfo.cs file of project as given in below picture


If you want to use some third party dll and don't want to give that access to Security Critical code then wrap-up the third party dll with your own code and mark that Security Transparent.
Sand Boxing: 
If you want to use some untrusted third party dll, then create your app domain, assign permissions to that app domain & run the third party dll under this domain, this approach is called Sandboxing.

Saturday, October 17, 2015

CLR, CLS, MSIL, CTS & JIT

MSIL (Microsoft Intermediate Language): An intermediate language generated by .net compilers (C#,VB,VC++...) when compiles source code.

CLR (Common Language Runtime): CLR is runtime environment of .net responsible to compile & manage .net code. Verifies the code for security, provide code access security means restrict code to access restricted area & resources of system, provides garbage collection service.

CLS (Common Lanuage Specifications): There is a defined set of specifications that should be followed by every compiler who is targeting to communicate with .net languages.

CTS (Common Type System): There is a defined set of Types, every .net language compiler compiles code with these common data types, so that all .net languages can communicate.
After compilation IL code of every .net language code, have common types.

JIT (Just In time Compiler): JIT is responsible to compile MSIL code into native machine code and save it in memory.

Types of JIT compiler
  1. Pre JIT  - Compiles whole code in a single cycle.
  2. Econo JIT - Compiles code part by part when required. It means compiles methods those called at runtime, and then remove those compiled code when not required. That means called code compiles, serves and then frees.
  3. Normal JIT - Compiles code part by part as Econo but only once when any code part called first time, then it put that code to cache and if required later, then serves from cache that part.
    Normal JIT = Econo JIT + Caching


Wednesday, October 14, 2015

SQL - Pivot & Unpivot

Pivot & Unpivot are relational operators.
Pivot is used to rotate table valued expression. In a table valued expression, it turns unique values of one column into multiple columns and perform aggregation on any of other columns that is required in output.
Unpivot is used to perform opposite operation of Pivot, In a table valued expression, it turns multiple columns into one column values with multiple rows.

Syntax
SELECT <non-pivoted column>,
    [first pivoted column] AS <column name>,
    [second pivoted column] AS <column name>,
    ...
    [nth pivoted column] AS <column name>
FROM
    (<SELECT query that produces the data>)
    AS <alias for the main source query>
PIVOT
(
    <aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
    IN ( [first pivoted column], [second pivoted column],
    ... [nth pivoted column])
) AS <alias for the pivot table>

<optional ORDER BY clause>

Example
 Table valued sql expression
Pivoted table 

Unpivot Example



Monday, October 12, 2015

SQL - Change Data Capture (CDC)

CDC - Change data capture helps us to track insert/update/delete on any table
CDC is not available in express edition, it is available in enterprise, developer & enterprise evaluation editions

Steps to enable CDC on one or more tables those you want to track
  1. First enable CDC at database level
    sp_cdc_enable_db
  2. Enable CDC at table level
    sp_cdc_enable_table
    @source_schema = 'dbo',
    @source_name = 'table1'

    @role_name = null

    sp_cdc_enable_table
    @source_schema = 'dbo',
    @source_name = 'table2'

    @role_name = null
Sql creates some 6 new tables and lots of procedures to track and store data of change.
For every table on which you on CDC it creates a table with name pattern for above example
cdc.dbo_table1_CT
cdc.dbo_table2_CT

Sql stores all changed data in their respective table with a column "_$operation" that have values as given below
1 - deleted record
2 - Inserted record
3 - Record before update
4 - Record after update

SQL - Useful system functions

1. Coalesce
--COALESCE ( expression [ ,...n ] )
 --Returns value from first not null column from the provided columns
  select Coalesce(FirstName,MiddleName,LastName) from tb_userdesc

2. Row_Number
 --Row_number()
 --Sorted on FirstName and given serial number
 select Row_number() over (order by Firstname) as SNo, userid, FirstName, Address_Type from tb_userdesc





 -- Records grouped on values of Address_Type then these group has sorted on UserId with row number restarted
 select Row_number() over (PARTITION by Address_Type order by UserId) as SNo, userid, FirstName, Address_Type from tb_userdesc




3. Rank
 --Rank()
 -- Generates sequence like Row_number, gives same number for same values in order by column. But for next value uses the number that actually returned by Row_number
 select rank() over (order by FirstName) as SNo, userid, FirstName, Address_Type from tb_userdesc




4. Dense_Rank
 --dense_rank()
 --Generates sequence like Row_number, gives same number for same values in order by column. For next value uses the next number in sequence

 select dense_rank() over (order by FirstName) as SNo, userid, FirstName, Address_Type from tb_userdesc



SQL - Stored Procedures

Stored procedures are precompiled sql statements.

Sql caches execution plan for stored procedures and on every execution of procedure it fetches execution plan from cache by procedure id and executes it.
So, Sql no need go through steps of syntax checking(step1) and plan selection(step2), this increases performance of application that uses the procedure.

While Sql also caches execution plan for sql statements we execute. But if we change any value sql treated it as new sql statement and cache it again with different name.
Example
Select * from employees where Name like '%Ram%'
Select * from employees where Name like '%Raj%'
Plans for above both statements will be cached but with different Ids, so can't take benefit of it.


Procedure Syntax :
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
       -- Add the parameters for the stored procedure here
       <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
       <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
       -- SET NOCOUNT ON added to prevent extra result sets from
       -- interfering with SELECT statements.
       SET NOCOUNT ON;

    -- Insert statements for procedure here
       SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END

Node | Cluster Vs Worker Threads

Cluster: Multiple processes (scale app across CPU cores) Worker Threads: Multiple threads (handle CPU-heavy work inside one process) Cluster...