menu

Sunday, September 18, 2016

Data Annotation

Is a way to validate business objects in easy and simplified manner.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace DataAnnotations
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer cust = new Customer();
            try
            {
                cust.name = "";//It will throw exception
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            cust.name = "TestName";
            cust.number = "1234567890";//here length is 11 and requird max length is 10
            ValidationContext context = new ValidationContext(cust);
            List<ValidationResult> vResults = new List<ValidationResult>();
            bool isValid = Validator.TryValidateObject(cust, context, vResults, true);
            foreach (ValidationResult vResult in vResults)
           {
                Console.WriteLine(vResult.ErrorMessage);
            }
            Console.WriteLine("Customer Validation: {0}", isValid);
        }
    }
    public class Customer
    {
        private string _name;
        //Traditional way
        public string name
        {
            get { return _name; }
            set
            {
                if (value.Length == 0)
                {
                    throw new Exception("Name is required !");
                }
                _name = value;
            }
        }
        private string _number;
        //Through data annotation
        [Required(ErrorMessage = "Number is required !")]
        [StringLength(10, ErrorMessage = "Length should be <= 10")]
        public string number
        {
            get { return _number; }
            set { _number = value; }
        }
    }
}

Saturday, September 17, 2016

Change Data Capture (CDC)

In order to enable it we have to enable on 2 level.
1.       Database Level - EXEC SYS.SP_CDC_ENABLE_DB
Once CDC enables, it creates some tables and stored procedures to keep track of records.

2.       Table Level - Before running below query first make sure that SQLServerAgent is running.
EXEC SYS.SP_CDC_ENABLE_TABLE @SOURCE_SCHEMA=N'dbo',
@SOURCE_NAME=N'Sales', @ROLE_NAME=NULL

Once CDC enables on table, it creates one more table.

Insert, update and delete operations on dbo.Sales table will insert log record in cdc.dbo_Sales_CT.
1.       Blank tables

2.       Insert new records to dbo.Sales will make an entry in cdc.dbo_Sales_CT table to keep log of insert with flag 2 in __$operation column.

3.       Update a record will make 2 entry in cdc.dbo_Sales_CT to indicate old as well as new row with flag 3 and 4 respectively in __$operation column.

4.       Delete a record will make one entry in cdc.dbo_Sales_CT to indicate deleted row with flag 1 in __$operation column.

Sunday, September 11, 2016

Custom List with OnAdd Event

using System;
using System.Collections.Generic;
namespace Event
{
    class Program
    {
        static void Main(string[] args)
        {
            CustomList<int> list = new CustomList<int>();
            list.OnAdd += new EventHandler<CustomEventArgs<int>>(OnListAdd);
            list.Add(1);
        }
        static void OnListAdd(object sender, CustomEventArgs<int> e)
        {
            Console.WriteLine("Item: {0} added...", e.Item);
        }
    }
    class CustomList<T> : List<T>
    {
        public event EventHandler<CustomEventArgs<T>> OnAdd;
        public void Add(T item)
        {
            if (null != OnAdd)
            {
                CustomEventArgs<T> args = new CustomEventArgs<T>() { Item = item };
                OnAdd(this, args);
            }
            base.Add(item);
        }
    }
    class CustomEventArgs<T> : EventArgs
    {
        public T Item { get; set; }
    }

}

Sunday, August 28, 2016

Sunday, July 24, 2016

IOC (Inversion of Control)

An entity must concern to its own logic and all unconcerned logic should put inside any other external logic.

Design Patterns

Factory Pattern - Creational Pattern - Remove lots of scattered new keywords by introducing a factory class, and stop exposing our concrete classes by introducing a base interface type.

Abstract Factory Pattern - Creational Pattern – It is an extension of factory pattern, in case we have lots of similar kind of factory pattern classes in to one interface.

Builder Pattern - Creational Pattern - Helps if construction process of an object (Invoice) is complex and do I need to separate construction from its representation. It has 3 component.
Builder - Defines the construction of individual parts.
Director - Takes those individual part from builder and define the sequence to build the product.
Product - is the final object. Ex. construction of Tea object like Tea without sugar and tea without milk.

Prototype Pattern - Creational Pattern - Helps us to give a way to create a clone object from existing object. There are 2 type of cloning.
1.  Shallow cloning - When only parent object is being cloned.
2.  Deep cloning - When with parent, its aggregated child objects are also need to be cloned.
Adaptor Pattern - Structural Pattern - Helps us in case of 2 class types are incompatible because of its incompatible interface. These are of 2 type.
1.   Class Adaptor Pattern –
2.  Object Adaptor Pattern –
Collection classes have Add() and Stack class have Push(), here both are doing same thing but Add and Push are not compatible.

Bridge Pattern - Structural Pattern - Helps to decouple abstraction from its implementation.
Composite Pattern - Structural Pattern - Helps to treat different type of objects in uniform manner.
Decorator Pattern – Structural Pattern – Are nothing but the inheritance.

Proxy Pattern – Structural Pattern – Helps in making available heavy object or sensitive object throughout network by sharing parent interface ref object rather than actual object (Web & WCF Service Client).

Template Pattern – Structural Pattern – Helps in generalising something by using abstract class and make specific type by inheriting abstract class. Here abstract class will be like template for all its derived class.

Mediator Pattern – Behavioural Pattern – Helps in communicating component (purchase, payment, checkout etc.) in a loosely coupled manner. Move communication logic from component to mediator.


Iterator Pattern – Behavioural Pattern – Helps by allowing sequential access of element without exposing the inside code.

SOLID Principles

Is first five object oriented design (OOD) principle given by Robert C. Martin.

S – Single responsibility principle – A class should have one reason to change. Means a class must have only responsibilities related to it.

O – Open-closed principle – Extension should be preferred over modifications. Classes & functions should be open for extension but close for modifications (by sub classing & virtual functions).

L – Liskov substitution principle – Parent type should easily replace its child types (Polymorphism).

I – Interface segregation principle – Means, share only those functionality to clients whichever they want and introduce another more functionalities for new client without affecting old clients.

D – Dependency Inversion Principle – Depending on abstraction not on concretion.
High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.