Objection of a blog reader about Islam and my short reply

August 29, 2010

I am getting heavy traffic on my blog. One of the reader from Amsterdam – NL passed comments as:

Author : Me (IP: , 95-42-82-81.btc-net.bg)
E-mail : xxxxxx@yahoo.com
Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=
Why the hell someone wants to write his religion in “CV” ? Muslim ? So what ? So silly ? What if I’m Christian or atheist ? You could kill me ? Hello ? Can’t read your post, I’m repulsed.

Reference: https://softarchitect.wordpress.com/2010/08/14/software-architecture-c-sharp-4-0-new-feature-whywhathow-of-managed-extensibility-framework-mef-sample-calculator-code/

My Short Reply was as:

Thanks for your comments about my blog. Your concerns are very valuable to me. I really appreciate that.

Now coming back to your question. Have you ever seen national id card which is used to identify a person. It has all information like name, age (date of birth), sex, martial status, region and address. Actually all these information are vital to identify a person. In my blog I have put all these information, just to identify me. Answer to question “Who is Shahzad?”

Now coming to second part, “Is Islam really a region of hate or terrorism?” This whole theory is sponsored by certain groups specially media tycoons. TV, radio, newspapers and magazines, are influenced by their investors or owners who have certain agendas and who want to promote certain values and points of view. So this whole thing is Mind Control game which is against Muslim these days.

Read the article below, which explain Islam and its values.
[Read all 5 pages]
Secondly, read book:

Don’t afraid about Islam and Muslims. Get close to them, you will find them niece and friendly.

Have a nice day.

Software Architecture – Replication Limitations of SQL Server Express Edition

August 19, 2010

SQL Server Express Edition 2008 R2
Can SQL Server Express Edition be used in replication?
Let’s suppose a typical replication case. Publisher, Distributor and Subscriber are the main elements as shown below.

• SQL Server Express Edition can act a subscriber to a replication process, means that it can receive data in replication.
• SQL Server Express Edition can’t act a publisher/Distributor to a replication process, means that it can’t deliver data in replication.
• So a non Express Edition of SQL Server should be used to act as publisher and in such case Express Edition SQL Server can get data as subscriber.

Software Architecture – C# – 4.0 – New Feature – Memory Mapped File

August 15, 2010

While exploring new features of C# 4.0 , i came across with concept of memory mapped file.

According Wiki , memory mapped file can be defined as:

A memory-mapped file is a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource is typically a file that is physically present on-disk, but can also be a device, shared memory object, or other resource that the operating system can reference through a file descriptor.

In C# 4.0, Microsoft has introduced a new namespace “System.IO.MemoryMappedFiles” for manipulating memory mapped file.

CreateViewStream method of MemoryMappedFiles is used to create a stream which is mapped to memory mapped file.

Data can be written to memory mapped file as:

MemoryMappedFile mmFile = MemoryMappedFile.CreateNew("testdata.data" ,int.MaxValue,MemoryMappedFileAccess.ReadWriteExecute);
var wStream = mmFile.CreateViewStream(0,3000);
BinaryWriter writer = new BinaryWriter(wStream);
writer.Write("Very Very Very Very Very huge volume of data ..............");
Console.WriteLine("Data has been written to Memory Mapped file");

Data can be read from memory mapped file as:

var rStream = mmFile.CreateViewStream(0, 3000);
BinaryReader reader = new BinaryReader(rStream);            
Console.WriteLine("Data read from Memory Mapped file as: " + reader.ReadString());

Enjoy Coding….

Software Architecture – C# 4.0 – New Feature – Synchronized Data Structures – Thread Safe Queue

August 15, 2010

In c# 4.0, Microsoft has introduced a new namespace with name “System.Collections.Concurrent” which provide set of collections having thread safe behavior.

Some of these thread safe collections are as:

ConcurrentDictionary<TKey, TValue>

 Lets take example of example of Queue data structure, Simple queue is explained here


The generic version of queue is explained here.


Before arrival of C# 4.0, thread safety in queue was ensured by using Queue.Synchronized as explained at:


But now we have better implementation of thread safe queue with System.Collections.Concurrent namespace, as explained here:


Software Architecture – C# – 4.0 – New Feature – Tuple

August 15, 2010

Tuple is a generic type that provide an option to group together different type of object in one piece.
According to MSDN:

System.Tuple is a fixed-size collection of heterogeneously typed data. Like an array, a tuple has a fixed size that can’t be changed once it has been created. Unlike an array, each element in a tuple may be a different type, and a tuple is able to guarantee strong typing for each element.

1. Tuple can be created by using constructor as

//Method for 1 for creating tuple
Tuple<int, string> myTuple = new Tuple<int, string>(1, "Shahzad");
Tuple<int, string, double, string> myTuple2 = new Tuple<int, string, double, string>(1,"Shahzad",33,"Lahore");
//Printing the items
Console.WriteLine("myTuple.Item1:" + myTuple.Item1); // Output : 1
Console.WriteLine("myTuple.Item2:" + myTuple.Item2); // Output : Shahzad

2. Tuple can also be created by using static method create of System.Tuple as:

// Method for 2 for Creating Tuple
var myTuple3 = Tuple.Create(1, "Shahzad");
var myTuple4 = Tuple.Create(1, "Shahzad", 33, "Lahore");
//Printing the items
Console.WriteLine("myTuple.Item1:" + myTuple3.Item1);// Output : 1
Console.WriteLine("myTuple.Item2:" + myTuple3.Item2);// Output : Shahzad

Nested Tuple:
Nested Tuple allow us to create a hierarchy of item by creating a tuple as item in other tuples as:

 // Nested Tuples
 Tuple<double, double, double, double> myRestTuple2 = new Tuple<double, double, double, double>(2.1, 2.4, 2.5, 2.7);
Tuple<double, double, double, double> myRestTuple1 = new Tuple<double, double, double, double>(3.1, 3.4, 3.5, 3.7);
Tuple<int, Tuple<double, double, double, double>, Tuple<double, double, double, double>> myTuple5 = new Tuple<int, Tuple<double, double, double, double>, Tuple<double, double, double, double>>(1, myRestTuple1, myRestTuple2);

// Printing the items
Console.WriteLine("Print First Item:" + myTuple5.Item1); // Output : 1
Console.WriteLine("Print First Item of second item(tuple):" + myTuple5.Item2.Item1); // Output : 3.1
Console.WriteLine("Print Second Item of second item(tuple):" + myTuple5.Item2.Item2);// Output : 3.4
Console.WriteLine("Print Third Item of second item(tuple):" + myTuple5.Item2.Item3);
// Output : 3.5
Console.WriteLine("Print fourth Item of second item(tuple):" + myTuple5.Item2.Item4);// Output : 3.7

There are only 8 items allowed in a tuple by default .If you to add more items, nested tuple can be used as:

// Nested Tuples for having items more then 8
Tuple<int, int, int, int> myRestTuple = new Tuple<int, int, int, int>(8, 9, 10, 11);
Tuple<int, int, int, int, int, int, int, Tuple<int, int, int, int>> myTuple6 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int, int>>(1, 2, 3, 4, 5, 6, 7, myRestTuple);
 Console.WriteLine("First item : "+ myTuple6.Item1);  // Outpt : 1
 Console.WriteLine("Second item : " + myTuple6.Item2);// Outpt : 2
 Console.WriteLine("Third item : " + myTuple6.Item3);// Outpt : 3
 Console.WriteLine("Forth item : " + myTuple6.Item4);// Outpt : 4
 Console.WriteLine("Fifth item : " + myTuple6.Item5);// Outpt : 5
 Console.WriteLine("Sixth item : " + myTuple6.Item6);// Outpt : 6
 Console.WriteLine("Seventh item : " + myTuple6.Item7);// Outpt : 7
 Console.WriteLine("Eightth item : " + myTuple6.Rest.Item1);// Outpt : 8
 Console.WriteLine("Nineth item : " + myTuple6.Rest.Item2);// Outpt : 9
 Console.WriteLine("Tenth item : " + myTuple6.Rest.Item3);// Outpt : 10
 Console.WriteLine("Eleventh item : " + myTuple6.Rest.Item4);// Outpt : 11

Tuples vs Anonymous Type
Anonymous type can’t be passed as return type from a method, however Tuples can be passed as input parameters and return type.
Items of Tuples can’t be identified with name, they are just called item1,item2,item3…itemn. However items of anonymous type can be named.
With introduction of Tuples, C# has provided one more feature of functional programming languages.
Enjoy Coding…

Software Architecture -C#-4.0 – New Feature – Why/What/How of Managed Extensibility Framework -MEF , Sample Calculator Code

August 14, 2010

According to MSDN:

The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.

I have copied few definitions from MSDN as:

Composable part (or, simply, part)—A part provides services to other parts and consumes services provided by other parts. Parts in MEF can come from anywhere, from within the application or externally; from an MEF perspective, it makes no difference.
Export—An export is a service that a part provides. When a part provides an export, it is said that the part exports it. For example, a part may export a logger, or in the case of Visual Studio, an editor extension. Parts can provide multiple exports, though most parts provide a single export.
Import—An import is a service that a part consumes. When a part consumes an import, the part imports it. Parts can import single services, such as the logger, or import multiple services, such as an editor extension.
Contracts—A contract is an identifier for an export or an import. An exporter specifies a string contract that it provides, and an importer specifies the contract that it needs. MEF derives contract names from the types that are being exported and imported, so in most cases you don’t have to think about it.
Composition—Parts are composed by MEF, which instantiates them and then matches up exporters to importers.

Sample :
Download sample code from here Rename file as mefdemoappsol.zip and extract the code
In this example, we will implement different methods of calculator via Managed Extensibility Framework-MEF.
1. Create a C# Console application with name “MEFDemoApp” with solution name as “MEFDemoAppSol”.
2. Add a C# Library project to solution with name “MEFAppLib”.
3. Add reference to “System.ComponentModel.Composition.dll” in both projects.
4. Add reference of console application (MEFDemoApp) project in Library project(MEFAppLib).
5. Change Build Output path of C# library project in project properties to “..\\MEFDemoApp\\bin\\Debug”, so that dll created for Library project is copied to console project.
6. Create an interface for calculation in console project as:

 public interface ICalRule 
             int GetResults(int First, int Second);                        

7. In C# library, add two implementations for above interface is shown as:

    public class CalRule1 : ICalRule
        public int GetResults(int First, int Second)
            return First + Second;
    public class CalRule2 : ICalRule
        public int GetResults(int First, int Second)
            return First - Second;

Note here that Export attribute is used to show that this is a service that is provided to others.
8. In console application, import is used as:

        public  ICalRule[] CalRules { get; set; }

9. Composition process is created and extensions are loaded from library project as:

// Compose 
DirectoryCatalog catalog = new DirectoryCatalog(Environment.CurrentDirectory);
CompositionContainer container = new CompositionContainer(catalog);

10. Testing operations on methods avaliable via MEFcan be done as:

    public void ShowResults()
        foreach (var d in CalRules)
            int ans = d.GetResults(3, 6);
          Console.WriteLine(string.Format("Rule {0}: {1}", d.GetType(), ans));

11. Collectively, the console side of application will look like as:

class Program
        public  ICalRule[] CalRules { get; set; }
        private Program()
            // Compose 
            DirectoryCatalog catalog = new DirectoryCatalog(Environment.CurrentDirectory);
            CompositionContainer container = new CompositionContainer(catalog);
        public void ShowResults()
            foreach (var d in CalRules)
                int ans = d.GetResults(3, 6);
                Console.WriteLine(string.Format("Rule {0}: {1}", d.GetType(), ans));
        static void Main(string[] args)
            Program pro = new Program();

12. On running the application, results will l.ook like as:

Rule MEFAppLib.CalRule1: 9
Rule MEFAppLib.CalRule2: -3

In the above example, we have created two extensibility examples by implementing interfaces using import and export attributes.

The sample in this post is simplified version of calculator discussed by MSDN Team at:
MSDN Team have provided complex cases, but to make things simple I have provided a new version of calculator via MEF, hope you will like it.

Software Architecture – C# 4.0 -New Feature – Lazy initialization

August 14, 2010

“Lazy initialization” is new concept in introduced in C# 4.0

If you have worked in OR Mapping technologies like linq 2 sql or nhibernate then you must be aware of concept called ”Lazy loading” which suggest that a object/collection as property in a object is not created till it is accessed in the code.

Similar is true for “Lazy initialization” which is provided with Lazy class at System.Lazy(Of T).
It proposes that an object is not created till it is used by the first time the Lazy(Of T).Value property is accessed or the Lazy(Of T).ToString method is called.

According to MSDN, “Lazy initialization” is basically used to:

Defer the creation of a large or resource-intensive object or the execution of a resource-intensive task, particularly when such creation or execution might not occur during the lifetime of the program.

Let see an example.

Suppose there is an object doing very heavy processing  going on in its constructor.

class HeavyWeightObject

        public  HeavyWeightObject()
Console.WriteLine("Constructor for HeavyWeightObject.. Doing a lot of processing..");

Let create and instance of this object as:

HeavyWeightObject hWO = new HeavyWeightObject();

You will notice that when the above line executes at the same time constructor for
HeavyWeightObject is called. So output will be as:

Constructor for HeavyWeightObject.. Doing a lot of processing..

That is normal behavior.
Lets try a twist in code as:

      Console.WriteLine("Lets analyse Lazy initilization .....");
      Console.WriteLine("Before Lazy  is created ..");
 Lazy<HeavyWeightObject> lHWO = new Lazy<HeavyWeightObject>( );
 Console.WriteLine("After Lazy  is created ..");
 Console.WriteLine("Acess Object " + lHWO.Value);

The output will look like as:

Notice that when line 3 executes, then constructor is not called, because of lazy initialization.
After that line no 4 executes, by printing the “After Lazy is created ..”
When line no 5 executes, .value property is accessed at this point, so is constructor called, this is because of lazy initialization.

Hope things are clear.

Enjoying coding in C# 4.0