Monday, April 15, 2013

Partial Methods - C# 3.0

I was going through MSDN site and I came to know that there is a new features of C# 3.0 called Partial Methods.


Partial methods enable the implementer of one part of a class to define a method, similar to an event. The implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method. Therefore, any code in the partial class can freely use a partial method, even if the implementation is not supplied. No compile-time or run-time errors will result if the method is called but not implemented.
Partial methods are especially useful as a way to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method. Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.


See the below example :

TestClass1
{

   partial void DoSomething();

}

Testclass2
{
   partial void DoSomething()
   {
      // Some Code
   }
}

Friday, April 12, 2013

Inroduction to F#

Hi Friends!!

Sometime back I was going through the Microsoft's research site Channel9.

There I read about F# (FSharp). I was little bit curious about it.

So, I went through some sites and came to know what F # is all about.

Here, I am sharing little knowledge about what is F# and where it stands in today's ever changing development world.


F# is a statically typed functional programming language that targets the .NET framework. It shares a common core language with OCaml, another popular functional programming language, and draws ideas from many other programming languages, including Haskell, Erlang, and C#. In a nutshell this means that F# is a programming language that has a nice succinct syntax that feels a bit like scripting as we are able to execute the code interactively but has all the type safety and performance of a compiled language.
F#  is  a scripted/functional/imperative/object-oriented programming language that is a fantastic basis for many practical scientific, engineering and web-based programming tasks.

Some of the nice features of F# are:

  • Interactive scripting like Python,

  • The foundations for an interactive data visualization environment like MATLAB,

  • The strong type inference and safety of ML,

  • A cross-compiling compatible core shared with the popular OCaml language,

  • A performance profile like that of C#,

  • Easy access to the entire range of powerful .NET libraries and database tools,

  • A foundational simplicity with similar roots to Scheme,

  • The option of a top-rate Visual Studio integration,

  • The experience of a first-class team of language researchers with a track record of delivering high-quality implementations,

  • The speed of native code execution on the concurrent, portable, and distributed .NET Framework.

F# compiler can be downloaded from http://research.microsoft.com/fsharp/release.aspx.

"Get updated every time because a Change is the only thing constant in this world. "

Thursday, April 11, 2013

Generic Singleton C#

Hi Friends,

In my previous thread I discussed about Singleton design pattern with examples.

.NET framework 2.0 has got many new features like Generics, Nullable types, Partial Classes and many more.

Generics are most discussed features of 2.0 framework.

So, now the idea comes that can we have generic singleton class which will return a single object of provided class?

The answer is Yes. We can achieve a generic singleton class using generics.



Here, is how it goes..

class SingletonProvider where T : new() 
{

internal static readonly T instance = new T(); 

public static T Instance 
{

get { return instance; } 
}

static SingletonProvider() { } 


}



Here, I have used static constructor this is to provide both the thread safety as the lazy instantiation.

Test the above SingletonProvider with following programs.



1) Create a one Employee Class.


class Employee

{



string lName; 
string fName;

public String LName 
{

get 
{ 

return lName; 
}

set

{

lName = value; 
}

}



public String FName 
{

get

{

return fName; 
}

set

{

fName = value; 
}

}

public void PrintName() 
{

Console.WriteLine("First Name : " + this.FName + " and Last Name : " + this.LName); 
}

}



Here, I have created an Employee class with two properties LName, FName and one method PrintName().

2) Now, Its time to test SingletonProvider .

I have created following class to test the program.


class TestGenericSingleton

{

static void Main(string[] args) 
{

SingletonProvider.Instance.FName = "James"; SingletonProvider.Instance.LName = "Bond"; 
SingletonProvider.Instance.PrintName();

Console.ReadLine(); 
}

}

The output will be :

First Name : James and Last Name : Bond .

In this way we can create a generic singleton object.

Wednesday, April 10, 2013

Grails Plug-in Development “Jmesa”



Contribute back to Grails Developer Community

Sigma Infosolutions - a global IT and Product Engineering company is continuously growing towards creating standards and going beyond satisfaction to delight their clients.They are constantly updating themselves and developing Extensions/Plugins/Custom Modules to fulfill their patron’s need. They are 100% determined to accomplish their vision as they had planned.

Their Grails team has recently received a Milestone and Completed the Development of Plugin for Jmesa in Grails. This is not an end for Sigma Infosolutions’ Grails team but a beginning and in coming days you can see more of this from their Grails Developers contributing towards Grails development ecosystem.

Description

Overview
Jmesa plugin is a dynamic html tabular representation of data that allows you to filter,sort, paginate, and export your data.

The Plugin allows Grails applications to easily integrate with the functionality. Utilizes Jmesa feature as an underlying mechanism so serves managing representation of data in tabular form, filter,sort, paginate, export your data. We recommend you read the Jmesa documentation and understand how this plugin operates before you start using this plugin.
Concepts
Sort: Allow sorting feature based on Domain class field name.
Paginate: Provide the number of record need to be display.
Export: Provide export data in PDF and XLS format.
Filter: Allowing filtering of data based on domain class field name.
Installation
Install the plugin by using the below command or download the plugin and install by pointing to the path of the file where the plugin.zip is located.http://www.grails.org/plugin/jmesa
grails install-plugin jmesa

About Sigma Infosolutions:
For the last 8 years, Sigma Infosolutions’ Grails development team is using Grails framework that offers new levels of productivity. Sigma Infosolutions' Grails Development team seamlessly integrates with your existing processes and work-flows to work with data as non-intrusively as possible. They use agile methodologies and develop high quality, easy to use applications in small time that meets every user's expectations and requirements. Sigma Infosolutions is a TUV Certified ISO 9001:2008 and 27001:2005 certified company.
Besides Grails, Sigma Infoslolutions’ also has expertise in Spring, Struts, Hibernate, JSP/Servlets, Java Beans (EJB), AWT/Swing and more.
For Groovy and Grails application development you can visit their website  http://www.sigmainfo.net/grails-development/

Singleton Design Pattern C#

Design patterns are recurring solutions to software design problems we find again and again in real-world object oriented application development.

Design patterns are there to address a variety of design problems, from small issues to large, architecture-level problems.

Here, I am starting a thread which explains different categories of design patterns and their usage.

Design patterns are not language specific. Here I have tried to use C# to demonstrate design patterns examples.

Design patterns are categorized in three groups: Creational, Structural, and Behavioral.

I will be using following format to explain design pattern to make it consistent across all patterns.

Name and Category: Describes the name of the pattern and category of pattern (Creational, Structural, or) Behavioral).

Intent: It describes what kind of design problem this pattern addresses.

Motivation: A scenario that illustrates a design problem and how the class and object structures in the pattern solve the problem.

Consequences: It will describe the trade-offs of design pattern.

_________________________________________________________________________________________________________________

In this article we will have a look at Singleton design pattern.

Name and Category: Singleton (Creational Pattern)

Intent: Ensure a class only has one instance, and provide a global point of access to it.

Motivation: Sometimes we want just a single instance of a class to exist in the system
For example; we want just one window manager. Or just one factory for a
family of products.


Let us look into implementation.

How can I make only one instance of my class?

First, we will create a private constructor of our class.

When constructor is a private then object of that class can not be created at all.

Here, we are restricting user from creating an instance of a class. But as per our requirement we need to have one and only one constructor.

In order to achieve that, we will create a one static method which in turn returns an object of class.

Here, is how it goes:

// Class : Singleton.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern
{
class Singleton
{
private static Singleton singleton = null;

/**
* Returns a reference to the single instance.
* Creates the instance if it does not yet exist.
* (This is called lazy instantiation.) 
*/
public static Singleton Instance()
{
if (null == singleton)
singleton = new Singleton();
return singleton;
}
/** The Singleton Constructor.
* Note that it is private!
* No client can instantiate a Singleton object!
*/
private Singleton()
{
}
}
}



Here, we have a static method Instance() which takes care of single instance.

In this method first we check whether the static private variable singleton is created or not.

If it is null it will create a constructor.

If it is not null it will return the existing one.

So we are sure that only one instance of our class will be created.

Now let us think in different way. If two threads are trying to access the Instance () method at the same time what will happen?

In that case we will be having two instance of our class> It means our class is not a singleton. right??

So, how can we achieve singleton object?



// Class : Singleton.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace DesignPattern
{
class Singleton
{
private static Singleton singleton = null;
// Lock synchronization object
private static object syncLock = new object();



/**
* Returns a reference to the single instance.
* Creates the instance if it does not yet exist.
* (This is called lazy instantiation.)
*/
public static Singleton Instance()
{
/* Support multithreaded applications through
* 'Double checked locking' pattern which (once
* the instance exists) avoids locking each
* time the method is invoked
*/
if (null == singleton)
{
lock (syncLock)
{
if (singleton == null)
{
singleton = new Singleton();}
}
}
return singleton;
}
/* The Singleton Constructor.
* Note that it is private!
* No client can instantiate a Singleton object!
*/
private Singleton()
{ 
}
}
}


This will make sure that only single instance of class will be created.

Consequences:

Benefits: 1) Controlled access to sole instance.
Software Development Blogs - BlogCatalog Blog Directory RSS Search Technology Blogs - Blog Rankings Blog Directory