Thursday, December 6, 2012

Method overloading in ASMX service

·         Method overloading (polymorphism) is not supported by default

·         But can be achieved using MessageName property of WebMethod attribute.

·         Reference:  http://forums.asp.net/t/1162934.aspx/1

 

[WebService(Namespace = "http://tempuri.org/")]

//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[WebServiceBinding(ConformsTo = WsiProfiles.None)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService

{

    public Service () {

 

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

 

    [WebMethod]

    public string HelloWorld()

    {

        return "Hello World";

    }

 

    [WebMethod(MessageName = "HelloWorldwithParam")]

    public string HelloWorld(string message)

    {

        return message;

    }

   

}

 

 




DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.

Wednesday, November 28, 2012

Self join: Employee - Manager


ID   Name        ManagerID

1 Balaji NULL
2 Ashish 1
3 Divya 1
4 Nidhish 2


Write a query to display ManagerName (ManagerID is foreign key to ID)/



SELECT     e.ID, e.Name, mgr.Name AS ManagerName
FROM         Employee AS e LEFT OUTER JOIN
                      Employee AS mgr ON e.ManagerID = mgr.ID

Result:


1 Balaji NULL
2 Ashish Balaji
3 Divya Balaji
4 Nidhish Ashish


SQL Sample question





1.       Find out Name of customer who has maximum number of product?
2.       Find out Name of customer who does not have product.

/*find customer who has maximum product*/
select top 1 c.Name , COUNT(c.ID) as CustomerCount from Customer c left outer join CustomerProduct cp
      on c.ID = cp.CustomerID
            group by c.ID ,c.Name
            order by CustomerCount desc



/*find all customers who does not have any product*/
select c.Name  from Customer c left outer join CustomerProduct cp
      on c.ID = cp.CustomerID
     
            where cp.CustomerID is null

Thursday, October 25, 2012

Strategy Pattern


A manufacturing company produces different products, and they sell it with help of distributors. Manufacturing company and distributor makes agreement like 10% rebate for 1000 products. Rebate claim by distributor can be send to company in the form of files (txt, excel1, excel2, excel3). And claim management system processes these files. 


txt         - process directly
excel1 - 4 columns template
excel2 - 8 columns template
excel3 - 9 columns template

Which GoF pattern is ideal in this scenario?

Solution: Strategy pattern

  • Strategy is a behavioral pattern
  • algorithms can be selected at runtime
  • defines a family of algorithms, encapsulates each one, make them interchangable

Reference: 
http://www.dofactory.com/Patterns/PatternStrategy.aspx
http://en.wikipedia.org/wiki/Strategy_pattern




Friday, October 5, 2012

Publisher - Subscriber (optimized)


  • Publisher-Subscriber is a messaging pattern. 
  • Publisher is the sender of the message. (PUSH)
  • Publisher does not have to know about the subscribers.
  • Subscribers has the control to subscribe/unsubscribe message

Reference: http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern 

Scenario: There is a publisher and many subscribers (like Printer, Fax, Web server etc). Whenever Publisher gets file, it should notify the subscribers. And subscriber should have the authority to subscribe/not subscribe. How will you design this approach?



class Program
    {
        static void Main(string[] args)
        {
            //create publisher
            Publisher p = new Publisher();

            //Create subscribers
            Print pr = new Print(p);
            Fax f = new Fax(p);
            Broadcast b = new Broadcast(p);

            //subscribe
            pr.Subscribe();
            b.Subscribe();

            //message
            Console.WriteLine("Message: ");
            string s = Console.ReadLine();

            //push message to subscribers
            p.Publish(s);

            Console.Read();
        }
    }

    class Publisher
    {
        public delegate void NotifyDelegate(string message);
        public event NotifyDelegate NotifyMessageToSubscribers;

        public void Publish(string message)
        {
            NotifyMessageToSubscribers(message);
        }
    }

    class Print
    {
        Publisher _publisher;

        public Print(Publisher p)
        {
            _publisher = p;
        }

        public void Subscribe()
        {
            _publisher.NotifyMessageToSubscribers += doPrint;
        }

        public void doPrint(string message)
        {
            Console.WriteLine("Printing: " + message);
        }
    }

    class Fax
    {
        Publisher _publisher;

        public Fax(Publisher p)
        {
            _publisher = p;
        }

        public void Subscribe()
        {
            _publisher.NotifyMessageToSubscribers += doFax;
        }

        public void doFax(string message)
        {
            Console.WriteLine("Faxing: " + message);
        }
    }

    class Broadcast
    {
        Publisher _publisher;

        public Broadcast(Publisher p)
        {
            _publisher = p;
        }

        public void Subscribe()
        {
            _publisher.NotifyMessageToSubscribers += doBroadcast;
        }

        public void doBroadcast(string message)
        {
            Console.WriteLine("Broadcasting: " + message);
        }
    }


Thursday, October 4, 2012

is a and has a relationship




House is a building (inheritance)

House has bathroom (composition)

Publisher - Subscriber

Scenario: There is a publisher and many subscribers (like Printer, Fax, Web server etc). Whenever Publisher gets file, it should notify the subscribers. And subscriber should have the authority to subscribe/not subscribe. How will you design this approach?

Solution:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PublisherSubscriber
{
    class Program
    {
        static void Main(string[] args)
        {
            Publisher p = new Publisher();

            Console.WriteLine("Message: ");
            string s = Console.ReadLine();
            p.Publish(s);

            Console.Read();           
        }
    }

    class Publisher
    {
        public delegate void NotifyDelegate(string message);
        public event NotifyDelegate NotifyMessageToSubscribers;

        Print p ;
        Fax f ;
        Broadcast b ;

        public Publisher()
        {
             p = new Print(this);
             f = new Fax(this);
             b = new Broadcast(this);
        }

       
        public void Publish(string message)
        {
            NotifyMessageToSubscribers(message);
        }

    }

    class Print
    {
        Publisher _publisher;

        public Print(Publisher p)
        {
            _publisher = p;

            //Subscribe to publisher event
            Subscribe();
        }

        public void Subscribe()
        {
            _publisher.NotifyMessageToSubscribers += doPrint;
        }
       
        public void doPrint(string message)
        {
            Console.WriteLine("Printing: " + message);
        }
    }

    class Fax
    {
        Publisher _publisher;

        public Fax(Publisher p)
        {
            _publisher = p;

            //Subscribe to publisher event
            Subscribe();
        }

        public void Subscribe()
        {
            _publisher.NotifyMessageToSubscribers += doFax;
        }

        public void doFax(string message)
        {
            Console.WriteLine("Faxing: " + message);
        }
    }

    class Broadcast
    {
        Publisher _publisher;

        public Broadcast(Publisher p)
        {
            _publisher = p;

            //Subscribe to publisher event
            Subscribe();
           
        }

        public void Subscribe()
        {
            //Publisher.NotifyMessageToSubscribers += doBroadcast;
        }

        public void doBroadcast(string message)
        {
            Console.WriteLine("Broadcasting: " + message);
        }
    }
}



Saturday, September 22, 2012

WCF


1. FaultContract
2. MessageContract
3. Duplex
4. Hosting
5. Transactions
6. Session management
7. Polymorphism
8. tcpbinding





  1. FaultContract
    • FaultContract helps to do Exception handling in WCF service
    • convert .NET exceptions into Fault
    • Service client does not have to be built by using .NET framework
    • SOAP protocol provides a standard mechanism to report faults within SOAP message.
    • So client can recognize it as fault from the SOAP message

    • Types of error:
      • Domain error: (eg: NoFunds)
      • Technical error:  (Expected and                 unexpected technical errors(eg: divide by zero))
    • These errors should be mapped to one of the defined faults.
    • Structure of SOAP fault is an XML representation of error information.
    • SOAP fault contains
§ Code
§ Reason
§ Detail


    • Using Fault in service



    • Typed and Untyped fault


    • Handling service Exceptions at client


  1. MessageContract
    1. WCF allow you to control SOAP message structure with MessageContract
    2. You can modify message by

·   Define custom header
·   Adding elements to SOAP body
·   Altering namespace in SOAP body

  1. Message exchange patterns
    1. Request response
    2. Simplex (one way)
    3. Duplex

·         Request  response
o  default message exchange pattern in WCF
o Synchronous remote procedure call(RPC)

·         One way
o Client send message to service, but service does not send message back
o Return type of one-way operation must be void
o Drawback- no way to detect operation failure
o Advantage – performance – as client does not have to wait for response

[OperationContract(IsOneWay = true)]
void MakeDeposit(string account, decimal amount);



·         Duplex
o This pattern consists of 2 separate contracts
o One contract implemented by service, other by client
o 2 one-way messages


  1. Polymorphism
·         Operation overloading - Operation with same name, but different parameters
·         Use Name property in OperationContract.

[OperationContract(Name="DepositLocalCurrency")]
void Deposit(string account, decimal amount);

[OperationContract(Name="DepositAnyCurrency")]
void Deposit(string account, Currency amount);




  1. Transactions
@server:
    1. Select a transaction aware binding
o NetTcpBinding
o NetNamedPipeBinding
o wsHttpBinding
o wsDualHttpBinding
o wsFederationHttpBinding

    1. Enable TransactionFlow property of binding in configuration file.
 
   
         transactionProtocol="WSAtomicTransactionOctober2004"/>
 
    1. Transactional requirement of operation




@client
1.       Enable transaction flow on binding in configuration file.
2.       Use TransactionScope block at client code


Wednesday, September 12, 2012

ASP.NET MVC FAQ


1. Can you have viewstate in ASP.NET MVC?

  • Ans: No

2. Can you do unit testing of view in ASP.NET MVC?

  • Ans: Yes

3. Is routing happens every time a page request or only first time?

  • Ans: - Only first time
  • - Shared Sub RegisterRoutes(ByVal routes As RouteCollection)



http://jinaldesai.net/asp-net-mvc-3-interview-questions/

Tuesday, September 11, 2012

Datastructures in .NET


a.       Stack:
·   FILO (first in last out)
·   Push(object o) – insert an object at top of stack
·   POP() – removes object at top of stack
·   Peek() – returns object at top of stack without removing
·   One of the classic examples of stack in .net framework is in memory management of value types.
·   There are non generic collection stack object (System.Collections.Stack) and generic collection stack object (System.Collections.Generic.Stack)



b.      Queue:
·   FIFO
·   Enqueue(object o) – add an object at end of queue
·   Dequeue() – remove object at beginning of queue
·   Peek() - return object at beginning of queue
·   First object entering to the Queue, exits first
·   One of the classic example of queue implementation in MSMQ
·   Queue is used in the highly reliable communication scenarios. Even if the message channel not available, messages are stored in the queue and transmitted once the channel is up.
·   There are non generic collection object (System.Collections.Queue) and generic collection object (System.Collections.Generic. Queue)


c.       Hashtable           
·   Hashtable stores key – value pair
·   Is organized based on the hash code of the key
·   Each element in hashtable is stored as DictionaryEntry object
·   A key cannot be null, but value can be
·   It is a non generic collection System.Collections type
·   Hashtable stores types of object
·   There is a type conversion required every time when we use hashtable

IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable

d.      Dictionary
·   Dictionary is similar to Hashtable (key-value pair storage)
·   It is a generic collection (System.Collections.Generics)
·   Type of the object to store in the dictionary should specify at compile time
·   Performance wise,  Dictionary is better option that Hashtable
IDictionary, ICollection<KeyValuePair>, IEnumerable<KeyValuePair>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

e.      List
·   It is generics collection (System.Collections. Generics)
·   It represents strongly typed list of objects
·   Consider a scenario to store all the employee names of Satyam, then use List
·   No type conversion required hence preferred over ArrayList

IList, ICollection, IEnumerable, IList, ICollection, IEnumerable

f.        ArrayList
·   ArrayList is a non generic collection (System.Collections)
·   Size can be dynamically increase
·   ArrayList stores objects
·   Type conversion required every time when use AL
·  This affects the performance