WebSearch2 released with many updates

WebSearch2 released with many updates

Hello, WebSearch2 is released with Visual Studio 2012, 2013, 2015 support, by this release old version of WebSearch is no more supported in Visual Studio 2012 and above. Have considered many of your suggestions in this version, Hope you will like this updated version.

IconWeb Search2 is the updated version of Web Search.Web Search Features

  • Code or Comment Web Search from any Visual Studio Text Editor
  • Set Deafult Search Provider based on File Extension
  • Error List Assistant
  • Reference related Web Search from Solution Explorer Reference List
  • Search Anything using Web Search Toolbar
  • Web Search using Default Search, Custom Search, any Pre-configured Search
  • Open Search in Default Web Browser or Visual Studio
  • Shortcut key to execute
  • Supports Visual Studio 2012 and above.

Read more…

File Path On Footer 1.4.7 released

Hello, File Path On Footer 1.4.7 released, Please update.

Using this plugin you can,

  • File name always on bottom of editor.
  • Open Containing Folder using Ctrl+Click.
  • Copy Full Path using Right Click.
  • Copy Partial Path using text selection.
  • Use Alt + Click to force Theme updation.
  • Standard Visual Studio themes are supported.
  • Environment Font settings are also supported.
  • HighContrast Supported

Click here to see more details

Learning Factory Pattern using C#

Introduction

Factory pattern is very common in programming. If you check .NET framework or any other frameworks Factory Pattern is widely used which shows its popularity. Factory Pattern belongs to Creational Patterns, and it deals with object creation. In this article we shall try to learn Factory Pattern using a simple C# console application.

Background

When we write codes we used to come across situations like creating objects based on some if conditions or switch case. If this object creation is not based on a proven design pattern then we are really making the object creation complex and future enhancements very tough. At this time we should consider Factory Pattern to abstract the complexity of the object creation logic, and enabling future additions hassle free.

Let’s take a very simple example of logging functionality in a project, If we had the implementation as below, then for adding a new Logging option requires considerable code changes at client code base which is not a good practice.

public void Log(string message, string type)
{
    switch (type)
    {
        case "Database":
            DatabaseLogger databaseLogger = new DatabaseLogger();
            databaseLogger.Log(message);
            break;
        case "EventViewer":
        default:
            EventViewerLogger eventviewerLogger = new EventViewerLogger();
            eventviewerLogger.Log(message);
            break;
    }
}

Factory Pattern is nothing but hiding object creation logic from the client so that the end client doesn’t have to worry about object creational logic, rather the client can refer the factory pattern created object using a common interface.

Using the code

For better understanding we can create a simple Logger Factory which will help the client to log messages to different options based on their choice.

Factory Pattern Sample Logger Factory

To start with, we need to create an Interface or abstract class as the base class for all classes which we are going to create instance in factory. Here I have used an Interface ILogger with a method Log message.

interface ILogger
{
    void Log(string Message);
}

Now we are going to implement this ILogger in all classes which we wanted to return from Logger Factory. Here ideally Log method should have the real method to log the message to a file but for demonstrating purpose I’m just adding a console message.

class FileLogger : ILogger
{
    public void Log(string Message)
    {
        Console.WriteLine("{0} - Logged in File.", Message);
    }
}

the same way implementing ILogger to DatabaseLogger, also Log method definition added.

class DatabaseLogger : ILogger
{
    public void Log(string Message)
    {
        Console.WriteLine("{0} - Logged in Database.", Message);
    }
}

EventViewerLogger also implemented from ILogger and Log method definition added based on the type. We can add new logger classes same as these logger classes.

class EventViewerLogger : ILogger
{
    public void Log(string Message)
    {
        Console.WriteLine("{0} - Logged in EventViewer.", Message);
    }
}

Create an enum to easily identify the LoggerType, In case if we have a new LoggerType then we can add it here.

enum LoggerType
{
    File,
    Database,
    EventViewer
}

Finally we have reached to the LoggerFactory,now this factory will take care of the object creation based on the enum values, and will return the created instance back to client code which will be a type of ILogger.

class LoggerFactory
{
    public static ILogger Get(LoggerType type)
    {
        switch (type)
        {
            case LoggerType.Database:
                return new DatabaseLogger();
            case LoggerType.EventViewer:
                return new EventViewerLogger();
            case LoggerType.File:
            default:
                return new FileLogger();
        }
    }
}

If you look at the above code you can see that the object creation logic is abstracted in the factory and the objects are created based the kind of object requested, as the return type is an Interface ILogger, the client code need not worry about new addition to the factory.

Have written a client code to make use of this factory as below,

static void Main(string[] args)
{
    ILogger logger1 = LoggerFactory.Get(LoggerType.Database);
    logger1.Log("Message from Main");

    ILogger logger2 = LoggerFactory.Get(LoggerType.File);
    logger2.Log("Message from Main");

    ILogger logger3 = LoggerFactory.Get(LoggerType.EventViewer);
    logger3.Log("Message from Main");

    Console.ReadKey();

    /*Output
    Message from Main - Logged in Database.
    Message from Main - Logged in File.
    Message from Main - Logged in EventViewer.
    */
}

In the above code the client code uses the enum to request a specific object and using that object the log method is called. Objects are created using ILogger and instantiated using factory. Have given the output below,

FactoryPatter_Sample_Output.png

To explore this sample yourself you can download the attached code or create a console project named “FactoryPatternSample” and replace the content of Program.cs with below code block.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryPatternSample
{
    class Program
    {
        interface ILogger
        {
            void Log(string Message);
        }

        enum LoggerType
        {
            File,
            Database,
            EventViewer
        }

        class FileLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in File.", Message);
            }
        }

        class DatabaseLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in Database.", Message);
            }
        }

        class EventViewerLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in EventViewer.", Message);
            }
        }

        class LoggerFactory
        {
            public static ILogger Get(LoggerType type)
            {
                switch (type)
                {
                    case LoggerType.Database:
                        return new DatabaseLogger();
                    case LoggerType.EventViewer:
                        return new EventViewerLogger();
                    case LoggerType.File:
                    default:
                        return new FileLogger();
                }
            }
        }

        static void Main(string[] args)
        {
            ILogger logger1 = LoggerFactory.Get(LoggerType.Database);
            logger1.Log("Message from Main");

            ILogger logger2 = LoggerFactory.Get(LoggerType.File);
            logger2.Log("Message from Main");

            ILogger logger3 = LoggerFactory.Get(LoggerType.EventViewer);
            logger3.Log("Message from Main");

            Console.ReadKey();

            /*Output
            Message from Main - Logged in Database.
            Message from Main - Logged in File.
            Message from Main - Logged in EventViewer.
            */
        }
    }
}

Summary

In this article I have explained Factory Pattern with a simple C# application. I hope you have enjoyed this article and got some value addition to your knowledge. Please don’t forget to mark your votes, suggestions and feedback to improve the quality of this and upcoming articles.

Click here to Download Factory Pattern Sample – 6.7 KB

I Wish you a Happy New Year 2016

2015 was a busy year for me still I was able to publish some articles and plugins. This year I will try to find more time on blogging. Thanks everyone for the wonderful support and hoping the same in this year too.

“Optimism is the faith that leads to achievement. Nothing can be done without hope and confidence.”.– Helen Keller, American author, political activist, and lecturer

Best wishes for New Year 2016

Keep going! Happy Programming.

Cheers!

Shemeer.

Changing Visual Studio 2013 CAPS Menus to Normal

Are you okay with Visual Studio 2013 ALL CAPS Menu? If not there’s a simple solution.VS 2013 Menu to Normal Small letter

I have attached the .reg file with post. Extract the downloaded zip file and execute the reg. file with admin rights.

Restart Visual Studio 2013. It’s Done. If you want you can review the downloaded reg file in any text editor before executing.

If you want to do this manually then Open the Registry Editor (regedit) and add the below key and value.

Note: If you do anything wrong in this registry editing then Visual Studio 2012 won’t work after splash screen


HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\General\SuppressUppercaseConversion
REG_DWORD value: 1

Download zip contains .reg from Codepoject

I just ran this and I got the normal menu like below,

Visual Studio 2013 Normal Small Menu

If you want that CAPS Menus back then just remove the above key from the specified location.

Web Search – Visual Studio Plugin for Integrated Search Options in IDE

As a developer we often search some thing in Internet search engines or in favourite blogs. If you are working with Visual Studio then the search might be related to an exception, keyword, referenced items or any text that is in any code file.

I have published a Visual Studio plugin to do the same kind of activity inside the IDE.

Search anything from Visual Studio IDE using Web Search Plugin\Addin

Web Search

The plugin supports below options,

  • Multiple search options included in all Text Editor, Reference Items, Error List. (Integrated search options for Visual Studio IDE)
  • Currently this Add-in works fine with Visual Studio 2005, 2008, 2010, 2012.
  • You can customize web-search by configure option.
  • Open Search in Default Web Browser or Visual Studio Tab or any other installed browser.
  • Change WebSearch context menu position
  • Easy access with Shortcut key. I use Alt+F1.
  • WebSearch context menu availabe in all most all editor files.
  • WebSearch menu also availabe in Tools menu with default search option.
  • Configurable shortcut key.
  • WebSearch menu in Tools menu
  • WebSearch context menu in all Text Editor, Reference Items, Error List.

Click here to read more from Visual Studio Gallery. Interested in creating something like this then you might wanted to check WebSearch on the Code Project

Click here to DownloadVisual Studio Plugin Addin