Monday, July 21, 2014

Anonymous types were introduced in c#3.0 mainly to support LINQ. But it has find its good use in other places also like in HTMLHelper classes available in MVC.

Anonymous types are classed which do not have any name assigned to them. They only have properties defined and initialized then and there only.

Anonymous types are not public.

Friday, July 18, 2014

Extension Methods in c#


If one wants to extend a property of a class without modifying the existing code of class and compile it again one can use Extension methods concept which has been introduced in .Net 3.5 and C#3.0.

Previously if one wants to extend any particular class one had to inherit that class and create a new class and then give its implementation but using extension methods one can easily extend functionality of an existing class.

To explain the concept better I will take example. Lets say I have a following class BookMyShow which provided different methods to provide booking of different shows. On top of that this class is sealed which means this class cant be inherited further which means in future if I want to extend the functionality of this class I need to have either access to the source code of this class and then change the source code and compile it and then re-distribute the same..........ufffffffffffffffff.... lot of things to do.

namespace BookingPlaza
{
    class BookMyShow
    {
        public bool Book(string[] strppl)
        {
            /// ;;.......
            return true;
        }

        public int CancelBooking(string[] strArrBook)
        {
            return 0;
        }

        public DateTime GetShowBooking(string strWhen)
        {
            DateTime dtm = new DateTime();
            switch (strWhen)
            {
                case "AM":
                    {
                        DateTime.TryParse("10:00 AM",out  dtm);
                        break;
                    }
                case "PM":
                    {
                        DateTime.TryParse("10:00 PM", out dtm);
                        break;
                    }
                default:
                    DateTime.TryParse("03:00 PM", out dtm);
                    break;

            }

            return dtm;
        }
    }
}


Here comes the extension method to rescue.

Using extension method one can extend the functionality without source code access of the type to be extended

namespace ExtensionOfBMS
{
    static class ExtendBookMyShow
    {
        public static bool AdvanceBooking(this BookMyShow objBMS, DateTime timings)
        {
            return true;
        }

    }
}

As you can see above we extended the BookMYShow class but you might be wondering how this happened as I was so whole magic lies in the first parameter of the static method.

So first I will explain some syntax:

1. You need to create a static class having static method which will have extended functionality of the type to be extended.
2. Here comes the magic ;) . First parameter of the function should be type to be extended with this modified specified. As in this case it was BookMyShow.
3. Now where ever you want to use it you just need to put using directive. Like using ExtensionOfBMS.; and then you can access the extended method just like a instance method. As you can see in the screen shot AdvanceBooking is available as the extension method.




I would say amazing thing in this extension method implementation is that a static method is just available as instance method of the class when IL code is generated its actually static class call to static methods.

One more thing to point here is binding. If you create a same method as extension method which is already defined in the class and when you call this extension method it will always going to call the type implementation instead of the extended one. So for example I try to create one more extension method having same signature as already existing method compiler will always pick base class implementation.

Extension methods cant access the private methods or variables of the type or class which they are extending hence encapsulation is still intact in the design.

Keep extending!!!!!!!!!!!!!!!!!!!!!!!

Thursday, July 17, 2014

Impersonation - A holistic approach


What is impersonation?
When you try to execute current request in a different user's context instead of default one so that means you are impersonating.

Any windows process runs under a windows identity assigned to that process or a user account under which a process executes.So all permissions applicable to that identity as per that only process can execute.
 In Asp.Net the process runs under its ASPNET account. It has limited privileges. So if sometimes if we want to access resources which are not allowed to be accessed by default windows identity account under which that process is running we might have to use windows account credentials which have access to the resource to be accessed. So running the process under user provided authenticated account instead of default account is called impersonation.

Impersonation can significantly affect performance and scaling. It is generally more expensive to impersonate a client on a call than to make the call directly.

One can set impersonation through IIS at application or page level using Authentication feature.
There are scenarios where you dont want to set impersonation at application level and dont want to share your credentials with the site administrator for security reasons. In that case developer have option where he can change the windows account context which is be default Network Service or ASPNET account to user users domain account.

Following blog entry explains very clearly how to impersonate programatically. In this author has nicely explain the caveats also. http://weblog.west-wind.com/posts/2005/Feb/24/Using-programmatic-Impersonation-from-an-ASPNET-Page

As shown in this link one has to use interop to call Win32 LogonUser and CloseHandle functions. We have to use interop because .NET doesn’t provide the equivalent methods.

So.... IMPERSONATE!! ...but only when required :)!!

Wednesday, July 16, 2014

Awesome sayings

Scott Gu's BOss says:

"Guy walks into support, sez he needs a bigger mobile phone antenna. Doe he need a bigger antenna or does he really want better reception? Don't let your users dictate your solution with their statement of the problem."

Tuesday, July 1, 2014

base64 encoding and decoding

There are numerous instances where we want to serialize or de-serialize our object. In order to do the same different type of serialization techniques are being user like binary serialization, xml serialization, json serialization.

Recently I came across base64 serialization. Using this approach you can serialize your object as a base64 text string which means your object will be converted into string of characters represented by string.

Also same set of string can be de-serialized back to object.

This serialization can be used where we are not sure of the transport medium or protocol which will be used to transfer the data. Since text are treated as text so its safe to send it as base64 encoded object.

Encoding defined how a character is stored on your hard disk. Mostly Unicode encoding is used since it defines all the characters any language can have under the sky. So if we are using UTF for decoding or encoding that means you are able to interpret that character and now just to show the same you need fonts of that language.