Saturday, May 10, 2014

Facade Pattern

Facade Pattern falls under the Structural Pattern category of Design Patterns. As per GoF, Facade Pattern can be defined as "unified interface to a set of sub system. Facade defines high level interface that makes the sub systems easy to use".

Lets define the Facade Pattern with the help of an example as given below.

Lets take a scenario around Ticket Reservation System. Lets design a real world application called "Ticket Reservation System". This system is used to reserve tickets for Flight, Train, Bus modes of transport. So here the user needs to interact with Flight Reservation Sub system, Train Reservation Sub System, Bus Reservation Sub System etc. So lets define each of the Sub systems as given below

Flight Reservation Sub System:

Train Reservation Sub System :
Bus Reservation Sub System :
Now lets define a Facade which interact with all the above Sub systems so that all these sub systems are hidden from a client perspective. Here we have the feasibility to plug in more Sub systems into this picture

Ticket Reservation Facade:

Now lets once again define the high level interactions between the client, Facade and Sub systems as follows.



Please click the link for the Source code for the above Facade example... DOWNLOAD.


Tuesday, May 6, 2014

Singleton Pattern

Singleton Pattern is one of the widely used Creation Pattern in Software design. Singleton pattern which allows only on instance of class to be created and shared between clients and provides a global access to the instance. Moreover no client can instantiate an object  of the class.

Singleton Pattern uses a private constructor and static readonly instance variable which is lazily initialized. Below code snippet better explains the Singleton Pattern

First lets create Singleton class which is used to create and instantiate a unique instance.

Below snippet shows that the invoked class instances are same.


You can download the above example from the following link DOWNLOAD.

Monday, May 5, 2014

Factory Method Pattern

Factory method pattern is  a type of Creational category of Design pattern. Factory Method Pattern defines an interface for creating objects and it assigns the privilege for creating objects to sub classes. It abstracts the logic for creating which type of class to create.

  • Objects created through Factory method avoids the dependency of Sub classes code with specific implementation of the interface which it implements.
  • Factory method returns the same object for same set of parameters and hence we can skip duplicate objects
  • We can create and return any sub type of the type that factory is designed to create.
  • Object creation is abstract from client code


Below diagram explains the pattern in high level with different components involved.


Lets explain the above components in brief as follows

Product: Defines the interface for objects
Concrete Product: Implements the Product interface
Creator : Declares the factory method which returns the Product
Concrete Creator:Overrides the factory method to return Concrete Product.

Lets have the following implementation for the Factory method.

Product :



Concrete Product :

Creator : 

Implementation :

So here we have defined the Factory Method. Keep watching this space for more articles on Design Patterns.





Sunday, May 4, 2014

.NET Design Patterns

Design Patterns can be explained as recurring solutions to recurring problems in Software architecture. Patterns are all about reusable design and interactions of objects. The 23 Gang Of Four (GOF) patterns are considered as the foundation for all other patterns.

We can classify the Design Patterns as mainly onto

  • Creational
  • Structural
  • Behavioral

Creational Patterns:

Abstract Factory
Creates an instance of several families of classes
Builder
Separates object construction from its representation
Factory Method
Creates an instance of several derived classes
Prototype
A fully initialized instance to be copied or cloned
Singleton
A class of which only a single instance can exist

Structural Patterns :

Adapter                
Match interfaces of different classes
Bridge    
Separates an object’s interface from its implementation
Composite
A tree structure of simple and composite objects
Decorator
Add responsibilities to objects dynamically
Facade
A single class that represents an entire subsystem
Flyweight
A fine-grained instance used for efficient sharing
Proxy
An object representing another object

Behavioral Pattern :

Chain of Resp
A way of passing a request between a chain of objects
Command
Encapsulate a command request as an object
Interpreter
A way to include language elements in a program
Iterator
Sequentially access the elements of a collection
Mediator
Defines simplified communication between classes
Memento
Capture and restore an object's internal state
Observer              
A way of notifying change to a number of classes
State
Alter an object's behavior when its state changes
Strategy                
Encapsulates an algorithm inside a class
Template Method
Defer the exact steps of an algorithm to a subclass
Visitor
Defines a new operation to a class without change

Lets discuss each of these Patterns in my upcoming blogs. All the above definitions are referred from GOF. Keep watching this space...

C# Parameter Modifiers

Here lets discuss about the various Parameter modifiers that can be used to manage how arguments are passed to methods.

  • Default by Value Parameter : If we are not specifying any Parameter Modifier for an argument, by default it is set to Value Parameter. Since we are having Value types, the invoked method will be working on a copy of the original data. This can be better explained with the example as given below
Here you can see that the values are not changed even after the invoked method has changed the values. 
  • The out modifier :Out modifier is useful in scenarios where we need to return more than one values. Below example supports this scenario.
The invoking  method should set the value to the out parameter.
  • The ref modifier : The reference parameter modifier is used in scenarios where we need to change the values in the invoked methods. Unlike the out modifier , here we need to initialize the ref parameters before we pass to any method. The below example better explains the same.



  • The params modifier : We can use params modifier to pass array of similar types as arguments to a method as a single logical parameter. If we specify the params modifier , we can pass the parameters as comma delimited arguments. Below example better explains the same as given below
  • The optional modifier : The optional parameter modifiers are helpful in allowing the caller with the flexibility of passing only required parameters. The below example illustrate the concept


  • Named Parameters : Named modifier allows you to pass the parameters in any order to a method. This provides a readable and a cleaner code. But named parameters has got a performance draw back.

So here we have defined various C# Parameter modifiers.



Saturday, May 3, 2014

Intrinsic Data Types of C#

C#
CLS Compliant
System Type
Range
Definition
bool
Y
System.Boolean
T or F
True or False
sbyte
N
Syste.Sbyte
-128 to 127
Signed 8 bit number
byte
Y
System.Byte
0 to 255
Unsigned 8 bit number
short
Y
System.Int16
-32768 to 32767
Signed 16 bit number
ushort
N
System.Uint16
0 to 65535
Unsigned 16 bit number
int
Y
System.Int32
-2147483648 to 2147483647
Signed 32 bit number
uint
N
System.Uint32
0 to 4294967295
Unsigned 32 bit number
long
Y
System.Int64
-9223372036854775808 to 9223372036854775807
Signed 64 bit number
ulong
N
System.Uint64
0 to 18446744073709551615
Unsigned 64 bit number
char
Y
System.Char
U+0000 to U+ffff
Single 16 bit Unicode character
float
Y
System.Single
-3.4 1038 to +3.4 1038
32 bit floating point number
double
Y
System.Double
+-5.0 10-324 to +-1.7 10-308
64 bit floating point number
decimal
Y
System.Decimal
(-7.9x 1028 to -7.9x 1028)/(100 to 28)
128 bit signed number
string
Y
System.String
Limited
Set of unicode characters
object
Y
System.Object
No limit
Base class

Common Language Runtime

Runtime can be defined as a collection of services that are required to execute a given compiled unit of code.
.NET Runtime provides a single, well defined runtime layer that is shared across all.NET aware languages and is called as Common Language Runtime a.k.a. CLR.

Apart from code execution , CLR provides memory management, thread management, security management, code verification and compilation.

Lets discuss the roles of CLR as given below

Base Class Libraries : This provides class libraries required for an application in .NET world. CLR converts the source code into an intermediate language called Microsoft Intermediate Language (MSIL).

Thread Support: CLR manages the parallel execution of code via threading.

Code Manager : CLR manages the compiled Intermediate Language

Garbage Collector
Garbage Collector handles automatic memory management and it will release memory of unused objects in an application, this provides automatic memory management.

CLSCommon Language Specification (CLS) is defined as a set of rules that describes the minimal and complete set of features a given .NET aware compiler should support. 

CTSCTS defines how various types are declared, used and managed in Common Language Runtime in .NET world

Security Engine: It enforces security permissions at code level security, folder level security, and machine level security using Dot Net Framework setting and tools provided by Dot Net.

Common Language Specification

Common Language Specification (CLS) is defined as a set of rules that describes the minimal and complete set of features a given .NET aware compiler should support. Alternatively we can define it as a sub set of CTS as well. CLS does not apply to the implementation logic. Member definition should conform to the CLS compliance.

Friday, May 2, 2014

Common Type System Overview

In .NET world, Type refers to a generic term used to represent Class, Interface, Enumeration , Delight etc. Whenever we are building any solution around .NET we do interact with these types. So lets define CTS aka Common Type System as "CTS defines how various types are declared, used and managed in Common Language Runtime in .NET world"

  • CTS helps in cross language interaction
  • Provides an Object  oriented model
  • Defines the rules that languages should follow for language interaction.
  • Provides a Base class library that contains primitive data types.
Types can be literally classified as Value Types and Reference Types. Lets define the classification as follows 
Value Types
Value types are data types whose objects are represented by the object's actual value. If an instance of a value type is assigned to a variable, that variable is given a fresh copy of the value.

Reference Types:
Reference types are data types whose objects are represented by a reference (similar to a pointer) to the object's actual value. If a reference type is assigned to a variable, that variable references (points to) the original value. No copy is made.

CTS supports the following categories of types
  • Classes : Classes are Reference types. It defines the operation an object can perform and the data that object contains
  • Structures : Structures refers to Value types. It derives from System.ValueType. Used extensively in low memory requirements.
  • Enumerations : Enumeration are Value types. It derives from System.Enum. It supplies alternate names for the values of an underlying primitive type.
  • Interfaces : Interfaces are contracts which contains declarations of method or properties which an implementing class will do the implementation.
  • Delegates : Delegates are Reference types which is a functional pointer used in event handlers and call back functions in .NET.
CTS type definitions includes the following
  • Any attribute defined on type
  • Type accessibility
  • Type name
  • Types base types
  • Any interface defined by the types
  • Definition for each type members
Type members defines the behavior and state of a types. The following are type members
  • Fields
  • Properties
  • Methods
  • Constructors
  • Events
  • Nested types

Installing ASP.NET MVC

Installing ASP.NET MVC Before we dive deep into the ASP.NET MVC, let’s install it our development machine. This is very straight forwa...