Pages

Tuesday, March 19, 2013

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

1. Open IIS
2. Create a new Application pool
3. Right click the newly added Application pool > go to Advance Setting
4. Set Enable 32-Bit Applications to True

5. Now set this newly added application pool to your application.
You are done.

Wednesday, March 13, 2013

Action Delegate implementation for publisher and subscriber with and without parameters


I usually used to declare a delegate and event as separate declarations to build a publisher and subscriber mechanism. I found an alternate mechanism to accomplish the same
The concept that came to my help was Action Delegate.

  I followed the steps below
1.  Create 2 classes publisher and Subscriber
2.  Declare the Action Delegate in the Publisher class
3.  Hook the action Delegate to the event handler in Subscriber
4.  Raise the event in any of the publisher functions

The example below demonstrates the use of Action delegate with no parameters and Parameterised Action delegates

class Publisher
    {

        /// <summary>
        /// Action delegate without parameter
        /// </summary>
        public Action Completed;

        /// <summary>
        /// Parameterised Action Delegate
        /// </summary>
        public Action<int, string, string> ParameterizedCompleted;

        public void PublishParameterisedAction()
        {

            string str1 = "Hello";
            string str2 = "World";
            StringBuilder builder = new StringBuilder();
            builder.Append(str1);
            builder.Append(str2);
            int length = builder.Length;

            //Raise an event with parameters
            ParameterizedCompleted(length, str1, str2);
        }
        public void PublishAction()
        {
            string str1 = "Hello";
            string str2 = "World";
            StringBuilder builder = new StringBuilder();
            builder.Append(str1);
            builder.Append(str2);
            int length = builder.Length;

            //Raise an event without parameters
           Completed();
        }
    class Subscriber
    {
        

        static void Main(string[] args)
        {
            Publisher publisher = new Publisher();
            publisher.Completed += new Action(handleCompleted);
            publisher.ParameterizedCompleted += new Action<int, string, string>(handleTemplateCompleted);
            publisher.PublishAction ();
            publisher.PublishParameterisedAction ();

            Console.ReadLine();
        }

       static  void handleTemplateCompleted(int length,string str1 ,string  str2)
        {
            Console.Write("Result from TemplateCompletedAction" + length + str1 + str2);
        }

      static  void handleCompleted()
       {

           Console.WriteLine("notification recieved");
       }

      
    }

Difference between Throw Vs Throw ex


Most of us use exception handling very frequently .we generally use try catch finally for handling exceptions.We are often mislead due to bad rethrow of exceptions.

The following snippet shows a bad rethrow
using System;

namespace ExceptionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                callerFunc();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();

        }

        static void  callerFunc( )
        {

            try
            {
                calleeFunc();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static void calleeFunc()
        {

            try
            {
                int i = 1;
                int j = 0;
                int k = i / j;

            }
            catch (Exception ex)
            {

                throw ex ;
            }


        }

    }

The result of this code is shown below


The information here is totally misleading as it only gives a partial idea of the total stacktrace of the program. The stacktrace is not preserved.

The Solution is to never use Throw ex, instead use only Throw. The following code preserves the stacktrace of the code.

using System;

namespace ExceptionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                callerFunc();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();

        }

        static void  callerFunc( )
        {

            try
            {
                calleeFunc();

            }
            catch (Exception ex)
            {
                throw;
            }
        }

        static void calleeFunc()
        {

            try
            {
                int i = 1;
                int j = 0;
                int k = i / j;

            }
            catch (Exception ex)
            {
                throw ;
            }


        }

    }
}


The output of this code preserves the stackTrace totally to get the exact location of the exception.

implementation of Factory Method in C#

Factory Method
Factory Method is a widely used mechanism for creating instances of classes in any object oriented programming language. The Factory Method abstracts the Creation of objects from the consumer.It also provides a single place where objects can be created.I supplies the desired objects hiding the complexity of creation for the consumers
Advantages of Factory Method
1. Hides the Complexity of Creation from the Consumer.

2. Ensures The object creation logic to be in single place
3. Helps customise creation without disturbing the Consumer logic4.brings in logical Seperation between creation and usage
 
I have used a simple CarFactory example to demonstrate the same .The Factory manufactures swift cars of 2 types basic and Featured with 3 different colors black,blue and Red.

Steps for implementing the Factory Method

1.Create an abstract class Swift Car with an attribute color and a method CaliculatePrice as the price differs for different models
 
















 
2. Create 2 derived classes For Basic Swift Car and Featured Swift Car

































 
3. Define Enumerations which describe the Car types and Car Colors
















 
4. Create a Static Class with a Static Method which Returns the Car of Desired Type
Note:
it is very important to keep the abstract base class Swift car as a Return Type.


















5. Design a Client to Consume the Car from the Swift Car Factory.




Finally it can be observed that creation logic is unknown to the client and Since the factory takes care of the creation .the Factory logic can be further customised or changed easily.