Wednesday 5 June 2019

Design Patterns

Factory Method Pattern

A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.
The Factory Method Pattern is also known as Virtual Constructor.

Advantage of Factory Design Pattern
  • Factory Method Pattern allows the sub-classes to choose the type of objects to create.
  • It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.

Usage of Factory Design Pattern

  • When a class doesn't know what sub-classes will be required to create
  • When a class wants that its sub-classes specify the objects to be created.
  • When the parent classes choose the creation of objects to its sub-classes.

UML for Factory Method Pattern

  • We are going to create a Plan abstract class and concrete classes that inherited the Plan abstract class. A factory class GetPlanFactory is defined as a next step.
  • GenerateBill class will use GetPlanFactory to get a Plan object. It will pass information (DOMESTICPLAN / COMMERCIALPLAN / INSTITUTIONALPLAN) to GetPalnFactory to get the type of object it needs.

Calculate Electricity Bill : A Real World Example of Factory Method

using System;
//-------------------------------------Factory Design Pattern------------------------------------------//
namespace Design_Patterns
{
    public class GenerateBill
    {
        public static void Main(String[] args)
        {
            GetPlanFactory planFactory = new GetPlanFactory();
            Console.WriteLine("Enter the name of plan for which the bill will be generated: ");
            String planName = Console.ReadLine();
            Console.WriteLine("Enter the number of units for bill will be calculated: ");
            int units = Convert.ToInt32(Console.ReadLine());
            Plan p = planFactory.getPlan(planName);
            //call getRate() method and calculateBill()method of DomesticPaln. 
            Console.WriteLine("Bill amount for " + planName + " of  " + units + " units is: ");
            p.getRate();
            p.calculateBill(units);
            Console.ReadKey();
        }
    }
    public abstract class Plan
    {
        public double rate { get; set; }
        public abstract  void getRate();
        public void calculateBill(int units)
        {
            Console.WriteLine(units * rate);
        }
    }
    public class DomesticPlan : Plan
    {
        public override void getRate()
        {
            rate = 3.50;
        }
    }
    public class InstitutionalPlan : Plan
    {
        public override void getRate()
        {
            rate = 5.50;
        }
    }
    public class CommercialPlan : Plan
    { 
        public override void getRate()
        {
            rate = 7.50;
        }
    }
    public class GetPlanFactory
    { 
        //use getPlan method to get object of type Plan   
        public Plan getPlan(String planType)
        {
            if (planType == null)
            {
                return null;
            }
            if (planType.ToUpper() == "DOMESTICPLAN")
            {
                return new DomesticPlan();
            }
            else if (planType.ToUpper() == "COMMERCIALPLAN")
            {
                return new CommercialPlan();
            }
            else if (planType.ToUpper() == "INSTITUTIONALPLAN")
            {
                return new InstitutionalPlan();
            }
            return null;
        }
    }  
}

output:





Tuesday 4 June 2019

ASP.NET MVC 5: Logging Exceptions in Database

ASP.NET MVC 5: Logging Exceptions in Database?

During application development, we should consider logically correct and incorrect code execution based on the end-users requests, or the data posted by them. Sometimes even after testing our code using various test-cases, our code may not respond correctly to the request posted by end-users. Taking view of this reality, we implement our code via exception handling techniques using try-catch-finally block. This block successfully executes for all possible known types of exceptions e.g. File IO Read/Write issues, database connectivity issues etc. But what if during a request processing, a request fails due to the resource requested not available, or data posted by the end-user fails against some business rules. In this case, a web developer has to implement custom exception handling logic for handling such exceptions. This logic should be so flexible that whenever at any point in the application, such an exception occurs, this logic should be executed and exception handled and logged for further analysis.

Since ASP.NET MVC sits on top of the ASP.NET Run time, it means that an MVC application can use ASP.NET’s Application_Error event. In this case, any unhandled exception by MVC can be handled by the Application_Error event of ASP.NET. This is a great approach to handle Application Level Exceptions. But the problem of such an approach is that since it is at the application level and provides a lesser specific way of handling an exception, the MVC Controller Context data is lost. This means that we are no more handling an exception in our MVC application. However our requirement here is that we need to handle all exceptions at the MVC level only.

Section 1: Action Filter Life cycle

In ASP.NET MVC, we have ActionFilter using which we can evaluate incoming requests and process them against these filters e.g. Authentication, Exceptions, Results etc. We can use Exception Filter to monitor any exceptions that occurs during MVC Request processing. In MVC, a request is accepted over the Controller, and then the ActionMethod in the controller is executed. The Exception ActionFilter is applied here. This Action Filter is executed within the scope of Action Method Execution. In the following diagram, the life cycle of the ActionFilter for handing Exceptions is explained.

The exception filter is applied on the Action Method (alternatively we can even apply it on the Controller containing the Action Method.) The exception filter is executed within the action execution scope. If action method is executed successfully, then the Action Result will be generated where there may be a possibility of exception occurrence e.g. we are using custom view but it is not executed successfully for the current request. 

Section 2 – Logging Exceptions in an ASP.NET MVC application

We will be implementing this application using Visual Studio 2015. You can use VS 2013 too.
Step 1: Open Visual Studio and create a new ASP.NET Web Application using New Project window. Set the name of the application as MVC_LoggingExcepion_with_ExceptionFilter. Clicking OK on this window will bring up the New ASP.NET Project window. Select Empty MVC project template as shown in the following image


Step 2: In the project, add a new Sql Server Database of name ApplicationDB.mdf. Leave this database blank. We will use Entity Framework Code first architecture.
Step 3: In the project in the Models folder, right-click and add a new ADO.NET Entity Data Model of name ApplicationModelThis will start the wizard, in this wizard, in the Choose Model Contents window, select Code First from Database, as shown in the following image


Click on the Next button and select ApplicationDB.mdf from this window. Click on finish, this will generate ApplicationModel class derived from DbContext class.
Step 4: In the Model folder, add a new class file of name EmployeeInfo.cs with following code in it:
using System.ComponentModel.DataAnnotations;
namespace MVC_LoggingExcepion_with_ExceptionFilter.Models
{
    public class EmployeeInfo
    {
        [Key]
        public int EmpNo { get; set; }
        public string EmpName { get; set; }
        public string Designation { get; set; }
        public decimal Salary { get; set; }
    }
}
This is an entity class. We will use this for creating EmployeeInfo table using Code-First with Database approach. In the above class EmpNo is an attribute as Key so that it will be treated as primary key.
Step 5: Since the article describes about logging exception in database, we need a table to store log information. In the Models folder, add a new class file of name ExceptionLogger.cs with the following code in it.
using System;
using System.ComponentModel.DataAnnotations;
namespace MVC_LoggingExcepion_with_ExceptionFilter.Models
{
    public class ExceptionLogger
    {
        [Key]
        public int Id { get; set; }
        public string ExceptionMessage { get; set; }
        public string ControllerName{ get; set; }
        public string ExceptionStackTrace { get; set; }
        public DateTime LogTime { get; set; } 
    }
}
The above class contains properties for storing exception details.
Step 6: In the ApplicationModel.cs class file, add the following properties in the ApplicationModel class (highlighted)
public partial class ApplicationModel : DbContext
{
    public ApplicationModel()
        : base("name=ApplicationModel")
    {        
    }
    public DbSet<EmployeeInfo> Employees { get; set; }
    public DbSet<ExceptionLogger> ExceptionLoggers{ get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    }
}
These properties will update the database with Employees and ExceptionLoggers table.

Step 7: In the project, add a new folder of name CustomFilterIn this folder add a new class of name ExceptionHandlerAttribute with following code.
using MVC_LoggingExcepion_with_ExceptionFilter.Models;
using System;
using System.Web.Mvc;
namespace MVC_LoggingExcepion_with_ExceptionFilter.CustomFilter
{
    public class ExceptionHandlerAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled) {                  
                ExceptionLogger logger = new ExceptionLogger()
                {
                    ExceptionMessage = filterContext.Exception.Message,
                    ExceptionStackTrace = filterContext.Exception.StackTrace,
                    ControllerName = filterContext.RouteData.Values["controller"].ToString(),
                    LogTime  = DateTime.Now
                };
                ApplicationModel ctx = new ApplicationModel();
                ctx.ExceptionLoggers.Add(logger);
                ctx.SaveChanges();
                filterContext.ExceptionHandled = true;
            }
        }
    }
}
The above class is derived from the FilterAttribute class, this means that the ExceptionHandlerAttribute class will be used as ActionFilter in the MVC Filter Attribute lifecycle as explained in Section 1. The class implements IExceptionFilter interface and its OnException() method. This method contains logic for storing exception information in ExceptionLoggers table of the database.
Step 8: We need to register the exception filter in our MVC application. To complete the registration, add a new class file of the name FilterConfig in the App_Start folder. In this class, add the following code
using MVC_LoggingExcepion_with_ExceptionFilter.CustomFilter;
using System.Web.Mvc;
namespace MVC_LoggingExcepion_with_ExceptionFilter.App_Start
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new ExceptionHandlerAttribute());
        }
    }
}
Note: Since we have selected an Empty MVC template in Step 1 the above class is not present with us, but if you selected a readymade MVC template then the above class will be already present in the application. This class is used to add all Action Filters in the global configuration. This will be instantiated when the Web application starts.
In Global.asax, add the following line in the Application_Start. (highlighted).
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Step 8: In the Controllers folder, add a new controller of name EmployeeController with following action methods
using MVC_LoggingExcepion_with_ExceptionFilter.CustomFilter;
using MVC_LoggingExcepion_with_ExceptionFilter.Models;
using System;
using System.Linq;
using System.Web.Mvc;
namespace MVC_LoggingExcepion_with_ExceptionFilter.Controllers
{
    public class EmployeeController : Controller
    {
        ApplicationModel ctx = new ApplicationModel();
        // GET: Employee
        public ActionResult Index()
        {
            var Emps = ctx.Employees.ToList();
            return View(Emps);
        }
        public ActionResult Create()
        {
            return View(new EmployeeInfo());
        }
        [ExceptionHandler]
        [HttpPost]
        public ActionResult Create(EmployeeInfo Emp)
        {
            if (Emp.Designation == "Manager" && (Emp.Salary < 40000 || Emp.Salary > 80000))
            {
                throw new Exception("Salary is not Matching with Manager Designatgion");
            }
            else
            if (Emp.Designation == "Operator" && (Emp.Salary < 10000 || Emp.Salary > 20000))
            {
                throw new Exception("Salary is not Matching with Operator Designatgion");
            }
            else {
                ctx.Employees.Add(Emp);
                ctx.SaveChanges();
            }            
            return View(Emp);
        }
    }
}
The Index method returns all Employees. The Create with HttpPost method accepts an Employee object. This method contains some business rules on the Employee Salary based on the Designation. If these rules fail, then an Exception will be thrown. If the Employee Data is correct as per the business rules, then it will be saved in Employee table.
Step 9: Scaffold an Index and Create Views from Index and Create Action methods. In the RouteConfig.cs, modify the route expression as shown in the following code (highlighted)
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional }
    );
}
The above code will run the Index action method of the EmployeeController which will display Index view.
Step 10: Apply breakpoints on the Create with HttpPost method of the EmployeeController class and the OnException method of the ExceptionHandlerAttribute class. Run the application, the Index View will be displayed. Click on Create New link on this view to display the Create View. Enter data in the Create view as shown in the following image

We have the Designation as Operator and Salary as 8666, click on the Create button, the control will enter in debug Mode, the if condition for the Operator will throw an exception as shown in the following image
exception-result
Press F5, the control will enter ExceptionHandlerAttribute where the Database logging will take place. Close the application, the database table ExceptionLoggers will show the logged message as shown in the following image
logger-exception
Conclusion:
In a web application, Exception handling and logging is very important. ASP.NET MVC provides this facility using Exception Action Filters. We can create custom action filters to log these exception as per our business need.




Top Agile Interview Questions & Answers

Top Agile Interview Questions & Answers 1. What is Agile Testing? The first question of agile interview question tests your k...