Tuesday, 5 September 2017

Single Responsibility Principle (SRP)

According to SRP a class should take one responsibility and there should be one reason to change that class. When a class has more than one responsibility, there are also more triggers and reasons to change that class. A responsibility is the same as “a reason for change” in this context.

  •       Base on cohesion.
  •     A class should do only one thing.
  •      Separation of concerns.



For real world example, such as famous Swiss knife. If one of them needs to be changed the whole set needs to be disturbed.



But if those items separated its simple, easy to maintain and one change does not affect the other. The same principle also applies to classes and objects as SRP in software architecture.




For Coding Example ,Check with this Employee class 


namespace SRP
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public bool InsertEmployee(Employee employee)
        {
            // Insert into employee table.
        }
        public bool DeleteEmployee(int employeeId)
        {
            // Delete the Employee in employee table.
        }
        public void GenerateReport(Employee employee)
        {
            // Report generation with employee data
        }
    }
}

Employee” class is taking 2 responsibilities,
  1. Take responsibility of employee database operation
  2. Generate employee report.
There are two reasons to change this class. That is not good approach of software Engineering.

According to SRP, one class should take one responsibility so we should write one different class for
report generation and if any change in report generation should not affect the ‘Employee” class.

public class ReportGeneration
    {
       
        public void GenerateReport(Employee employee)
        {
            // Report reneration with employee data.
        }
    }

Monday, 4 September 2017

String Pool in C#

The string pool is a table that contains a single reference to each unique literal string declared in your application which use by the Common Language Runtime (CLR) to minimize string storage requirements.



string string1 = "xyz";
string string2 = "xyz";


According to above code string1 and string2 assign to same literal string (“xyz”). The string pool contains a single reference for these unique literal strings.

To confirm if the same reference has been assigned we can check for the HashCode for both the string object.
Code and Output as Follow.


Console.WriteLine("Hash Code of string1- " + string1.GetHashCode());
Console.WriteLine("Hash Code of string2- " + string2.GetHashCode());

Sunday, 3 September 2017

What is the Difference between DDL, DML, DCL and TCL Statements in SQL

SQL language is divided into four types of primary language statements.

Follows mention regarding the four main categories of SQL statements:




1. DDL (Data Definition Language)
2. DML (Data Manipulation Language)
3. DCL (Data Control Language)
4. TCL (Transaction Control Language)

DDL (Data Definition Language)

DDL statements are used to define data structures or Schema.
For Example -
CREATE – create a new Table, database, schema
ALTER – alter existing table, column description
DROP – delete existing objects from database


DML (Data Manipulation Language)

DML statements are used to manipulate data itself.
For Example -
SELECT – select records from a table
INSERT – insert new records
UPDATE – update/Modify existing records
DELETE – delete existing records

DCL (Data Control Language)

DCL statements are used to control the level of access that users have on database objects.
For Example -
GRANT – allows users to read/write on certain database objects
REVOKE – keeps users from read/write permission on database objects

TCL (Transaction Control Language)

TCL statements are used to control and manage transactions to maintain the integrity of data within SQL statements.
For Example -
BEGIN Transaction – opens a transaction
COMMIT Transaction – commits a transaction
ROLLBACK Transaction – ROLLBACK a transaction in case of any error

Single Responsibility Principle (SRP)