Wednesday, May 21, 2008

Design Patterns Series - part 1

What is Design Patterns :
Design patterns are recurring solutions to software design problems you find again and again in real-world application development. It is a proven solution for a specific problem in specific context. There are lot of design patterens in world. Here i am going to discuss GOF(Gang of Four) design patterens.

Types of Design Patterns:
I. Creational
II. Structural
III. Behavioral

I. Creational Patterns:
1. Abstract Factory -
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

2. Builder - Separate the construction of a complex object from its representation so that the same construction process can create different representations.

3. Factory Method - Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

4. Prototype - Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

5. Singleton - Ensure a class has only one instance and provide a global point of access to it.

II. Structural Patterns:
1. Adapter - Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

2. Bridge - Decouple an abstraction from its implementation so that the two can vary independently.

3. Composite - Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

4. Decorator - Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

5. Facade - Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

6. FlyWeight - Use sharing to support large numbers of fine-grained objects efficiently.

7. Proxy - Provide a surrogate or placeholder for another object to control access to it.

III. Behavioral Patterns:
1. Chain of Resp -
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

2. Command - Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

3. Interpreter - Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

4. Iterator - Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

5. Mediator - Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

6. Memento - Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

7. Observer - Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

8. State -Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

9. Strategy - Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

10. Template Method - Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

11. Visitor - Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Posted by at 17:08:39 | Permanent Link | Comments (0) |

Tuesday, May 13, 2008

object class

object class: Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.

Members and Methods:
Equals
Finalize
GetHashCode
GetType
ReferenceEquals
ToString
Posted by at 16:26:06 | Permanent Link | Comments (0) |

Self Join - Sql server

Self-joins : A self-join is a query in which a table is joined  to itself.  Self-joins are used to compare values in a column with other values in the same column in the same table

Example:  In this example i am tring to insert data into Employee table with emp id, manager id. Manager id referes same tables emplid. Finally by using innerjoin i wanted to get data with manager name rather than manager id.

1. Create table
CREATE TABLE dbo.Employee

(

empId int NOT NULL IDENTITY (1, 1),

empName nvarchar(50) NOT NULL,

managerId int NULL

) ON [PRIMARY]

2. Insert Data into Table
1,Madhu,NULL
2,Bala,1
3,Krishna,1
4,Ram,1

3. SQL Query - Self Join

select a.empId, a.empName as Employee , b.empName as ManagerName

from Employee a, Employee b where b.empId=a.managerId


4. Output
2,Bala,Madhu
3,Krishna,Madhu
4,Ram,Madhu

Posted by at 14:11:33 | Permanent Link | Comments (0) |

Friday, April 25, 2008

enum - most useful functions


When i am doing .NET programming, many times i have get use of common tasks required related to 'enum'. Then i taught of writing some common functions related to enum reading, parsing, iterating enum etc. Here we go ...

What is enum: enumuration is comprised of a number of named constants.

Creating Enumuration :
public enum UIType
{
None,
Label,
DropDown,
TextBox,
CheckBox,
ListBox,
DateTimePicker
}

Convert Enumuration Item to string :
string itemValue = UIType.CheckBox.ToString();

Convert string to Enumuration instance:
returnValue = (UIType)Enum.Parse(typeof(UIType), str, true);

Note:
// While parsing enum it throws an ArgumentException if the string is found not to be one of the members of the enum. you must handle it.

Iterating Enum and reading data from Enum:
foreach
(string s in Enum.GetNames( typeof(UIType)))
dropdownlist1.Items.Add(s);

Convert integer to Enum instance:
UIType chkbox=(UIType)Enum.ToObject(typeof(UIType), (int)UIType.CheckBox);

 
Posted by at 18:58:42 | Permanent Link | Comments (0) |

Thursday, April 10, 2008

Maintain SQL Server 2005 DB changes in log table

Maintaining SQL Server DDL command changes in log file is most important and now it becomes very easy in SQL Server 2005 by using EVENTDATA () function.
 
EVENTDATA()
is a function is provided by SQL Server 2005, It provides
Information about an event that fires a DDL trigger is captured by using the EVENTDATA function. This function returns an xml value. The XML schema includes information about the following:
·         The time of the event.
·         The System Process ID (SPID) of the connection when the trigger executed.
·         The type of event that fired the trigger.

Steps for implementation of storing DDL log changes in Table

1.      Create table to store DDL Transactions


   CREATE
TABLE [dbo].[DDLLog](
   [logId] [int] IDENTITY(1,1) NOT NULL,
   [logData] [xml] NOT NULL,
   CONSTRAINT [PK_DDLLog] PRIMARY KEY CLUSTERED
   ([logId] ASC)) ON [PRIMARY]

2.     
Create Trigger to fetch DDL commands info and store in DDLLog table  

   CREATE
TRIGGER [Trig_DDLLog]
   ON DATABASE FOR DDL_DATABASE_LEVEL_EVENTS
   AS
   INSERT INTO dbo.DDLLog(logData)
   SELECT EVENTDATA()
   GO
   ENABLE TRIGGER [Trig_DDLLog] ON DATABASE

3.     
Create a test table to check 

   create table testTable (id int, description varchar(100))

4.     
Watch DDLLog table for log entry …it is available in XML format like below.

<
EVENT_INSTANCE>
<EventType>CREATE_TABLEEventType>
      <PostTime>2008-04-10T11:48:08.463PostTime>
      <SPID>53SPID>
      <ServerName>BSIAP021ServerName>
      <LoginName>saLoginName>
      <UserName>dboUserName>
      <DatabaseName>AdventureWorksDatabaseName>
      <SchemaName>dboSchemaName>
      <ObjectName>testTableObjectName>
      <ObjectType>TABLEObjectType>
      <TSQLCommand>
            <SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON"
            ANSI_PADDING="ON" QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE"
            />
<CommandText>create table testTable (id int, description varchar(100))
            CommandText>
      TSQLCommand>
EVENT_INSTANCE>

References: http://technet.microsoft.com/en-us/library/ms187909.aspx

Now it is so simple to maintain SQL Server DDL changes log 

 

*** END ***

Posted by at 13:04:38 | Permanent Link | Comments (0) |