Saturday, August 4, 2012

Event Bubbling

Event Bubbling is nothing but events raised by child controls is handled by the parent control. Example: Suppose consider datagrid as parent control in which there are several child controls.There can be a column of link buttons right.Each link button has click event.Instead of writing event routine for each link button write one routine for parent which will handlde the click events of the child link button events.

protected override bool OnBubbleEvent(object source, EventArgs e) {
if (e is CommandEventArgs) {
// Adds information about an Item to the
// CommandEvent.
TemplatedListCommandEventArgs args =
new TemplatedListCommandEventArgs(this, source, (CommandEventArgs)e);
RaiseBubbleEvent(this, args);
return true;
}
return false;
}

Refer: http://msdn.microsoft.com/en-us/library/aa719644(VS.71).aspx

COM Callable Wrapper(CCW)


When a COM client calls a .NET object, the common language runtime creates the managed object and a COM callable wrapper (CCW) for the object. Unable to reference a .NET object directly, COM clients use the CCW as a proxy for the managed object.

The runtime creates exactly one CCW for a managed object, regardless of the number of COM clients requesting its services. As the following illustration shows, multiple COM clients can hold a reference to the CCW that exposes the INew interface. The CCW, in turn, holds a single reference to the managed object that implements the interface and is garbage collected. Both COM and .NET clients can make requests on the same managed object simultaneously.

Accessing .NET objects through COM callable wrapper

COM callable wrapper


COM callable wrappers are invisible to other classes running within the .NET Framework. Their primary purpose is to marshal calls between managed and unmanaged code; however, CCWs also manage the object identity and object lifetime of the managed objects they wrap.

Ref-http://msdn.microsoft.com/en-us/library/f07c8z1c.aspx

Daemon Thread

 
Daemon term is mainly used in UNIX. Normally when a thread is created in Java, by default it is a non-daemon thread. Whenever a Java Program is executed, the Java Virtual Machine (JVM) will not exit until any non-daemon threads are still running. This means if the main thread of an application ends and the remaining threads left are Daemon threads, then the JVM will exit killing all the daemon threads without warning.

A daemon thread should be used for some background task that might provide a service to the applications. e.g. a Server thread listening on a port for the clients` requests. A thread for which an exit method is not provided or which does not have an exit mechanism can be marked as a daemon thread.
 

Difference Between Web Farm and Web Garden

Web Farms and Web Garden are very common terminology for any production deployment. Though these terms looks same but the things are totally different. Many beginners very confused with these two terms. Here you can find the basic difference between the Web Farm and Web Garden.


Web Farm

After developing our asp.net web application we host it on IIS Server.  Now one standalone server is sufficient to process ASP.NET Request and response for a small web sites but when the site comes for big organization where there an millions of daily user hits then we need to host the sites on multiple Server. This is called web farms. Where single site hosted on multiple IIS Server and they are  running behind the Load Balancer.
This is the most common scenarios for any web based production environment. Where Client will hit an Virtual IP ( vIP) . Which is the IP address of Load Balancer. When Load balancer received the request based on the server load it will redirect the request to particular Server.

Web Garden
All IIS Request process by worker process ( w3wp.exe). By default each and every application pool contain single worker process. But An application pool with multiple worker process is called Web Garden.   Many worker processes with same Application Pool can sometimes provide better throughput performance and application response time. And Each Worker Process Should have there own Thread and Own Memory space.
There are some Certain Restriction to use Web Garden with your web application. If we use Session Mode to "in proc", our application will not work correctly because session will be handled by different Worker Process. For Avoid this Type of problem we should have to use Session Mode "out proc" and we can use "Session State Server" or "SQL-Server Session State".

Ref- http://www.codeproject.com/Articles/114910/What-is-the-difference-between-Web-Farm-and-Web-Ga

Monday, June 4, 2012

How to check string is palindrome or not in .NET

public bool isPalindrome(String str)
{
    if (String.IsNullOrEmpty(str))
        return false;
 
    int length = str.Length;
 
    str = str.ToLower();
 
    for (int i = 0; i < (length / 2); i++)
    {
        if (str[i] != str[length - 1 - i])
            return false;
    }
 
    return true;
}

How to generate Fibonacci series in c#

Open a console application project in c# language in your editor and just paste the following code in your class's brackets -

static void Main(string[] args)
{
     int previous = -1; int next = 1;
     int position;
    Console.WriteLine("Enter the position");
    position = int.Parse(Console.ReadLine());
   for (int i = 0; i < position; i++)
   {
        int sum = next + previous; previous = next;
        next = sum;
       Console.WriteLine(next);
   }
   Console.ReadLine();
}


Tip: One problem with the implementation in this article is that the int types will overflow past the 47th Fibonacci number. To solve this problem, you can use the double type. Change Fibonacci() to return double. Additionally, change previous , next , and position to be of type double.


Output
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377

Saturday, June 2, 2012

Indexer in C#

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.
In the following example, a generic class is defined and provided with simple get and set accessor methods as a means of assigning and retrieving values. The Program class creates an instance of this class for storing strings.



class SampleCollection<T>
{
    // Declare an array to store the data elements.
    private T[] arr = new T[100];
    // Define the indexer, which will allow client code
    // to use [] notation on the class instance itself.
    // (See line 2 of code in Main below.)       
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}
// This class shows how client code uses the indexer.
class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>();
        // Use [] notation on the type.
        stringCollection[0] = "My Name is Psingh";
        System.Console.WriteLine(stringCollection[0]);
    }
}

Output:
"My Name is Psingh"


 

Comparison Between Properties and Indexers

Indexers are similar to properties. Except for the differences shown in the following table, all of the rules defined for property accessors apply to indexer accessors as well.

PropertyIndexer
Identified by its name.Identified by its signature.
Accessed through a simple name or a member access.Accessed through an element access.
Can be a static or an instance member.Must be an instance member.
A get accessor of a property has no parameters.A get accessor of an indexer has the same formal parameter list as the indexer.
A set accessor of a property contains the implicit value parameter.A set accessor of an indexer has the same formal parameter list as the indexer, in addition to the value parameter.

IQueryable vs. IEnumerable in terms of LINQ to SQL queries

IQueryable inherited IEnumerable, so it obviously get all the functionality IEnumerable has. The working style of both is still different. There are still lot many differences exists between the two which impact the decision we took in usage of either one. Both of these suits for particular scenarios. Following are some differences using which we can took decision to use anyone of these optimally.
ParameterIQueryableIEnumerable
Extension MethodsExtension methods defined for IQueryable take expression objects. Means, the delegate IQueryable extension methods receives is an expression tree.Extension methods defined for IEnumerable take functional objects. Means, the delegate IEnumerable extension methods receives is a method to invoke.
Great ForIQueryable allows out-of-memory things like remote data source operations, such as working with database or web service.IEnumerable is great for working with sequences (in-memory collections), that are iterated in-memory.
Supported QueriesIQueryable supports LINQ to SQL queries.IEnumerable supports LINQ to Object and LINQ to XML queries.
Deferred ExecutionIQueryable supports lazy loading (Suited for scenarios like paging and composition based queries).IEnumerable lost lazy loadnig ability on the external provider.
Moving between itemsIQueryable provides many methods to move between the items.IEnumerable doesn’t have the concept of moving between items, it is forward only collection.
Filtering MechanismIQueryable executes query on server side along with all the filters applied.IEnumerable executes select query on server side, loads data in-memory and then executes filter at client side.
CreateQuery and Execute MethodsIQueryable has these two additional methods. Both takes expression as input. CreateQuery returns IQueryable representing expression tree. Execute returns result of the query.NA
Custom Querying CapabilityIQueryable provides additional functionality to implement custom querying with LINQ.IEnumerable does not have custom querying capability.
Performance PerspectiveIQueryable gives best performance when used for out-of-memory data store operations (that is external to .NET CLR memory).IEnumerable gives best performance when used to manipulate in-memory collections.


IEnumerable: IEnumerable is best suitable for working with in-memory collection. IEnumerable doesn’t move between items, it is forward only collection.
IQueryable: IQueryable best suits for remote data source, like a database or web service. IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).


So when you have to simply iterate through the in-memory collection, use IEnumerable, if you need to do any manipulation with the collection like Dataset and other data sources, use IQueryable

Thursday, May 24, 2012

How will implement Page Fragment Caching?

How will implement Page Fragment Caching?

Page fragment caching involves the caching of a fragment of the page, rather than the entire page. When portions of the page are need to be dynamically created for each user request this is best method as compared to page caching. You can wrap Web Forms user control and cache the control so that these portions of the page don’t need to be recreated each time.