How does it work in C#?

A new article has been published in the Code Project. The article is in here.

Send ASP.NET formatted errors details via Email.

Exception/Error is common scenario in any application. In ASP.NET, there many ways to handle those errors or exception, for example, when an error or exception occurred in ASP.NET application, system is going to send formatted error message to specified developers or team mentioned in the email list. The difference will be the body of email will contain exactly same formatted error ASP.NET generated to display the error or exception details when occurred. For example,



But when an ASP.NET application goes into life, user should not experience to see the above error page where they should see custom error page which easy to understand. More information about is in here.

So in here, this test application will display default error page when there is an error occurred in the system and send an email formatted like above image. 

To do that need to add void Application_Error(object sender, EventArgs e) method into Global.asax.cs file,  the code block is,


    void Application_Error(object sender, EventArgs e)
        {
            HttpUnhandledException httpUnhandledException = new HttpUnhandledException(Server.GetLastError().Message, Server.GetLastError());
            SendEmailWithErrors(httpUnhandledException.GetHtmlErrorMessage());
        }
So GetHtmlErrorMessage() method of HttpUnhandledException class will do all the formatting stuff.
 
The code block to send email is below,
        private static void SendEmailWithErrors(string result)
        {
            try
            {
                MailMessage Message = new MailMessage();
                Message.To = "To address";
                Message.From = "FROM addressd";
                Message.Subject = "Exception raised";
                Message.BodyFormat = MailFormat.Html;
                Message.Body = result;
                SmtpMail.SmtpServer = "SMTP Sever Address";
                SmtpMail.Send(Message);
            }
            catch (System.Web.HttpException ehttp)
            {
                // Write o the event log.
            }
        }
 
To test it there is button on page which manually trigger an exception, the code for button is as below,
 
        protected void btnError_Click(object sender, EventArgs e)
        {            throw new Exception("This Exception is raised to test");
        }
 
 
So now the system will display default error page when an exception occurred and send email with formatted exception details as below,
 
Default Error page,
 

 
And an email with formatted exception details,
 



Codeproject article about extension methods of IEnumerable in C# in

I posted another article about extension methods of IEnumerable in C# in the Codeproject. It could be found in here

Few extension methods of IEnumerable

Following code shows few extension methods of IEnumerable<string>,
public static class EnumberableExtensions
{
         public static IEnumerable<string> IfMatchWith(this IEnumerable<string> myList, string itemToMatch)
        {
            foreach (var item in myList.Where(item => item == itemToMatch))
                yield return item;
        }
 
        public static IEnumerable<string> IfNotMatchWith(this IEnumerable<string> myList, string itemToMatch)
        {
            foreach (var item in myList.Where(item => item != itemToMatch))
                yield return item;
        }
 
        public static IEnumerable<string> IgnoreNullOrEmptyOrSpace(this IEnumerable<string> myList)
        {
            foreach (var item in myList.Where(item => !string.IsNullOrEmpty(item) && item != " "))
                yield return item;
        }
 
        public static IEnumerable<string> MakeAllUpper(this IEnumerable<string> myList)
        {
            foreach (var item in myList)
                yield return item.ToUpper();
        }
 
        public static IEnumerable<string> MakeAllLower(this IEnumerable<string> myList)
        {
            foreach (var item in myList)
                yield return item.ToLower();
        }
 
        public static IEnumerable<T> MakeAllDefault<T>(this IEnumerable<T> myList)
        {
            foreach (var item in myList)
                yield return default(T);
        }
 
        public static IEnumerable<string> IfMatchWithPattern(this IEnumerable<string> myList, string pattern)
        {
            foreach (var item in myList.Where(item => Regex.IsMatch(item, pattern)))
                yield return item;
        }
 
        public static IEnumerable<string> IfLengthEquals(this IEnumerable<string> myList, int itemLength)
        {
            foreach (var item in myList.Where(item => item.Length == itemLength))
                yield return item;
        }
 
        public static IEnumerable<string> IfLengthInRange(this IEnumerable<string> myList, int startOfRange, int endOfRange)
        {
            foreach (var item in myList.Where(item => item.Length >= startOfRange && item.Length <= endOfRange))
                yield return item;
        }
}

Codeproject article about Template method pattern in C#

I posted another article about Template method pattern and Action in C# in the Codeproject. It could be found in here.

Codeplex - .NET Utility Library project

I just published a project in Codeplex. The project name is DotNetUtility - a .NET Utility Library for C#. The project URL is DotNetUtility - a .NET Utility Library for C#. This is open source and if any one willing to contribute please let me know.

Code project - Caching uses with Proxy pattern in C#

I posted another article about improvement of Proxy Pattern by using Caching technique in C#. It could be found in Caching uses with Proxy pattern in C#.

return new Lazy(() => new T()); /* Lazy initialization of objects in C# */

We should try to initialize objects when we need it otherwise the objects will reside in the memory. Probably it won't matter when the object is not that expensive otherwise it really matters. So if we could use something which give us the opportunity to initialize objects only when we need it or probably Singleton pattern or something like this. In .NET 4.0, there is new feature which will do the job for us. That feature is Lazy<T>, following code block will show the use of it,
static Lazy<T> LazyLoader<T>() where T : new()
{
    return new Lazy<T>(() => new T());
}
 
static Lazy<T> LazyLoader<T>(Func<T> lazyInitialisationBlock) where T : new()
{
    return new Lazy<T>(lazyInitialisationBlock);
}
 
static List<string> InitialisationBlock()
{
    return new List<string> { "One""Two" };
}
Usage of the above code,
static void Main(string[] args)
{
   Lazy<ClassForLazyLoad> loader = LazyLoader<ClassForLazyLoad>();
   Console.WriteLine("{0}", loader.Value.Name);
   Lazy<List<string>> listLoader = LazyLoader<List<string>>(InitialisationBlock);
   Console.WriteLine("{0}", ReferenceEquals(listLoader, null) ? "Loading hasn't been finished yet" : "Loading has been finished");
   Array.ForEach(listLoader.Value.ToArray(), item => Console.WriteLine(item.ToString()));
}
related classes,
public class ClassForLazyLoad
{
   public string Name { get { return GetType().FullName; } }
}

Codeproject articles - Strategy method or Switch statement in C#

I just published another article in the code project. This article is about Strategy method which can be used to replace the switch statement in C#. That article could be found,

Strategy method or Switch statement in C#

A Generic Strategy Pattern implementation using C#

Strategy Pattern - according to this pattern, there will be an interface, few concrete classes which will implement the interface and there will be selection process of the concrete classes. There is good definition in here.

I created a Generic class for Strategy Pattern. Generic Strategy Pattern class will accept the type of Concrete class and types of the parameters and it will take parameter for the argument of the concrete class.

Usage of the StrategyPattern,
 class Program
 {
   static void Main(string[] args)
   {
     int resultOfInt = StrategyPattern.StrategyAddExecutor<intIntProgrammingCalculatorImp>(1, 2);
     string resultString = StrategyPattern.StrategyAddExecutor<stringStringProgrammingCalculatorImp>("1AA2AA""2");
   }
 }
The Generic StrategyPattern class will implement the access of different methods defined in the IProgrammingCalculator interface for instance in here Add and Sub methods. In here T denotes the type of parameter of the methods and S denotes the concrete class type.
 public static class StrategyPattern
 {
    public static T StrategyAddExecutor<T, S>(T firstItem, T secondItem)
        where S : IProgrammingCalculator<T>, new()
   {
        return new S().Add(firstItem, secondItem);
   }
 
   public static T StrategySubExecutor<T, S>(T firstItem, T secondItem)
       where S : IProgrammingCalculator<T>, new()
   {
       return new S().Sub(firstItem, secondItem);
   }
 }
IProgrammingCalculator defines the definition of the calculator. There three different types of calculator IntProgrammingCalculator, LongProgrammingCalculator and StringProgrammingCalculator with its own implementation of the methods defined in the IProgrammingCalculator interface. 
 public interface IProgrammingCalculator<T>
 {
     T Add(T firstItem, T secondItem);
     T Sub(T firstItem, T secondItem);
 }
 
 public class IntProgrammingCalculatorImp : IProgrammingCalculator<int>
 {
     public int Add(int firstItem, int secondItem)
     {
         return firstItem + secondItem;
     }
 
     public int Sub(int firstItem, int secondItem)
     {
         return secondItem - firstItem;
     }
 }
 
 public class LongProgrammingCalculatorImp : IProgrammingCalculator<long>
 {
     public long Add(long firstItem, long secondItem)
     {
         return firstItem + secondItem;
     }
 
     public long Sub(long firstItem, long secondItem)
     {
        return secondItem - firstItem;
     }
 }
 
 public class StringProgrammingCalculatorImp : IProgrammingCalculator<string>
 {
     public string Add(string firstItem, string secondItem)
     {
       return firstItem + secondItem;
     }
 
     public string Sub(string firstItem, string secondItem)
     {
       return firstItem.Replace(secondItem, string.Empty);
     }
 }
There are few good articles in the web for further reading,

dofactory.com
Strategy pattern in the wikipedia

Codeplex, few new extension methods in .NET Extensions Methods Library for C# and VB.NET

I have added few extension methods to the project. For instance, in the BooleanExtensions class I added ToBinaryTypeNumber and CombineWith method in the ArrayExtension class.

Source code is in the Change Set 62750 and 62790.

Improvement of Generic Enum Parser and .NET Extensions Methods Library for C# and VB.NET

I updated the previous code about Generic Enum Parser. That code is I include into the .NET Extensions Methods Library for C# and VB.NET project in the Codeplex. The code is below,

public static TEnum ParseStringToEnum<TEnum>(this string dataToMatch, bool ignorecase = default(bool))
       where TEnum : struct
{
       return dataToMatch.IsItemInEnum<TEnum>()() ? default(TEnum) : (TEnum)Enum.Parse(typeof(TEnum), dataToMatch, default(bool));
}
 
public static Func<bool> IsItemInEnum<TEnum>(this string dataToCheck)
        where TEnum : struct
{
        return () => { return string.IsNullOrEmpty(dataToCheck) || !Enum.IsDefined(typeof(TEnum), dataToCheck); };
}
If anyone wants to download it please visit here. The code is part of the StringExtensions class.

Codeproject article about Page and Web user control communication.

I just finish writing a tips/tricks article about Asp.Net Page and Web user control communication in Code project. That article is about, if we want to execute a method of Asp.Net page from a event handler of Web user control's control. The article could be found at How to execute Asp.Net Page method from control's event handler of a web user control.

thanks mohammad.

Generic Factory pattern - to switch between Mock and Production environment service

Factory pattern - in my previous article, I explain the factory in detail. One thing came up in my mind about generic factory pattern. This is really handy to have in project. For example if we are working in a environment where service could be tested in Mock or Production environment. And also Mock and Production services using common contracts. So this is a good candidate of Generic Factory. In this article, I will include few sample contracts, production and mock implementation of those contracts, a Service Factory and Test class.
public interface IServiceProxyOne { void ProcessRequest(string data);   }
public interface IServiceProxyTwo { void ProcessRequest(string data);    }
 
public class ServiceProxyOne : IServiceProxyOne
{
  public void ProcessRequest(string data) { /* do something.*/}
}
public class ServiceProxyTwo : IServiceProxyTwo
{
  public void ProcessRequest(string data) { /* do something.*/}
}
 
public class MockServiceProxyOne : IServiceProxyOne
{
  public void ProcessRequest(string data) { /* do something.*/}
}
public class MockServiceProxyTwo : IServiceProxyTwo
{
  public void ProcessRequest(string data) { /* do something.*/}
}
Service Factory implementation code is below,
public static class ServiceFactory
{
     public static IServiceProxyOne CreateServiceProxyOne()
     {
       return GetProxyInstance<IServiceProxyOneMockServiceProxyOneServiceProxyOne>();
     }

     public static IServiceProxyTwo CreateServiceProxyTwo()
     {
       return GetProxyInstance<IServiceProxyTwoMockServiceProxyTwoServiceProxyTwo>();
     }
 
     public static T1 GetProxyInstance<T1, T2, T3>()
            where T2 : T1, new()
            where T3 : T1, new()
     {
       return IsMockEnvironment() ? (T1)new T2() : (T1)new T2();
     }
 
     public static bool IsMockEnvironment() { return !default(bool); }
}
To test the code we can use below,
static void Main(string[] args)
{
    ServiceFactory.CreateServiceProxyOne().ProcessRequest("Service Proxy one test.");
    ServiceFactory.CreateServiceProxyTwo().ProcessRequest("Service Proxy two test.");
} 
thanks mohammad.

 Few C# and Application Design books from Amazon,

Enum Parser - Enum with string as values.

Enum  parser - I already wrote a article about this. The enum I used that article was standard enum i.e.
public enum myEnum{ itemOne =0, itemTwo=1 } etc, but sometimes we need to do something like public enum myEnum{ itemOne ="Zero", itemTwo="One" },unfortunately .Net doesn't support that functionality directly. We need to do some thing different to do the job. One approach I used in this example is to use Attribute for every items in the enum. In the  processing time of this enum we will use reflection, using reflection we loop through attribute values and try to match with given value. Also I implement the related parser for the enum. The code I wrote is below, following code will show the enum and its structure.

public enum EnumWithText{ [TextValue("Clr")] ItemOne = 0,[TextValue("Dlr")]  ItemTwo = 1}
The above enum member has an attribute named TextValueAttribute, the implementation of the above class is as below,
public class TextValueAttribute : Attribute{
    public string TextValue { getprotected set; }
    public TextValueAttribute(string value)
     {TextValue = value;}}
Thats all we need to do. Now we need few methods to get the TextValue from the enum, following Parser class will help us to do the job,
public static class Parser
{
     public static string GetEnumValue(this Enum value)
     {
      Type type = value.GetType();
      FieldInfo fieldInfo = type.GetField(value.ToString());
      TextValueAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(TextValueAttribute), falseas TextValueAttribute[];
         return attribs.Length > default(int) ? attribs[default(int)].TextValue : null;
     }
     public static T1 ParseEnum<T1, T2>(T2 item)
     {
        return string.IsNullOrEmpty(item.ToString()) ?
        default(T1) :(T1)(Parser.ParseExtension<T1>(typeof(T1), item.ToString(), !default(bool)));
     }

     public static object ParseExtension<T1>(Type type, string stringValue, bool ignoreCase)
      {
        object result = null;string enumStringValue = null;
        if (!type.IsEnum) throw new ArgumentException("Enum type is required.");
         Array.ForEach(type.GetFields(),
                field =>
                {
                    TextValueAttribute[] attrs = field.GetCustomAttributes(typeof(TextValueAttribute), default(bool)) as TextValueAttribute[];
                    if (attrs.Length > default(int))
                        enumStringValue = attrs[default(int)].TextValue;
                    if (string.Compare(enumStringValue, stringValue, ignoreCase) == default(int))
                    {
                        result = Enum.Parse(type, field.Name);
                        return;
                    }
                });
             return result == null ? default(T1) : result;
        }
}
To test above code we could use following test harness code, 
static void Main(string[] args)
{
  string result = EnumWithText.ItemOne.GetEnumValue();
  EnumWithText myEnum1 = Parser.ParseEnum<EnumWithTextstring>("Clr");
  EnumWithText myEnum2 = Parser.ParseEnum<EnumWithTextstring>("Dlr");
  EnumWithText myEnum3 = Parser.ParseEnum<EnumWithTextstring>("None");
}

thanks mohammad.


 Few C# and Application Design books from Amazon,

Microsoft FxCop Sdk, Coding Standard and Custom RuleSet.

Microsoft FxCop Sdk is a library to help write programmers own ruleset i.e. we can write our own code analysis rulesets using FxCop sdk. Now the question is do we need to create our own custom ruleset? The answer depends on the environment of our work. If our work enforce about the coding standard, naming convention strictly then it is handy to have tool set which automatically check the standard etc.

In this article I am going to show how we can create custom ruleset and how to use those in Visual Studio. I use Microsoft Visual Studio 2010 as dev environment, Microsoft FxCop Sdk which comes along with VS 2010 installation. And coding rules, in this case I have two rules (1) All class names should end with 'Test' (2)
All variable names should not start 'm_' or '_'.

I created a solution named CodingStandard, in the solution I created AnalyzerEngine class library, set few dlls as reference. We need FxCopSdk and Microsoft.Cci.dll to reference in the project. Dlls are reference from C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop (this path is based on my VS 2010 installation in Windows 7 box might be different on yours).

Now the coding part, I created a xml file named RuleDefinition.xml with following contents,
<?xml version="1.0" encoding="utf-8" ?>
<Rules FriendlyName="Naming Standard Rules">
  <Rule
    TypeName="TypeNameAnalyzer"
    Category="AnalyzerEngine.CodingStandard"
    CheckId="AE0001">
    <Name>All class names should end with 'Test'</Name>
    <Description>TypeNameAnalyzer rule will check whether the class name ends with Test.</Description>
    <Url></Url>
    <Resolution Name="ClassNameResolution">The name of type {0} does not end with the suffix 'Test'. Add the suffix to the type name.</Resolution>
    <MessageLevel Certainty="95">Warning</MessageLevel>
    <FixCategories>Breaking</FixCategories>
    <Email />
    <Owner />
  </Rule>
 
  <Rule
    TypeName="VariableNameAnalyzer"
    Category="AnalyzerEngine.CodingStandard"
    CheckId="AE0002">
    <Name>All variable names should not start 'm_' or '_'</Name>
    <Description>VariableNameAnalyzer rule will check whether the variable name starts with m_ or _.</Description>
    <Url></Url>
    <Resolution Name="PrivateMemberNameResolution">The name of type {0} start with m_ or _</Resolution>
    <MessageLevel Certainty="95">Warning</MessageLevel>
    <FixCategories>Breaking</FixCategories>
    <Email />
    <Owner />
  </Rule>
</Rules>

base class named AnalyzerBase.cs with the following contents,

namespace AnalyzerEngine
{
    using Microsoft.FxCop.Sdk;
    public abstract class AnalyzerBase : BaseIntrospectionRule    {
        protected AnalyzerBase(string rule)
            : base(rule, "AnalyzerEngine.RuleDefinition.xml"typeof(AnalyzerBase).Assembly)
        { }
    }
}
in the above code BaseIntrospectionRule is from FxCop.Sdk and xml file name will be Namespace + "." + XML file name so in the case namespace is AnalyzerEngine and xml file name RuleDefinition.xml => AnalyzerEngine.RuleDefinition.xml. Also xml file has to be Embedded Resource of this assembly, so right click on this xml file select Properties -> then set the Build Action as Embedded Resource.

Two other rule classes TypeNameAnalyzer.cs and VariableNameAnalyzer.cs

TypeNameAnalyzer.cs
namespace AnalyzerEngine
{
    using System;
    using Microsoft.FxCop.Sdk;
    public class TypeNameAnalyzer : AnalyzerBase    {
        private readonly string classNameEndsWith = "Test";
        private readonly string resolutionName = "ClassNameResolution";
        public TypeNameAnalyzer()
            : base("TypeNameAnalyzer")
        { }
        public override ProblemCollection Check(TypeNode type)
        {
            if (!type.Name.Name.EndsWith(classNameEndsWith, StringComparison.Ordinal))
            {
                var classNameResolutionDetails = GetNamedResolution(resolutionName, type.Name.Name);
                var classNameProblem = new Problem(classNameResolutionDetails, type);
                Problems.Add(classNameProblem);
            }
            return Problems;
        }
    }
}
VariableNameAnalyzer.cs
namespace AnalyzerEngine
{
    using Microsoft.FxCop.Sdk;
    public class VariableNameAnalyzer : AnalyzerBase    {
        private readonly string vairableNameStartsWith_m = "m_";
        private readonly string vairableNameStartsWith_underscore = "_";
        private readonly string resolutionName = "PrivateMemberNameResolution";
        public VariableNameAnalyzer()
            : base("VariableNameAnalyzer")
        { }
        public override ProblemCollection Check(Member member)
        {
            Field variable = member as Field;
            if (variable == nullreturn Problems;
            if (variable.Name.Name.StartsWith(vairableNameStartsWith_m, System.StringComparison.Ordinal) ||
                variable.Name.Name.StartsWith(vairableNameStartsWith_underscore, System.StringComparison.Ordinal))
            {
                var resolutionDetails = GetNamedResolution(resolutionName, variable.Name.Name);
                var problem = new Problem(resolutionDetails, variable);
                Problems.Add(problem);
            }
            return Problems;
        }
    }
}

the Last bit is to create a ruleset file, in this case I created a ruleset named CodingStandardRules.ruleset with following contents
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Coding Standard Rules" Description=" " ToolsVersion="10.0">
  <RuleHintPaths>
    <Path>C:\Common\CodingStandardRules</Path>
  </RuleHintPaths>
  <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
    <Rule Id="AE0001" Action="Error" />
    <Rule Id="AE0002" Action="Error" />
  </Rules>
</RuleSet>

Now build this library. If we see the ruleset file from above then we can see a tag named <Path> this tag refers to the place in where we going to place our newly created assembly and the ruleset file so from where everyone(programmer in the team) can use to run the code analysis. After compiling the project we will get assembly AnalyzerEngine.dll, copy this dll along with CodingStandardRules.ruleset into the C:\Common\CodingStandardRules\ folder on my dev box. In the work environment should the folder should be somewhere in the network so then everyone can use the same information.

To test the rules I am going the create another project named AnalyzerEngine-TestHarness in the CodingStandard solution. The project has only simple Program.cs file with following contents,
namespace AnalyzerEngine_TestHarness
{
    class Program    {
        static void Main(string[] args)
        {
        }
    }
    public class CodingStandardClassname    {
        private string name = "I am string";
        private string _name = "I am string";
    }
}

Now we need to set our ruleset with this project, So right click on the project -> select Propertites -> from the new window click on the Code Analysisi tab from the left.From this window under Run this rule set  combo bex select the <Browse> option then it will pop up file dialog from there select the CodingStandardRules.ruleset file which is in this case in C:\Common\CodingStandardRules here, see the image below


Fig: Set options in Code analysis window

So all done build the  AnalyzerEngine-TestHarness project and we will get following errors,


Fig: Code analysis output.

I found following resources are really helpful for extra information in regards to FxCop,


I will try to upload the source code sometimes later. Thanks mohammad.

 Few C# and Application Design books from Amazon,