Skip to main content

Posts

Decorator Design Pattern

Intent:
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing for extending functionality (also known as Wrapper)





using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DecoratorPattern { publicclass UpperStream : Stream { private Stream _stream; public UpperStream(Stream stream) { this._stream = stream; } publicoverridelong Length { get { return _stream.Length; } } publicoverrideint Read(byte[] buffer, int offset, int count) { var numberOfBytesRead = _stream.Read(buffer, offset, count); for (int i = 0; i < numberOfBytesRead; i++) { buffer[i] = (byte)char.ToUpper((char)((int)buffer[i])); } return numberOfBytesRead; } publicoverrideboo…

Strategy Design Pattern

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

Problem:
Client wants to decide at run-time what serialization it should use to serialize a type. Many different serialization algorithms are already available.
Solution:
Encapsulate the different serialization algorithms using the Strategy pattern!
C# Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StrategyPattern { publicinterface ISerializer { void Serialize(); } publicclass XmlSerializer : ISerializer { publicvoid Serialize() { Console.WriteLine("Xml serilizer Invoked"); } } publicclass JsonSerializer : ISerializer { publicvoid Serialize() { Console.WriteLine("Json serializer Invoked"); } }�…

Adapter Design Pattern

Adapter Pattern: The Adapter pattern converts the interface of a class into another interface that clients expect.The client makes a request on the adapter by invoking a method from the target interface on it and then adapter translates that request into one or more calls on the adaptee using the adaptee interface. The client receives the results of the call and never knows there is an adapter doing the translation

Pre-Condition: You are maintaining an existing system that makes use of a third-party class library from vendor A
Stimulus: Vendor A goes belly up and corporate policy does not allow you to make use of an unsupported class library.
Response: Vendor B provides a similar class library but its interface is completely different from the interface provided by vendor A
Assumptions: You don’t want to change your code, and you can’t change vendor B’s code
Solution?: Write new code that adapts vendor B’s interface to the interface expected by your original code

C# Example




using System;
usi…

Creating Organization Hierarchy in ASP.NET using Google Organizational Chart

In this post I will show you how to integrate google org chart in your asp.net application. First create a new empty website in visual studioAdd jquery and google visualization api reference to the default page Let's first create dummy data.Create following table schema and insert some dummy data into this. Table Schema CREATETABLE [dbo].[EMPLOYEES] ( [EmpID] CHAR (4) NOTNULL, [Ename] VARCHAR (10) NULL, [MGR] CHAR (4) NULL, PRIMARYKEYCLUSTERED ([EmpID] ASC), UNIQUENONCLUSTERED ([EmpID] ASC) ); Insert Data INSERTINTO [dbo].[EMPLOYEES] ([EmpID], [Ename], [MGR]) VALUES (N'7369', N'Smith', N'7902') INSERTINTO [dbo].[EMPLOYEES] ([EmpID], [Ename], [MGR]) VALUES (N'7499', N'Allen', N'7698') INSERTINTO [dbo].[EMPLOYEES] ([EmpID], [Ename], [MGR]) VALUES (N'7521', N'Ward', N'7698') INSERTINTO [dbo].[EMPLOYEES] ([EmpID], [Ename], [MGR]) VALUES (N'7566', N'Jones', N'7839'…

How to read mp3 tag info using c#

Mp3 tag  consists of attaching a fixed block size of 128 bytes at the end of the file in question. This block contains the following tags: A header that identifies the presence of block ID3 and version. Specifically, said header comprises TAG characters.

Title: 30 characters.
Artist: 30 characters.
Album: 30 characters.
Year: 4 characters.
Comment: 30 characters.
Genre (music): a character.

How to encrypt Bitmap image in c#

In this post I will show you how to encrypt bitmap image in c#.The technique is very simple.First we extract header from the image and then encrypt the rest byte data and then combined the header with this encrypted data.


using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace Enc { class Program { staticstring FILENAME = @"D:\ub.bmp"; staticstring ENCFILENAME = @"D:\enc.bmp"; staticvoid Main(string[] args) { //Create instance of DES TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); //Generate IV and Key des.GenerateIV(); des.GenerateKey(); //Set Encryption mode des.Mode = CipherMode.ECB; //Read FileStream fileStream = new FileStream(FILENAME, FileMode.Open, FileAccess.Read…

How to post array of hashes in wcf

In this post I will show you how to post json array hashes in wcf rest service.Before going to code snippet let's discuss some basic terminology.

What is Array of Hashes
Array of hashes is a data structure in which each record itself contains key/value pairs. Arrays of hashes are used less frequently than the other structures.Below is example of array of hashes
AoH = ( { husband => "barney", wife => "betty", son => "bamm bamm", }, { husband => "george", wife => "jane", son => "elroy", }, { husband => "homer", wife => "marge", son => "bart", }, ); JSON String

When we enclosed the json data into quote then we called this string is JSON string
var jsonString='[{ "x": 10, "y": 20, "z": 30 }, { "x": 12, "y"…