Wednesday, August 25, 2010

Applications of Extension methods

Extension methods was introduced some time back with C#3.0 .The feature is great we can attach one more function to the existing classes and can call those methods with the object of those classes itself.It is very helpful for architects to write all the required methods and attach with the existing classes as well.

We all might have surprised by seeing so many methods suddenly in our favorite IEnumerable derived classes such as List and observable collection when the linq is introduced.Did they wrote all these methods inside the List class? absolutely not.Instead they wrote extension methods for IEnumerable and from the user point of view they looks like normal methods because the methods can be invoked  just by using the objects.

I was looking for some uses of the extension methods like this.The option is to replace all most all the helper classes by extension methods. While looking through the helper classes I struck around the reflection helper. The code was written to get the value of property using reflection.If the developer is familiar with the code base he probably knows that there is a helper library and can use it.But what about a fresher.He will surely struck on this and may think about changing the design.If he is coming from the true object oriented world where there is no chance to invoke private properties he is done. The GetProperty and SetProperty really comes helpful here.

Don’t feel so much complicated .Its so simple .Write 2 extension methods which internally gets and sets the value of properties by getting the name of property in string.Now it comes very easy to the end developer as the method will be shown when they type the magic key dot(.)

public static class MyExtensions 
{
    /// <summary>
    /// Gets the value of property mentioned in propertyName.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="obj"></param>
    /// <param name="propertyName"></param>
    /// <returns></returns>
    public static T GetProperty<T>(this object obj, string propertyName)
    {
        PropertyInfo pi = obj.GetType().GetProperties().FirstOrDefault((p) => string.Equals(p.Name,propertyName));
        T t = default(T);
        if (pi != null && typeof(T) == pi.PropertyType)
        {
            t = (T)pi.GetValue(obj, new object[] { });
        }
        return t;
    }
    /// <summary>
    /// Sets the value of property mentioned in propertyName.Returns true on success.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="obj"></param>
    /// <param name="propertyName"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool SetProperty<T>(this object obj, string propertyName,T value)
    {
        PropertyInfo pi = obj.GetType().GetProperties().FirstOrDefault((p) => string.Equals(p.Name, propertyName));
        if (pi != null && typeof(T) == pi.PropertyType)
        {
            pi.SetValue(obj,value, new object[] { });
            return true;
        }
        return false;
    }
}

Before using extension methods make sure that the assembly is referred and using the namespace.

SampleVM vm = new SampleVM();
vm.StringProperty = "Joy";
string s = vm.GetProperty<string>("StringProperty");
vm.SetProperty<string>("StringProperty", "Joymon");

Sunday, August 22, 2010

Maximum capacity specs for SQL server 2008

Recently I had to check the maximum capacity of SQL server for our project.The real question was how many tables SQL server can have? After a fine search I could see that there is no maximum limit for the tables alone.It is considered as a database object and the maximum number of database objects supported is 2,147,483,647. The database objects includes tables, sps,indexes,views ,triggers etc…

I was searching for SQL server 2008 and here is the link with more details.
http://msdn.microsoft.com/en-us/library/ms143432.aspx

Of course the tables are not created by hand, it is planned to be created through code.

Sunday, August 8, 2010

Handling certificate permissions

If you are using digital certificates to authenticate your application you might have encountered error messages related to certificate permissions.Mainly due to access permissions to the certificate files for the current user.Below is a link which explains how to manage certificate permissions.

http://stackoverflow.com/questions/1271497/asp-net-permissions-to-root-certificate-store