Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, April 22, 2013

Design Pattern in Microsoft.NET



Design Pattern
ASP.NET Example or Implementation
Adaptor and Bridge Pattern
List box,  Data Grid  and Tree View
Abstract Factory
Paint Graphics
Iterator and Composite Pattern
Enumeration interface
Singleton
Exceptions
Façade Pattern
ADO.NET Database connections
Proxy Pattern
 C# Timer



Creational
  • Abstract Factory Pattern
    • Provide an interface to create and return one of several families of related objects
      • E.g., FFT (Fast Fourier Transform )
  • Builder Pattern
    • Separate the construction the complex object from its representing so that several different representations can be created, depending on the needs of programs
  • Prototype Pattern
    • Start with an instantiated class and copies or clones it to make a new instances. These instance can then be further tailored using their public methods
  • Singleton
    • It's a class of which there can be no more than once instance. It provide a single global point  of access to that instance
Structural
  • Adapter Pattern
    • It can be used to make one class interface match another to make programming easier
  • Composite Pattern
    • It's a composition of objects, each of which may be either simple or itself a composite object
  • Proxy Pattern
    • It's frequently a simple object take place of a more complex object that may be invoked later
  • Flyweight Pattern
    • It's a pattern for sharing objects, where each instance does not contains it's own state but stores it externally
  • Façade Pattern
    • It used to make single class represent the entire subsystem
  • Bridge Pattern
    • It separates an object's interface from it's implementation, so you can vary them separately
  • Decorator Pattern
    • It used to add responsibilities to objects dynamically
Behavioral
  • Chain of Responsibility Pattern
    •  It allows a decoupling between objects by passing a request from one object to the next in a chain until the request is recognized.
  • Command Pattern
    • It utilizes simple objects to represent execution of software commands and allows you to support logging and undoable operations.  
  • Interpreter Pattern
    •  It provides a definition of how to include language elements in a program.
  • Iterator pattern
    • It defines how communication between objects can be simplified by using a separate object to keep all objects from having to know about each other.
  • Memento Pattern
    • It defines how you might save the contents of an instance of a class and restore it later.
  • Observer Pattern
    • It  defines the way a number of objects can be notified of  a change
  • State Pattern
    • It  allows an object to modify its behavior when its internal state changes.
  • Strategy Pattern
    • It encapsulates an algorithm inside an class
  • Template Method Pattern
    • It provides an abstract definition of an algorithm.
  • Visitor Pattern
    • It adds polymorphic functions to a class noninvasively



The above information is a good summary point to define and describe each design patterns. we could keep this as a reference point for technical interview.


Reference:

This note is picked from the following book. any body wants to learn in details
please check out this link
http://www.amazon.com/Design-Patterns-C-Software/dp/0321718933


 

Monday, December 24, 2012

Read Private Type using Visual Studio Unit Testing in C#

small things can make a different

I want to read my class  private values to verify the unit testing using MS unit test.

Here is the sample code

Created the sample class named NumberChecker, it contains two private property and public method.
The method going to check, the passed number is greater than zero or not. based on that it's going to pass a different message back the client.


namespace PrivateTypeDemo
{
    public class NumberChecker
    {
        private const string Validmessage = "The Number is Greater than Zero";
        private const string DefaultMessage = "The Number is Zero or Less than Zero";
        public string ValidateNumber(int number)
        {
            return number > 0 ? Validmessage : DefaultMessage;
        }
    }
}
Here is the console application which consumes the library

namespace PrivateTypeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var numcheck = new NumberChecker();
            Console.WriteLine("Please Enter your Number");
            var  number = Convert.ToInt32(Console.ReadLine());
            var result = numcheck.ValidateNumber(number);
            Console.WriteLine(result);
 
 
        }
    }
}

Unit Test for the NumberCheck class
 [TestClass]
    public class NumberCheckerTest
    {
        [TestMethod]
        public void ValidateNumberTest()
        {
            var target = new NumberChecker();
            const int number = 15;
            var privateType = new PrivateType(target.GetType());
            var expected = privateType.GetStaticFieldOrProperty("Validmessage");
            string actual = target.ValidateNumber(number);
            Assert.AreEqual(expected, actual);
        }
        [TestMethod]
        public void ValidateNumber2Test()
        {
            var target = new NumberChecker();
            const int number = -5;
            var privateType = new PrivateType(target.GetType());
            var expected = privateType.GetStaticFieldOrProperty("DefaultMessage");
            string actual = target.ValidateNumber(number);
            Assert.AreEqual(expected, actual);
        }
        [TestMethod]
        public void NumberCheckerConstructorTest()
        {
            var target = new NumberChecker();
            Assert.IsNotNull(target);
        }
    }
The below lines does the magic of accessing the private type of the classes.This class is shipped part of the 
Microsoft.VisualStudio.TestTools.UnitTesting Assembly  which support this functionality in unit Testing

var privateType = new PrivateType(target.GetType());
var expected = privateType.GetStaticFieldOrProperty("DefaultMessage");

Here i am reading the value of DefaultMessage through the GetStaticFieldOrProperty Method in the PrivateType class
To Learn More about this,
PrivateType

Tuesday, October 16, 2012

Read Event Log Information using LINQ PAD


Anyone tried the LINQ PAD. I found this is very useful to execute for query any short of information from different sources like database, xml, event logs, iis logs with the minimal code

In this blog, i am going to show a example, how we could pull the summary of the events using the LINQ PAD

Link to download LINQ PAD http://www.linqpad.net/


LINQPad is a free software utility for Microsoft .NET to interactively query SQL databases and other data sources such as OData or WCF Data Services using LINQ. LINQPad supports the following LINQ dialects:
  • LINQ to Objects
  • LINQ to SQL
  • Entity Framework
  • LINQ to XML
It also supports writing queries in SQL as well.

Download and install the LINQ PAD. Open the LINQ PAD and select a new query and select the language as C# Program

Code



void Main()
{
var serverList = new List
  {
  //List of Servers Name
  };
var eventCollection = new Dictionary();

int count = 0;
foreach (var list in serverList)
{
var eventLog = new EventLog(EVENTLOGNAME, list, SOURCENAME);
        foreach (EventLogEntry logEntry in eventLog.Entries)
        {
if (logEntry.TimeGenerated  >=  DateTime.Today)
{
if (eventCollection.ContainsKey(logEntry.InstanceId))
{
eventCollection[logEntry.InstanceId]++;
}
else
{
eventCollection.Add(logEntry.InstanceId, 1);
}
}

        }
Console.WriteLine("Summary of Server " + list);
foreach (var logs in eventCollection)
{
Console.WriteLine(logs.Key + "  " + logs.Value);
}
eventCollection.Clear();
}

}

// Define other methods and classes here


Sample Output

Summary of the Server "ServerName1"
Event ID   Number Of Times
100            7
106            15

Summary of the Server "ServerName2"
Event ID   Number Of Times
100            70
106            8

Tuesday, June 26, 2012

Shanmugam Currently Reading this Book


I am listing out the technical books i was currently reading it

CLR Via C#


CLR via C#


ASP.NET MVC 4 in Action




MVC 4 in Action


The Science of Influence



The Science of Influence

Domain Driven Design

DDD