Posts

Threading in .NET

Introduction and Concepts C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads. A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. Here’s a simple example and its output: All examples assume the following namespaces are imported: using System; using System.Threading; class ThreadTest { static void Main() { Thread t = new Thread (WriteY); // Kick off a new thread t.Start(); // running WriteY() // Simultaneously, do something on the main thread. for (int i = 0; i } static void WriteY() { for (int i = 0; i } } xxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyy yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxx...
Image
  My dear Sachin... I love u :-)

C# Cloning

Introduction "Introduction Cloning C# objects is one of those things that appears easy but is actually quite complicated with many 'gotchas.' This articl..." Cloning C# objects is one of those things that appears easy but is actually quite complicated with many "gotchas." This article describes the most common ways to clone a C# object. Shallow vs. Deep Cloning There are two types of object cloning: shallow and deep. A shallow clone copies the references but not the referenced objects. A deep clone copies the referenced objects as well. Hence, a reference in the original object and the same reference in a shallow-cloned object both point to the same object. Whereas a deep-cloned object contains a copy of everything directly or indirectly referenced by the object. ICloneable Interface The ICloneable interface contains a single Clone method, which is used to create a copy of the current object. public interface ICloneable { object Clone(); } The problem wi...

The Great Rhino Mocks

What is Rhino Mocks? Rhino Mocks allows you to easily create mock objects and setup a wide range of expectations on them using strongly typed notation instead of compiler-opaque strings. It's as simple as: IProjectView projectView = mocks.StrictMock (); Expect.Call(projectView.Title).Return("Project's Title"); Definitions: Mock Object - an object that pretends to be another object and allows you to set expectations on its interactions with another object. Interaction Based Testing - you specify a certain sequence of interactions between objects, initiate an action, and then verify that the sequence of interactions happened as you specified it. State Based Testing - you initiate an action and then check for the expected results (return value, property, created object, etc). Expectation - general name for validation that a particular method call is the expected one. Record & Replay model - a model that allows for recording actions on a mock object and then replay...

Windows Communication Foundation (WCF) - Instance Context Mode

Introduction This article illustrates the use of the InstanceContextMode property used in the ServiceBehaviour attribute and how it can be so handy in the latest communication technology Windows Communication Foundation (WCF), introduced by Microsoft as a part of the .NET framework SDK 3.0 and later versions. I will be creating a demo application which will make use of the various instance context modes available in WCF, along with the explanation. Why WCF? I thought of providing a little information about why Windows Communication Foundation (WCF) is required in the .NET framework when there are already communication technologies like web services, enterprise services and remoting. WCF is introduced by Microsoft to achieve a Single Unified API for.NET services, so that in the future if any one speaks about service communication in .NET it would be only Windows Communication Foundation (WCF). Even though web service has proved to be stable it has a lot of limitations like statelessness...

Interview Questions - Part 2

When to use Interface? Use an interface when: There are a group of related methods that may be called. A class only needs one implementation of the method. The class using the interface will want to cast that interface to other interface or class types. The method being implemented is linked to the type or identity of the class: for example, comparison methods. When to use delegates? Use a delegate when: An eventing design pattern is used. It is desirable to encapsulate a static method. The caller has no need access other properties, methods, or interfaces on the object implementing the method. Easy composition is desired. A class may need more than one implementation of the method. Service contracts? Data Contracts? What keyword shadowing? What is keyword yield? What are different kinds of Cursors? What are different kinds of triggers? What is var? What is the difference between memory table v/s temp tables? Why u have not used cursors in ur project? When u ll go for win forms and web...

Message Headers for WCF Request

Often do we want to pass some data to some or maybe all our service operations. This data is usually context data such as user tokens (user information), or environmental preferences of the user or machine (in Respond terminology: UserConext object). In these kind of situations, we would rather not add additional context parameters to the contracts or our services, because we don’t want to involve implementation data / context data with the business parameters of our services. A nice and easy way to pass that data is to use MessageHeaders. In order to do this we follow these steps: Add the context data to the outgoing message headers. Call the operation (Nothing special here). Extract the data from the incoming message headers. Using an OperationContextScope In order to add the message header, you should have a OperationContext for your call. An OperationContext will automaticly be created for the call, and will be available in the service side. If one wishes to add the message he...