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

16 April 2013

ASP.NET, SQL, ADO.NET Video Tutorial

ASP.NET, SQL, ADO.NET Video Tutorial

Friend , Below link is really useful for Beginners

ASP.NET Gridview Video Tutorial

http://www.youtube.com/playlist?list=PL6n9fhu94yhW1NryGv6LxX4U4b07T4RlI


ADO.NET Tutorial for Beginner

http://www.youtube.com/playlist?list=PL6n9fhu94yhX5dzHunAI2t4kE0kOuv4D7

ASP.NET Tutorial for Beginner

http://www.youtube.com/playlist?list=PL6n9fhu94yhXQS_p1i-HLIftB9Y7Vnxlo


SQL Tutorial for Beginner

http://www.youtube.com/playlist?list=PL08903FB7ACA1C2FB

Dotnet Tutorial for Beginner

http://www.youtube.com/playlist?list=PL8598C97BA1D871C1

C# Tutorial for Beginner

http://www.youtube.com/playlist?list=PLAC325451207E3105


Uses of WCF


Uses of WCF


Interoperability
When our application is in .Net technologies and when the partner application was in another platform [J2EE]. It is running different operating system.[Different Platform]. The security requirement is also quite different. So to Interoperability with this application WCF play a key role in interoperable with the application

Unification of Microsoft’s Distributed Computing Technologies, WCF vs. other technologies
1.     ASMX, also called ASP.NET Web Services, would be an option for communicating with the Java EE-based reservation application and with the partner applications.

 WCF  - WCF can communicate using SOAP-based Web services, interoperability with
other platforms that also support SOAP

2.     .NET Remoting - Remoting is designed expressly for .NET-to-.NET communication, so it would offer the best performance for this situation.

 WCF - To allow optimal performance when both parties in a communication are built on WCF,  the wire encoding used in this case is an optimized binary version of SOAP.

3.     Enterprise Services - managing object lifetimes and defining distributed transactions. But it supports only limited set of protocols.

WCF - Managing object lifetimes, defining distributed transactions, and other aspects of
Enterprise Services are also provided by WCF.


4.     Web Services Enhancements (WSE) - might be used along with ASMX to communicate, Because it implements more advanced SOAP-based standards, known collectively as the WS-*specifications, WSE can allow better security.
         
WCF -  it supports a large set of the WS-* specifications, WCF helps provide reliability,
security, and transactions when communicating with any platform that also supports these
Specifications.

5.     System.Messaging, which provides a programming interface to Microsoft MessageQueuing (MSMQ) could be used to communicate with Windows-based partner applications.
          
WCF – It has option for queued messaging, built on MSMQ, allows applications to use
Persistent queuing without needing to use another application programming interface.

6.     System.Net developers can create applications that use the HTTP-based communication style known as Representational State Transfer (REST).

WCF - It has built-in support for creating RESTful clients and services. 
           Rather than requiring different technologies for different communication styles, WCF provides a single unified solution.

12 April 2013

IQueryable and IEnumerable

 IQueryable and  IEnumerable

The IQueryable interface is derived from IEnumerable

IEnumerable

1. IEnumerable exists in System.Collections Namespace.
2. IEnumerable can move forward only over a collection, it can’t move backward and between the items.
3. IEnumerable is best to query data from in-memory collections like List, Array etc.
4. While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
5. IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
6. IEnumerable supports deferred execution.
7. IEnumerable doesn’t supports custom query.
8. IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
9. Extension methods supports by IEnumerable takes functional objects.

IEnumerable Example

 MyDataContext dc = new MyDataContext ();
IEnumerable< Employee > list = dc.Employees.Where(p = > p.Name.StartsWith("S"));
list = list.Take< Employee >(10); 
Generated SQL statements of above query will be :
 SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
WHERE [t0].[EmpName] LIKE @p0
Notice that in this query "top 10" is missing since IEnumerable filters records on client side



IQueryable
1. IQueryable exists in System.Linq Namespace.
2. IQueryable can move forward only over a collection, it can’t move backward and between the items.
3. IQueryable is best to query data from out-memory (like remote database, service) collections.
4. While query data from database, IQueryable execute select query on server side with all filters.
5. IQueryable is suitable for LINQ to SQL queries.
6.IQueryable supports deferred execution.
7. IQueryable supports custom query using CreateQuery and Execute methods.
8. IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
9. Extension methods supports by IEnumerable takes expression objects means expression tree.

IQueryable Example
MyDataContext dc = new MyDataContext ();
IQueryable< Employee > list = dc.Employees.Where(p => p.Name.StartsWith("S"));

list = list.Take< Employee >(10); 
Generated SQL statements of above query will be :SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]WHERE [t0].[EmpName] LIKE @p0
Notice that in this query "top 10" is exist since IQueryable executes query in SQL server with all filters.





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...