5 May 2013

Inheritance, Association, Aggregation, and Composition


Inheritance, Association, Aggregation, and Composition
      1.  Inheritance [IS a relationship]
      2.  Association [Using Relationship]
      3. Aggregation [ Has a Relationship]
      4. Composition -  [Part of relationship]

1.      Inheritance [ IS a relationship]
Example
Consider there are two Classes.
1.Employee
2.Manager 
Here Employee is the Parent class; Manager is the child class.Manager is one of the types of Employee. Class Manager is a relation with Class Employee.
I.e. Manager is (a or an) Employee

2. Association [Using relationship]
Example
       1. Manager
       2. SwipeCard
Here Manager and SwipeCard are two different classes.
Manager uses a swipe card to enter XYZ premises.

I.e. Manager Using swipe card to enter XYZ premises.



The above diagram shows how the SwipeCard class uses the Manager class and the Manager class uses the SwipeCard class. You can also see how we can create objects of the Manager class and SwipeCard class independently and they can have their own object life time.
This relationship is called the “Association” relationship.

3.      Aggregation [ Has a Relationship]

Aggregation gives us a 'has-a' relationship. Within aggregation, the lifetime of the part is not managed by the whole. 
The above UML describes Aggregation relation which mentions that Employee refers to Address. And life time of Address is not managed by Employee. It is like"Employee has a Address". Let us see how it can be implemented in C#.



So how do we express the concept of aggregation in C#? Well, it's a little different to composition. Consider the following code:
public class Address
{
 . . .
}
public class Person
{
     private Address address;
     public Person(Address address)
     {
         this.address = address;
     }
     . . .
}
Person would then be used as follows:
Address address = new Address ();
Person person = new Person(address);
    Or
Person person = new Person (new Address() );

4. Composition  -  [Part of relationship]
Composition gives us a 'part-of' relationship.

If we were going to model a car, it would make sense to say that an engine is part-of a car. Within composition, the lifetime of the part (Engine) is managed by the whole (Car), in other words, when Car is destroyed, Engine is destroyed along with it. So how do we express this in C#?
public class Engine
{
 . . .
 
}
public class Car
{
    Engine e = new Engine();
    .......
}
As you can see in the example code above, Car manages the lifetime of Engine.




4 May 2013

Return multiple Values in C#

Return multiple Values in C#


We can use formal parameters modified by the out keyword. Alternatively, we can allocate a KeyValuePair instance to store the result values.

Tips:
Instead of KeyValuePair you could use a custom class or the Tuple type from the .NET Framework 4.0.

Tuple


Coding

using System;
using System.Collections.Generic;

class Program
{
    static void GetTwoNumbers(out int number1, out int number2)
    {
number1 = (int)Math.Pow(2, 2);
number2 = (int)Math.Pow(3, 2);
    }

    static KeyValuePair GetTwoNumbers()
    {
return new KeyValuePair((int)Math.Pow(2, 2), (int)Math.Pow(3, 2));
    }

    static void Main()
    {
// Use out parameters for multiple return values.
int value1;
int value2;
GetTwoNumbers(out value1, out value2);
Console.WriteLine(value1);
Console.WriteLine(value2);

// Use struct for multiple return values.
var pair = GetTwoNumbers();
Console.WriteLine(pair.Key);
Console.WriteLine(pair.Value);
    }
}

Output

4
9
4
9

Return multiple Values in C# using Tuple
public Tuple GetMultipleValue()
{

     return new Tuple(1,2);

}



Other methods for returning multiple Values in C#
Rather than this you can also use object or Collections to return multiple values




Why C# or VB.NET doesn't support multiple inheritance


Why C# or VB.NET doesn't support multiple inheritance

1) First reason is ambiguity around Diamond problem, consider a class A has foo() method and then B and C derived from A and has there own foo() implementation and now class D derive from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond, see below

           A foo()
           / \
          /   \
   foo() B     C foo()
          \   /
           \ /
            D
           foo()

In my opinion even if we remove the top head of diamond class A and allow multiple inheritances we will see this problem of ambiguity.

Some times if you give this reason to interviewer he asks if C++ can support multiple inheritance than why not c# oR vb.net.In that case I would try to explain him the second reason which I have given below that its not because of technical difficulty but more to maintainable and clearer design was driving factor though this can only be confirmed by any of java designer and we can just speculate. Wikipedia link has some good explanation on how different language address problem arises due to diamond problem while using multiple inheritances.

2) Second and more convincing reason to me is that multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc and given that there are not many scenario on which you need multiple inheritance its wise decision to omit it for the sake of simplicity. Also c# and avoids this ambiguity by supporting single inheritance with interfaces. Since interface only have method declaration and doesn't provide any implementation there will only be just one implementation of specific method hence there would not be any ambiguity.


3 May 2013

WCF FAQ

WCF FAQ

1. What is WCF?
Windows Communication Foundation (WCF) is a framework for building and running service-oriented applications. WCF unifies and extends the functionality of existing Microsoft connecting technologies, providing a single programming model independent of underlying communications protocols. WCF applications can inter operate with other technologies using open standards and protocols.  WCF is a combined feature of Web Service, Remoting, MSMQ and COM+. 

2.Endpoints - All communications with the WCF service will happen via the endpoints. The endpoint is composed of 3 parts (collectively called as ABC's of endpoint) as defines below:

Address: The endpoints specify an Address that defines where the endpoint is hosted. It’s basically URL.


Ex: http://localhost/WCFServiceSample/Service.svc


Binding: The endpoints also define a binding that specifies how a client will communicate with the service and the address where the endpoint is hosted. Various components of the WCF are depicted in the figure below.

"A" stands for Address: Where is the service?
"B" stands for Binding: How can we talk to the service?
"C" stands for Contract: What can the service do for us?
                     
Binding
Description
BasicHttpBinding
Basic Web service communication. No security by default
WSHttpBinding
 WsHttpBinding sends the message in an encrypted and secured manner. 
WSDualHttpBinding
Similar to the wsHttpBinding binding, with additional support for duplex communication and lack of support for transport-level security.
WSFederationHttpBinding
Web services with federated security. Supports transactions
MsmqIntegrationBinding
Communication directly with MSMQ applications. Supports transactions
NetMsmqBinding
Communication between WCF applications by using queuing. Supports transactions
NetNamedPipeBinding
Communication between WCF applications on same computer. Supports duplex contracts and transactions
NetPeerTcpBinding
Communication between computers across peer-to-peer services. Supports duplex contracts
NetTcpBinding
Communication between WCF applications across computers. Supports duplex contracts and transactions

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...