Wednesday, July 29, 2009

C# Constraints on Generic Types

C# Constraints on Generic Types

When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints. Constraints are specified by using the where contextual keyword.

Why Use Generic Constraints

If you want to examine an item in a generic list to determine whether it is valid or to compare it to some other item, the compiler must have some guarantee that the operator or method it has to call will be supported by any type argument that might be specified by client code. This guarantee is obtained by applying one or more constraints to your generic class definition. For example, the base class constraint tells the compiler that only objects of this type or derived from this type will be used as type arguments. Once the compiler has this guarantee, it can allow methods of that type to be called in the generic class. Constraints are applied by using the contextual keyword where.

You can also set up constraints on generic classes. What if you wanted to create a generic list of objects that derived from a certain base class? This would allow you call certain functions that existed in that class.
By constraining the type, you increase the number of functions you can perform on the type.
// File: Constraints.cs
using System;
public class Employee
{
private string name;
private int id;
public Employee(string name, int id)
{
this.name = name;
this.id = id;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
}
class MyList where T : Employee
{
T[] list;
int count;
public MyList()
{
list = new T[10];
count = 0;
}
public void InsertSorted(T t)
{
int index = 0;
bool added = false;
while (index < count)
{
if (list[index].Id > t.Id)
{
for (int last = count; last > index; last--)
{
list[last] = list[last - 1];
}
list[index] = t;
added = true;
break;
}
index++;
}
if (!added)
{
list[index] = t;
}
count++;
}
}
class Program
{
static void Main()
{
MyList myList = new MyList();
myList.InsertSorted(new Employee("dan", 200));
myList.InsertSorted(new Employee("sabet", 100));
myList.InsertSorted(new Employee("mike", 150));
myList.InsertSorted(new Employee("richard", 120));
}
}

Friday, July 24, 2009

Abstract Factory Design Pattern

Design Patterns:

Design patterns make it easier to reuse successful designs and architectures. Design patterns help you choose design alternatives that make a system reusable and avoid alternatives that compromise reusability. They help make a system independent of how its objects are created, composed, and represented

Abstract Design Pattern:
An abstract factory provides an interface for creating families of related objects without specifying their concrete classes.

Sometimes one wants to construct an instance of one of a suite of classes, deciding between the classes at the time of instantiation. In order to avoid duplicating the decision making everywhere an instance is created, we need a mechanism for creating instances of related classes without necessarily knowing which will be instantiated.

Create an Abstract Factory class to answer instances of concrete classes (usually subclasses). The class of the resultant instance is unknown to the client of the Abstract Factory.

There are two types of Abstract Factory:

Simple Abstract Factory is an abstract class defining Factory methods to answer instances of concrete subclasses. The choice of which subclass to instantiate is completely defined by which method is used, and is unknown to the client.

The second form of Abstract Factory is an abstract class defining a common protocol of Factory methods. Concrete subclasses of the abstract factory implement this protocol to answer instances of the appropriate suite of classes.

Need to abstract from details of implementation of products –

1. The system shall be independent of how its constituent pieces are created, composed, and represented.
2. Need to have multiple families of products - The system shall be configured with one of multiple families of products.
3. Need to enforce families of products that must be used together - A family of related product objects is designed to be used together, and you need to enforce this constraint.
4. Need to hide product implementations and just present interfaces - You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Characteristics:

1. An abstract factory is an object maker.
2. It typically can produce more than one type of object.
3. Each object that it produces is known to the receiver of the created object only by that object's interface, not by the object's actual concrete implementation.
4. The different types of objects that the abstract factory can produce are related—they are from a common family.
5. An abstract factory isolates concrete classes.
6. It makes exchanging product families easy.
7. It promotes consistency among products.
8. It supports adding new kinds of products and their families

Example:

public interface IComputerFactory
{
ICPU createCPU();
IMemory createMemory();
}

public interface ICPU
{
string GetCPUString();
}

public interface IMemory
{
string GetMemoryString();
}

//Concrete CPUA
public class CPUA : ICPU
{
public string GetCPUString()
{
return "CPUA";
}
}
//Concrete MemoryA
public class MemoryA : IMemory
{
public string GetMemoryString()
{
return "MemoryA";

}
}

public class ComputerFactoryA : IComputerFactory
{
public ICPU createCPU()
{
return new CPUA();
}
public IMemory createMemory()
{
return new MemoryA();
}
}


public class Client
{
//this is a template method; does not depend on the Concrete Factory
//and the Concrete classes

public static string BuildComputer(IComputerFactory factory)
{
ICPU cpu = factory.createCPU();
IMemory memory = factory.createMemory();
StringBuilder sb = new StringBuilder();
sb.Append(string.Format("CPU:{0}", cpu.GetCPUString()));
sb.Append(Environment.NewLine);
sb.Append(string.Format("Memory:{0}",
memory.GetMemoryString()));

return sb.ToString();

}
}

Calling Client

private void button2_Click(object sender, EventArgs e)
{
Abstract_Factory.IComputerFactory factory= new Abstract_Factory.ComputerFactoryA();
MessageBox.Show(Abstract_Factory.Client.BuildComputer(factory));
}

Wednesday, July 1, 2009

Introduction to .Net 4.0

In the past programming languages were developed to be either Object oriented or functional. But, today languages were being designed with several paradigms in mind including all best features of programming and functional capabilities.

The .net languages especially C# is no mere exception to this. The C# language has been witnessing many changes and enhancements in each version to enable the application developers of C# to utilize the real power of programming languages. Ever since, the outburst of the C# language, a .net language in 1998, with goal of creating a simple, modern, object oriented and type safe language, it has witnessed many enhancements in each release of the .net framework.


The 2.0 version of the language saw the evolution of the support for the following



1) Generics,

2) Anonymous methods,

3) Iterators,

4) Partial types and

5) Nullable types.

The 3.0 version predominantly concentrated on LINQ (Language Integrated Query) and as a side note to this LINQ, the additional features includes the following to facilitate the former:

1) Implictly Typed Local Variables.

2) Extension Methods.

3) Lambda Expressions.

4) Object and Collection Initializers.

5) Annonymous types.

6) Implicitly Typed Arrays.

7) Query Expressions and Expression Trees.

Coming to the upcoming version of C# which is 4.0 is more inspired by dynamic languages like Perl, Python and Ruby. There are both advantages and disadvantages in using both statical and dynamic languages. Some of the new features that we are going to see in the upcoming release of .net Framework in respect to C# are as described in following sections.

C# 4.0 language innovations include:

Dynamically Typed Objects.
Optional and Named Parameters.
Improved COM Interoperability.
Safe Co- and Contra-variance.


1. Let us consider this simple statically typed .NET class which calls the Add method on that class to get the sum of two integers:

Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);

Our code gets all the more interesting if the Calculator class is not statically typed but rather is written in COM, Ruby, Python, or even JavaScript. Even if we knew that the Calculator class is a .NET object but we don't know specifically which type it is then we would have to use reflection to discover attributes about the type at runtime and then dynamically invoke the Add method.

object calc = GetCalculator();
Type type = calc.GetType();
object result = type.InvokeMember("Add",
BindingFlags.InvokeMethod, null,
new object[] { 10, 20 });
int sum = Convert.ToInt32(result);

If the Calculator class was written in JavaScript then our code would look somewhat like the following.

ScriptObect calc = GetCalculator();
object result = calc.InvokeMember("Add", 10, 20);
int sum = Convert.ToInt32(result);

With the C# 4.0 we would simply write the following code:

dynamic calc = GetCalculator();
int result = calc.Add(10, 20);

In the above example we are declaring a variable, calc, whose static type is dynamic. Yes, you read that correctly, we've statically typed our object to be dynamic. We'll then be using dynamic method invocation to call the Add method and then dynamic conversion to convert the result of the dynamic invocation to a statically typed integer.

You're still encouraged to use static typing wherever possible because of the benefits that statically typed languages afford us. Using C# 4.0 however, it should be less painful on those occassions when you have to interact with dynamically typed objects.


2. Another major benefit of using C# 4.0 is that the language now supports optional and named parameters and so we'll now take a look at how this feature will change the way you design and write your code.

One design pattern you'll often see as that a particular method is overloaded because the method needs to be called with a variable number of parameters.

Let's assume that we have the following OpenTextFile method along with three overloads of the method with different signatures. Overloads of the primary method then call the primary method passing default values in place of those parameters for which a value was not specified within the call to the overloaded method.

public StreamReader OpenTextFile(
string path,
Encoding encoding,
bool detectEncoding,
int bufferSize) { }

public StreamReader OpenTextFile(
string path,
Encoding encoding,
bool detectEncoding) { }

public StreamReader OpenTextFile(
string path,
Encoding encoding) { }

public StreamReader OpenTextFile(string path) { }

In C# 4.0 the primary method can be refactored to use optional parameters as the following example shows:

public StreamReader OpenTextFile(
string path,
Encoding encoding = null,
bool detectEncoding = false,
int bufferSize = 1024) { }

Given this declaration it is now possible to call the OpenTextFile method omitting one or more of the optional parameters.

OpenTextFile("foo.txt", Encoding.UTF8);

It is also possible to use the C# 4.0 support for named parameters and as such the OpenTextFile method can be called omitting one or more of the optional parameters while also specifying another parameter by name.

OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4098);

Named arguments must be provided last although when provided they can be provided in any order.


3. If you have ever written any code that performs some degree of COM interoperability you have probably seen code such as the following.

object filename = "test.docx";
object missing = System.Reflection.Missing.Value;
doc.SaveAs(ref filename,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);

With optional and named parameters the C# 4.0 language provides significant improvements in COM interoperability and so the above code can now be refactored such that the call is merely:

doc.SaveAs("foo.txt");

When performing COM interoperability you'll notice that you are able to omit the ref modifer although the use of the ref modifier is still required when not performing COM interoperability.



With previous versions of the technologies it was necessary to also ship a Primary Interop Assembly (PIA) along with your managed application. This is not necessary when using C# 4.0 because the compiler will instead inject the interop types directly into the assemblies of your managed application and will only inject those types you're using and not all of the types found within the PIA.


4. The final language improvement that we will explore is co-variance and contra-variance and we'll begin by exploring co-variance with .NET arrays.

string[] names = new string[] {
"Anders Hejlsberg",
"Mads Torgersen",
"Scott Wiltamuth",
"Peter Golde" };

Write(names);

Since version 1.0 an array in the .NET Framework has been co-variant meaning that an array of strings, for example, can be passed to a method that expects an array of objects. As such the above array can be passed to the following Write method which expects an array of objects.

private void Write(object[] objects)
{
}

Unfortunately arrays in .NET are not safely co-variant as we can see in the following code. Assuming that the objects variable is an array of strings the following will succeed.

objects[0] = "Hello World";

Although if an attempt is made to assign an integer to the array of strings an ArrayTypeMismatchException is thrown.

objects[0] = 1024;

In both C# 2.0 and C# 3.0 generics are invariant and so a compiler error would result from the following code:

List names = new List();

Write(names);

Where the Write method is defined as:


Generics with C# 4.0 now support safe co-variance and contra-variance through the use of the in and out contextual keywords. Let's take a look at how this changes the definition of the IEnumerable and IEnumerator interfaces.

public interface IEnumerable
{
IEnumerator GetEnumerator();
}

public interface IEnumerator
{
T Current { get; }
bool MoveNext();
}

You'll notice that the type parameter T of the IEnumerable interface has been prefixed with the out contextual keyword. Given that the IEnumerable interface is read only, there is no ability specified within the interface to insert new elements with the list, it is safe to treat something more derived as something less derived. With the out contextual keyword we are contractually affirming that IEnumerable is safely co-variant. Given that IEnumerable is safely co-variant we can now write the following code:


Because the IEnumerable interface uses the out contextual keyword the compiler can reason that the above assignment is safe.

Using the in contextual keyword we can achieve safe contra-variance, that is treating something less derived as something more derived.

public interface IComparer
{
int Compare(T x, T y);
}

Given that IComparer is safely contra-variant we can now write the following code:

IComparer[object] objectComparer = GetComparer();
IComparer[string] stringComparer = objectComparer;

Although the current CTP build of Visual Studio 2010 and the .NET Framework 4.0 has limited support for the variance improvements in C# 4.0 the forthcoming beta will allow you to use the new in and out contextual keywords in types such as IComparer. The .NET Framework team is updating the types within the framework to be safely co- and contra-variant.

Friday, June 19, 2009

How to make window services in .Net

How to make window services in .Net

Definition: Microsoft Windows services enable you to create long-running executable applications that run in their own Windows sessions. These services (e.g. SQL server Service) can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. Whenever you need long-running functionality that does not interfere with other users who are working on the same computer.


We can start, stop, or pause a Windows service from the Windows Services management console. This MMC (Microsoft Management Console) can be opened by selecting the group from the Administrative Tools programs group. All the services currently running on our system.

Windows Services in DOTNET
To make a Windows service, we need three types of operational programs. They are as follows:
• Service Program
• Service Control Program
• Service Configuration program
Service Program
A Service Program is the main program where our code resides. Here, we write the actual logic for our service. One point to note here is that each service program can refer to more than one service but each one of these services must contain their own entry point or a Main() method. This Main() method is just like the Main() method in C#.
To write a service class, we need to inherit it from a ServiceBase class. The ServiceBase class is a part of the System.ServiceProcess class.
Service Control Program
A service control program is a part of the operating system. It is responsible for the starting, stopping, and pausing of a Windows service. To implement a Windows service, we need to inherit from the ServiceController class of the System.ServiceProcess class.
Service Configuration Program
Once a service is made, it needs to be installed and configured. Configuration involves that this service will be started manually or at boot time. To implement a service program, we need to inherit from the ServiceProcessInstaller class of ServiceInstaller.

To create a new Windows service, select a Windows service project. Type a project name and click OK. VS.NET will create the basic code for us. We will just have to write the logic of the code.
There is one important thing to consider in the Main() method:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace MonitoringService
{
static class Program
{
///
/// The main entry point for the application.
///

static void Main()
{
ServiceBase[] ServicesToRun = new ServiceBase[]
{
new MonitoringWinService()
};
ServiceBase.Run(ServicesToRun);
}
}
}


MonitoringService.config










MonitoringWinService.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using System.IO;

namespace MonitoringService
{
public partial class MonitoringWinService : ServiceBase
{
readonly EventLog eventLog1 = new EventLog();

readonly System.Timers.Timer timer1 = new System.Timers.Timer();

public MonitoringWinService()
{
InitializeComponent();

timer1.Enabled = true;
timer1.Start();
timer1.Interval= 60000; // One Minute


if(!System.Diagnostics.EventLog.SourceExists("MonitoringWinService"))
System.Diagnostics.EventLog.CreateEventSource("MonitoringWinService",
"MonitoringWinService_Logging");

eventLog1.Source = "MonitoringWinService";

eventLog1.Log = "MonitoringWinService_Logging";
}

#region Properties

private string _MonitoringFolder = string.Empty;
public string MonitoringFolder
{
set
{
if(!string.IsNullOrEmpty(ConfigurationSettings.AppSettings["FolderPath"].ToString()))
{
_MonitoringFolder = mCheckFolderPath(ConfigurationSettings.AppSettings["FolderPath"].ToString());
}
else
{
_MonitoringFolder = @"c:\Image_Folder\"; // Default Value
}
}
get
{
return _MonitoringFolder;
}
}

private string _LogFile = string.Empty;
public string LogFile
{
set
{
if(!string.IsNullOrEmpty(ConfigurationSettings.AppSettings["WinService_log"].ToString()))
{
_LogFile = ConfigurationSettings.AppSettings["WinService_log"].ToString();
}
else
{
_LogFile = @"C:\MonitoringService_LogFile.log"; // Default Value
}
}
get
{
return _LogFile;
}
}

#endregion


#region CustomMethods

///
/// For Monitoring Folder Size
///

///
///
public int mGetDirectoryLength(string strDirectory)
{
long iTotalSize = 0;
List list = new List();
try
{
foreach (string d in Directory.GetFiles(strDirectory))
{
iTotalSize = iTotalSize + mGetFileLength(d);

}

return mConvertBytesIntoGB(iTotalSize);
}
catch (System.Exception excpt)
{
return 0;
}

}
///
/// Convert Bytes into MB
///

///
///
public int mConvertBytesIntoMB(long bytes)
{
return Convert.ToInt32(Math.Round(bytes / 1024.0 / 1024.0, 2));

}

///
/// Convert Bytes into GB
///

///
///
public int mConvertBytesIntoGB(long bytes)
{
return Convert.ToInt32(Math.Round(bytes / 1024.0 / 1024.0 / 1024.0, 2));

}
///
/// Get File Length of the File
///

///
///
public long mGetFileLength(string strFilePath)
{
if (!string.IsNullOrEmpty(strFilePath))
{
FileInfo info = new System.IO.FileInfo(strFilePath);
return info.Length;
}
return 0;
}
///
/// To Check the Folder Path.
/// Adding slash at end to make it valid path
///

///
///
public static string mCheckFolderPath(string strpath)
{
if(!string.IsNullOrEmpty(strpath))
{
if(strpath.Substring((strpath.Length-1), 1)!=@"\")
{
return strpath.Insert(strpath.Length, @"\");

}

return strpath;
}

return strpath;
}
///
/// Log-Writer
///

///
public void mWriteLogFile(string LogMessage)
{
if (LogMessage == null) throw new ArgumentNullException("LogMessage");
if (!File.Exists(LogFile))
File.Create(LogFile);

FileStream fs = new FileStream(LogFile, FileMode.Open, FileAccess.Write);

StreamWriter writer = new StreamWriter(fs, ASCIIEncoding.Default);

writer.WriteLine(LogMessage);
writer.Flush();
writer.Close();

fs.Close();

}

#endregion

protected override void OnStart(string[] args)
{
mWriteLogFile("Service Started ............." + DateTime.Now);
eventLog1.WriteEntry("Service Started " + DateTime.Now);
}

protected override void OnStop()
{
mWriteLogFile("Service Stopped ............." + DateTime.Now);
eventLog1.WriteEntry("Service Stopped " + DateTime.Now);
}

protected override void OnContinue()
{
mWriteLogFile("Image_Folder monitoring in Process " + DateTime.Now);
eventLog1.WriteEntry("Image_Folder monitoring in Process " + DateTime.Now);

}

private void timer1_Tick(object sender, EventArgs e)
{
mWriteLogFile(string.Format("Folder Size [GB] {0} - DateTime {1}", mGetDirectoryLength(MonitoringFolder).ToString(), DateTime.Now.ToString()));
eventLog1.WriteEntry(string.Format("Folder Size [GB]{0} - DateTime {1}", mGetDirectoryLength(MonitoringFolder).ToString(), DateTime.Now.ToString()));
}
}
}

InstallerClass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;



namespace MonitoringService
{
[RunInstallerAttribute(true)]
public class InstallerClass : System.Configuration.Install.Installer
{
public InstallerClass()
{
ServiceInstaller si = new ServiceInstaller();
ServiceProcessInstaller spi = new ServiceProcessInstaller();

si.ServiceName = "MonitoringImageFolder";
si.DisplayName = "MonitoringImageFolder";
si.StartType = ServiceStartMode.Automatic;
this.Installers.Add(si);

spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
spi.Username=null;
spi.Password=null;
this.Installers.Add(spi);
}
}
}


Deployment Process
To add a deployment project
1. On the File menu, point to Add, and then click New Project.
2. In the Add New Project dialog box's Project Type pane, open the Other Project Types node, select Setup and Deployment Projects. In the Templates pane, choose Setup Project. In the Name box, type MonitoringService_Setup.
The project is added to Solution Explorer and the File System Editor is displayed.
3. In the File System Editor, select Application Folder in the left pane. On the Action menu, point to Add, and then choose Project Output.
4. In the Add Project Output Group dialog box, OpenWeb3 will be displayed in the Project list. Select Primary Output.
Primary Output from OpenWeb (Active) appears in the Application Folder.
To add the custom action
1. Select the MonitoringService_Setup project in Solution Explorer. On the View menu, point to Editor, and then choose Custom Actions.
The Custom Actions Editor is displayed.
2. In the Custom Actions Editor, select the Commit node. On the Action menu, choose Add Custom Action.
3. In the Select Item in Project dialog box, double-click the Application Folder. Then select Primary output from MonitoringService.
Primary output from MonitoringService appears under the Commit node in the Custom Actions Editor.
4. In the Properties window, make sure that the InstallerClass property is set to True (this is the default).
5. In the Custom Actions Editor, select the Install node and add Primary output from MonitoringService to this node as you did for the Commit node.
6. On the Build menu, choose MonitoringService_Setup.
InstallUtil could also be use to Install the Windows Service
Open a Visual Studio .NET Command Prompt
Change to the bin\Debug directory of your project location (bin\Release if you compiled in release mode)
Issue the command InstallUtil.exe MonitoringService.exe to register the service and have it create the appropriate registry entries
Open the Computer Management console by right clicking on My Computer on the desktop and selecting Manage
In the Services section underneath Services and Applications you should now see your Windows Service included in the list of services
Start your service by right clicking on it and selecting Start
Each time you need to change your Windows Service it will require you to uninstall and reinstall the service. Prior to uninstalling the service it is a good idea to make sure you have the Services management console closed. If you do not you may have difficulty uninstalling and reinstalling the Windows Service. To uninstall the service simply reissue the same InstallUtil command used to register the service and add the /u command switch

Monday, June 8, 2009

What is SilverLight

What is Silverlight?

Silverlight is a new cross-browser, cross-platform implementation of the .NET Framework for building and delivering the next generation of media experiences and Rich Interactive Applications(RIA) for the web. It runs in all popular browsers, including Microsoft Internet Explorer, Mozilla Firefox, Apple Safari, Opera. The plugin required to run Silverlight is very small in size hence gets installed very quickly.
It is combination of different technolgoies into a single development platform that allows you to select tools and the programming language you want to use. Silverlight integrates seamlessly with your existing Javascript and ASP.NET AJAX code to complement functionality which you have already created.
Silverlight aims to compete with Adobe Flash and the presentation components of Ajax. It also competes with Sun Microsystems' JavaFX, which was launched a few days after Silverlight.
Currently there are 2 versions of Silverlight:

Silverlight 1.0 :

Silverlight 1.0 consists of the core presentation framework, which is responsible for UI, interactivity and user input, basic UI controls, graphics and animation, media playback, DRM support, and DOM integration.
Main features of Silverlight 1.0 :
1. Built-in codec support for playing VC-1 and WMV video, and MP3 and WMA audio within a browser.
2. Silverlight supports the ability to progressively download and play media content from any web-server.
3. Silverlight also optionally supports built-in media streaming.
4. Silverlight enables you to create rich UI and animations, and blend vector graphics with HTML to create compelling content experiences.
5. Silverlight makes it easy to build rich video player interactive experiences.

Silverlight 1.1 :

Silverlight 1.1 includes a version of the .NET Framework, with the full Common Language Runtime as .NET Framework 3.0; so it can execute any .NET language including VB.NET and C# code. Unlike the CLR included with .NET Framework, multiple instances of the CoreCLR included in Silverlight can be hosted in one process.[16] With this, the XAML layout markup file (.xaml file) can be augmented by code-behind code, written in any .NET language, which contains the programming logic.
Main features of Silverlight 1.1 :
1. A built-in CLR engine that delivers a super high performance execution environment for the browser. Silverlight uses the same core CLR engine that we ship with the full .NET Framework.
2. Silverlight includes a rich framework library of built-in classes that you can use to develop browser-based applications.
3. Silverlight includes support for a WPF UI programming model. The Silverlight 1.1 Alpha enables you to program your UI with managed code/event handlers, and supports the ability to define and use encapsulated UI controls.
4. Silverlight provides a managed HTML DOM API that enables you to program the HTML of a browser using any .NET language.
5. Silverlight doesn't require ASP.NET to be used on the backend web-server (meaning you could use Silverlight with with PHP on Linux if you wanted to).
How Silverlight would change the Web:
1. Highest Quality Video Experience : prepare to see some of the best quality videos you have seen in your life, all embedded in highly graphical websites. The same research and technology that was used for VC-1, the codec that powers BluRay and HD DVD, is used by Microsoft today with its streaming media technologies.
2. Cross-Platform, Cross-Browser : Finally build web applications that work on any browser, and on any operating system. At release, Silverlight will work with Mac as well as Windows! The Mono project has also already promised support for Linux!.
3. Developers and Graphic Designers can play together! : Developers familiar with Visual Studio, Microsoft.net will be able to develop amazing Silverlight applications very quickly, and they will work on Mac's and Windows. Developers will finally be able to strictly focus on the back end of the application core, while leaving the visuals to the Graphic Design team using the power of XAML.
4. Cheaper : Silverlight is now the most inexpensive way to stream video files over the internet at the best quality possible. Licensing is dead simple, all you need is IIS in Windows Server, and you’re done.
5. Support for 3rd Party Languages : Using the power of the new Dynamic Language Runtime, developers will now be able to use Ruby, Python, and EcmaScript! This means a Ruby developer can develop Silverlight applications, and leverage the .net Framework!
6. Cross-Platform, Cross-Browser Remote Debugging : If you are in the need to debug an application running on a Mac, no problem! You can now set breakpoints, step into/over code, have immediate windows, and all that other good stuff that Visual Studio provides.
7. The best development environment on the planet : Visual Studio is an award winning development platform! As it continues to constantly evolve, so will Silverlight!
8. Silverlight offers copy protection : Have you noticed how easy it is to download YouTube videos to your computer, and save them for later viewing ? Silverlight will finally have the features enabling content providers complete control over their rich media content! Streaming television, new indie broadcast stations, all will now be possible!
9. Extreme Speed :There is a dramatic improvement in speed for AJAX-enabled websites that begin to use Silverlight, leveraging the Microsoft .net framework.
Getting Started With SilverLight :
In order to create Silverlight applications with following :
Runtime :
Microsoft Silverlight 1.1 (currently alpha) : The runtime required to view Silverlight applications created with .NET Microsoft.
Developer Tools :
Microsoft Visual Studio codename "Orcas" (currently Beta 1) : The next generation development tool.
Microsoft Silverlight Tools Alpha for Visual Studio codename "Orcas" : The add-on to create Silverlight applications using .NET.
Designer Tools :
Download the Expression designer tools to start designing Silverlight application.
Expression Blend 2
Software Development Kit:
Microsoft Silverlight 1.1 Alpha Software Development Kit (SDK) : Download this SDK to create Silverlight Web experiences that target Silverlight 1.1 Alpha. The SDK contains documentation and samples.

Thursday, May 28, 2009

OracleHelper.cs

This OracleHelper class is intended to encapsulate high performance, scalable best practices for common uses of OracleClient

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;

namespace Trans829115.DAL
{
public class OracleHelper
{
private string strConnectionString;
private OracleConnection oConnection;
private OracleCommand oCommand;
private int iTimeOut = 30;

public enum ExpectedType
{

StringType = 0,
NumberType = 1,
DateType = 2,
BooleanType = 3,
ImageType = 4
}

public OracleHelper()
{
try
{

strConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

oConnection = new OracleConnection(strConnectionString);
oCommand = new OracleCommand();
oCommand.CommandTimeout = iTimeOut;
oCommand.Connection = oConnection;

}
catch (Exception ex)
{
throw new Exception("Error initializing OracleHelper class." + Environment.NewLine + ex.Message);
}
}


public OracleHelper(string MyConnectionString)
{
try
{

strConnectionString = MyConnectionString;

oConnection = new OracleConnection(strConnectionString);
oCommand = new OracleCommand();
oCommand.CommandTimeout = iTimeOut;
oCommand.Connection = oConnection;

}
catch (Exception ex)
{
throw new Exception("Error initializing OracleHelper class." + Environment.NewLine + ex.Message);
}
}

public void Dispose()
{
try
{
//Dispose off connection object
if (oConnection != null)
{
if (oConnection.State != ConnectionState.Closed)
{
oConnection.Close();
}
oConnection.Dispose();
}

//Clean Up Command Object
if (oCommand != null)
{
oCommand.Dispose();
}

}

catch (Exception ex)
{
throw new Exception("Error disposing OracleHelper class." + Environment.NewLine + ex.Message);
}

}

public void CloseConnection()
{
if (oConnection.State != ConnectionState.Closed) oConnection.Close();
}

public int GetExecuteScalarByCommand(string Command)
{

object identity = 0;
try
{
oCommand.CommandText = Command;
oCommand.CommandTimeout = iTimeOut;
oCommand.CommandType = CommandType.StoredProcedure;

oConnection.Open();

oCommand.Connection = oConnection;
identity = oCommand.ExecuteScalar();
CloseConnection();
}
catch (Exception ex)
{
CloseConnection();
throw ex;
}
return Convert.ToInt32(identity);
}

public void GetExecuteNonQueryByCommand(string Command)
{
try
{
oCommand.CommandText = Command;
oCommand.CommandTimeout = iTimeOut;
oCommand.CommandType = CommandType.StoredProcedure;

oConnection.Open();

oCommand.Connection = oConnection;
oCommand.ExecuteNonQuery();

CloseConnection();
}
catch (Exception ex)
{
CloseConnection();
throw ex;
}
}

public void GetExecuteNonQueryBySQL(string strSQL)
{
try
{
oCommand.CommandText = strSQL;
oCommand.CommandTimeout = iTimeOut;
oCommand.CommandType = CommandType.Text;

oConnection.Open();

oCommand.Connection = oConnection;
oCommand.ExecuteNonQuery();

CloseConnection();
}
catch (Exception ex)
{
CloseConnection();
throw ex;
}
}

public DataSet GetDatasetByCommand(string Command)
{
try
{
oCommand.CommandText = Command;
oCommand.CommandTimeout = iTimeOut;
oCommand.CommandType = CommandType.StoredProcedure;

oConnection.Open();

OracleDataAdapter adpt = new OracleDataAdapter(oCommand);
DataSet ds = new DataSet();
adpt.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection();
}
}

public DataSet GetDatasetBySQL(string strSQL)
{
try
{
oCommand.CommandText = strSQL;
oCommand.CommandTimeout = iTimeOut;
oCommand.CommandType = CommandType.Text;

oConnection.Open();

OracleDataAdapter Adapter = new OracleDataAdapter(strSQL, oConnection);
DataSet ds = new DataSet();
Adapter.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection();
}
}


public OracleDataReader GetReaderBySQL(string strSQL)
{
oConnection.Open();
try
{
OracleCommand myCommand = new OracleCommand(strSQL, oConnection);
return myCommand.ExecuteReader();
}
catch (Exception ex)
{
CloseConnection();
throw ex;
}
}

public OracleDataReader GetReaderByCmd(string Command)
{
OracleDataReader objOracleDataReader = null;
try
{
oCommand.CommandText = Command;
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.CommandTimeout = iTimeOut;

oConnection.Open();
oCommand.Connection = oConnection;

objOracleDataReader = oCommand.ExecuteReader() ;
return objOracleDataReader;
}
catch (Exception ex)
{
CloseConnection();
throw ex;
}

}

public void AddParameterToSQLCommand(string ParameterName, OracleType ParameterType)
{
try
{
oCommand.Parameters.Add(new OracleParameter(ParameterName, ParameterType));
}

catch (Exception ex)
{
throw ex;
}
}
public void AddParameterToSQLCommand(string ParameterName, OracleType ParameterType,int ParameterSize)
{
try
{
oCommand.Parameters.Add(new OracleParameter(ParameterName, ParameterType, ParameterSize));
}

catch (Exception ex)
{
throw ex;
}
}
public void SetSQLCommandParameterValue(string ParameterName, object Value)
{
try
{
oCommand.Parameters[ParameterName].Value = Value;
}

catch (Exception ex)
{
throw ex;
}
}
}

}

Wednesday, May 27, 2009

Attributes in C#.Net

Attributes are a new kind of declarative information. We can use attributes to define both design-level information (such as help file, URL for documentation) and run-time information (such as associating XML field with class field). We can also create "self-describing" components using attributes.

"An attribute is a piece of additional declarative information that is specified for a declaration."
- MSDN

Attributes
C# allows you to add declarative information to a program in the form of an attribute. An attribute defines additional information (metadata) that is associated with a class, structure, method, and so on. For example, you might define an attribute that determines the type of button that a class will display. Attributes are specified between square brackets, preceding the item to which they apply. Thus, an attribute is not a member of a class. Rather, an attribute specifies supplemental information that is attached to an item.
When do we need attributes ?

The advantage of using attributes resides in the fact that the information that it contains is inserted into the assembly. This information can then be consumed at various times for all sorts of purposes:
1. An attribute can be consumed by the compiler. The System.ObsoleteAttribute attribute that we have just described is a good example of how an attribute is used by the compiler, certain standard attributes which are only destined for the compiler are not stored in the assembly. For example, the SerializationAttribute attribute does not directly mark a type but rather tells the compiler that type can be serialized. Consequently, the compiler sets certain flags on the concerned type which will be consumed by the CLR during execution such attributes are also named pseudo-attributes.
2. An attribute can be consumed by the CLR during execution. For example the .NET Framework offers the System.ThreadStaticAttribute attribute. When a static field is marked with this attribute the CLR makes sure that during the execution, there is only one version of this field per thread.
3. An attribute can be consumed by a debugger during execution. Hence, the System.Diagnostics.DebuggerDisplayAttribute attribute allows personalizing the display of an element of the code(the state of an object for example) during debugging.
4. An attribute can be consumed by a tool, for example, the .NET framework offers the System.Runtime.InteropServices.ComVisibleAttribute attribute. When a class is marked with this attribute, the tlbexp.exe tool generates a file which will allow this class to be consumed as if it was a COM object.
5. An attribute can be consumed by your own code during execution by using the reflection mechanism to access the information. For example, it can be interesting to use such attributes to validate the value of fields in your classes. Such a field must be within a certain range. Another reference field must not be null. A string field can be atmost 100 character. Because of the reflection mechanism, it is easy to write code to validate the state of any marked fields. A little later, we will show you such an example where you can consume attributes by your own code.
6. An attribute can be consumed by a user which analyses an assembly with a tool such as ildasm.exe or Reflector. Hence you could imagine an attribute which would associate a character string to an element of your code. This string being contained in the assembly, it is then possible to consult these comments without needing to access source code.
Things to know about attributes
1. An attribute must be defined by a class which derives from System.Attribute.
2. An Instance of the attribute class is only instantiated when the reflection mechanism accesses one of its representatives, Depending on its use, an attribute class in not necessarily instantiated(as with the System.ObsoleteAttribute class which does not need to be used by the reflection mechanism).
3. The .NET framework puts several attributes to your disposition. Certain attributes are destined to be used by the CLR. Other are consumed by the compiler or tools supplied by Microsoft.
4. You have the possibility of creating your own attribute classes, They will then necessarily be consumed by your program as you cannot tinker the compiler or the CLR.
5. By convention, the name of an attribute class is suffixed by Attribute. However, an attribute named XXXAttribute can be used in c# both using the XXXAttribute expression but also with the XXX expression when it marks an element of the code.
Reference:
Practical .NET2 and C#2 By Patrick Smacchia


Attribute Basics

An attribute is supported by a class that inherits System.Attribute. Thus, all attribute classes must be subclasses of Attribute. Although Attribute defines substantial functionality, this functionality is not always needed when working with attributes. By convention, attribute classes often use the suffix Attribute. For example, ErrorAttribute would be a name for an attribute class that described an error.
When an attribute class is declared, it is preceded by an attribute called AttributeUsage. This built-in attribute specifies the types of items to which the attribute can be applied. Thus, the usage of an attribute can be restricted to methods, for example.
Creating an Attribute
In an attribute class, you will define the members that support the attribute. Often attribute classes are quite simple, containing just a small number of fields or properties. For example, an attribute might define a Note that describes the item to which the attribute is being attached. Such an attribute might look like this:
[AttributeUsage(AttributeTargets.All)]
public class NoteAttribute : Attribute {
string strNote;

public NoteAttribute(string comment) {
strNote = comment;
}

public string Note {
get {
return strNote;
}
}
}
Let’s look at this class, line by line.
The name of this attribute is NoteAttribute. Its declaration is preceded by the AttributeUsage attribute, which specifies that NoteAttribute can be applied to all types of items. Using AttributeUsage, it is possible to narrow the list of items to which an attribute can be attached, and we will examine its capabilities later in this chapter.
Next, NoteAttribute is declared and it inherits Attribute. Inside NoteAttribute there is one private field, strNote, which supports one public, read-only property: Note. This property holds the description that will be associated with the attribute. There is one public constructor that takes a string argument and assigns it to Note.
At this point, no other steps are needed, and NoteAttribute is ready for use.
Attaching an Attribute
Once you have defined an attribute class, you can attach the attribute to an item. An attribute precedes the item to which it is attached and is specified by enclosing its constructor inside square brackets. For example, here is how NoteAttribute can be associated with a class:
[NoteAttribute("This class uses an attribute.")]
class UseAttrib {
// ...
}
This constructs a NoteAttribute that contains the comment “This class uses an attribute.” This attribute is then associated with UseAttrib.
When attaching an attribute, it is not actually necessary to specify the “Attribute” suffix. For example, the preceding class could be declared this way:
[Note("This class uses an attribute.")]
class UseAttrib {
// ...
}
Here, only the name Note is used. Although the short form is correct, it is usually refer to use the full name when attaching attributes, because it avoids possible confusion and ambiguity.
Obtaining an Object’s Attributes
Once an attribute has been attached to an item, other parts of the program can retrieve the attribute. To retrieve an attribute, you will usually use one of two methods. The first is GetCustomAttributes( ), which is defined by MemberInfo and inherited by Type. It retrieves a list of all attributes attached to an item. Here is one of its forms:
object[ ] GetCustomAttributes(bool searchBases)
If searchBases is true, then the attributes of all base classes through the inheritance chain will be included. Otherwise, only those classes defined by the specified type will be found.
The second method is GetCustomAttribute( ), which is defined by Attribute. One of its forms is shown here:
static Attribute GetCustomAttribute(MemberInfo mi, Type attribtype)
Here, mi is a MemberInfo object that describes the item for which the attributes are being obtained. The attribute desired is specified by attribtype. You will use this method when you know the name of the attribute you want to obtain, which is often the case. For example, assuming that the UseAttrib class has the NoteAttribute, to obtain a reference to the NoteAttribute, you can use a sequence like this:
// Get a MemberInfo instance associated with a
// class that has the NoteAttribute.
Type t = typeof(UseAttrib);

// Retrieve the NoteAttribute.
Type tRemAtt = typeof(NoteAttribute);
NoteAttribute ra = (NoteAttribute)
Attribute.GetCustomAttribute(t, tRemAtt);
This sequence works because MemberInfo is a base class of Type. Thus, t is a MemberInfo instance.
Once you have a reference to an attribute, you can access its members. Thus, information associated with an attribute is available to a program that uses an element to which an attribute is attached. For example, the following statement displays the Note field:
Console.WriteLine(ra.Note);
The following program puts together all of the pieces and demonstrates the use of NoteAttribute:
// A simple attribute example.

using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.All)]
public class NoteAttribute : Attribute {
string strNote;
public NoteAttribute(string comment) {
strNote = comment;
}

public string Note {
get {
return strNote;
}
}
}

[NoteAttribute("This class uses an attribute.")]
class UseAttrib {
// ...
}

class AttribDemo {
public static void Main() {
Type t = typeof(UseAttrib);

Console.Write("Attributes in " + t.Name + ": ");

object[] attribs = t.GetCustomAttributes(false);
foreach(object o in attribs) {
Console.WriteLine(o);
}

Console.Write("Note: ");

// Retrieve the NoteAttribute.
Type tRemAtt = typeof(NoteAttribute);
NoteAttribute ra = (NoteAttribute)
Attribute.GetCustomAttribute(t, tRemAtt);

Console.WriteLine(ra.Note);
}
}
The output from the program is shown here:
Attributes in UseAttrib: NoteAttribute
Note: This class uses an attribute.
Locations of visitors to this page