Showing posts with label .Net. Show all posts
Showing posts with label .Net. Show all posts

Strategy Design Pattern Using .Net (C#)

In this article, we will understand what Strategy Pattern is and when we actually need to use it, along with a practical example and real-life use case.

This pattern falls under the category of behavioral pattern and as the name suggests, it allows clients to choose an algorithm from a set of algorithms at run time.
Strategy pattern defines a family of algorithms, encapsulates each one of them and makes them interchangeable at run time within that family.
Now let's understand what each one of them actually means.


Family of Algorithms
This means this pattern provides a set of algorithms using one of which at run time you can achieve the desired output.

Encapsulate each one of the
This pattern allows you to place your algorithms in different classes (encapsulate them).

Makes the algorithm interchangeable
The beauty with strategy pattern is that we can select at run time which algorithm we want to apply to our object and can also replace them with one another.

These are the three main points of Strategy pattern. I hope we are clear with the above- discussed points. So, let's understand one real -world scenario where it can be really helpful to use.

Example

Suppose you want to order food online and apply one of the available coup. In this case you can implement the Strategy Pattern.

As per our example, IApplyCoupon is the interface which looks something like,​

  • 1.  public interface IApplyCoupon    
  • 2.     {    
  • 3.         string applycoupon(string couponType);    
  • 4.     }   


We have 2 Concrete coupon classes which implement IApplyCoupon interface and these classes can have their own logic to calculate the time.
  1. 1.  public class FamilyHours :IApplyCoupon    
  2. 2.     {    
  3. 3.         public string applycoupon(string couponType)    
  4. 4.         {    
  5. 5.             return "You applied family hours coupon";    
  6. 6.         }    
  7. 7.     }    


  1. 1.  public class FestivalOffer :IApplyCoupon    
  2. 2.   {    
  3. 3.       public string applycoupon(string couponType)    
  4. 4.       {    
  5. 5.           return "You applied festival offer coupon";    
  6. 6.       }    
  7. 7.   }   




Lastly, we have one Context class called TravelStrategy using which a client can select any strategy at run-time.

  1. 1.  public class Coupon    
  2. 2.     {    
  3. 3.             private IApplyCoupon _applycoupon;    
  4. 4.             public Coupon(IApplyCoupon applycoupon)    
  5. 5.             {    
  6. 6.                 _applycoupon = applycoupon;    
  7. 7.             }    
  8. 8.             public void applycoupon(string couponType)    
  9. 9.             {    
  10. 10.                var result = _applycoupon.applycoupon(couponType);    
  11. 11.                Console.WriteLine(result);    
  12. 12.            }    
  13. 13.            
  14. 14.    }    

Our main program looks like:

  1. 1.  class Program    
  2. 2.     {    
  3. 3.         static void Main(string[] args)    
  4. 4.         {    
  5. 5.             Console.WriteLine("Hello!, Please select the coupon for Dinining \n1.FamilyHours \n2.FestivalHours");    
  6. 6.             var userStrategy = Console.ReadLine().ToLower();    
  7. 7.             Console.WriteLine("\nUser has selected " + userStrategy + " as dining coupon\n");    
  8. 8.             switch (userStrategy)    
  9. 9.             {    
  10. 10.                case "FamilyHours":    
  11. 11.                    new Coupon(new FamilyHours()).applycoupon("FamilyHours");    
  12. 12.                    break;    
  13. 13.                case "FestivalHours":    
  14. 14.                    new Coupon(new FestivalOffer()).applycoupon("FestivalHours");    
  15. 15.                    break;    
  16. 16.                default:    
  17. 17.                    Console.WriteLine("You didn't select any coupon");    
  18. 18.                    break;    
  19. 19.            }    
  20. 20.            Console.ReadLine();    
  21. 21.        }    
  22. 22.    }    

OUTPUT

When user selects family hours as the preferred dining coupon




Answer



 2. When user selects festival hours as the preferred dining coupon



Output




Hope you have learned something new in this article, Stay tune for next one.

Mixed Data Type in Excel

Myth around MS-Excel as Data source using .Net (C#)
Recently, I observed in the couple of projects, people use MS-Excel as data source using
1.       Interop – (Without knowing the pre-requisites in production environment i.e. MS-Office not allowed in production environment)
2.       Third party tool (free & open source) – Use unsupported.Net Framework.
This article emphasis, known issue to use MS-Excel as data source using .Net (C#).
Problem Statement
Excel column decide the datatype based on a certain number of rows i.e. 8. If we are using excel with mixed datatype i.e. alphanumeric value where First 8 as the number and 9 as a string -> which return null as below source.

Excel Behavior
This is the default behavior of excel. It decides the column datatype based on first 8 rows.
Alternative Approach
1.       Read data using  Excel –cell range
2.       Third party open source tool having supported framework i.e. 4.6 onwards.
Best Approach (Recommended)
1.       Always use CSV file instead of Excel. Easy to traverse using file stream object.
2.       Registry Changes - If business allowed.
Excel file  as .xls
(Registry Key)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows
Excel file is .xlsx
Registry Key Excel 2007:

HKEY_LOCAL_MACHINE\Software\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows

Excel file is .xlsx
Registry Key Excel 2010:

HKEY_LOCAL_MACHINE\Software\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows
Excel file is .xlsx
Registry Key Excel 2013:
HKEY_LOCAL_MACHINE\Software\Microsoft\Office\15.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows


Note: - Value should be 0 instead of 8.