Zeeshan Hirani

  • Deploying assemblies to GAC

    This topic may not sound too interesting too people because it is an old story. Basically in order to store anything in gac you have to strongly name an assembly. I don't  happen to put assemblies in GAC too often so I simply forget the process of strongly naming an assembly. This time around I decided to write a blog post so that I would always have something to look back into.

  • Initializing Controls in CreateChildControls

    Once again in my adventure of writing custom controls I have discovered that if you are overwriting custom CreateChildControls to add your own controls to the control collection, you should initialize the controls before adding them to the control collection.  Here is an example where I am setting properties on the link button after I add them to the controls collection. 

  • Using RenderBeginTag

    At my current work, I had my share of custom controls that I have to write. Basically I started to write my html by overriding the render contents. I had been using the old style RenderBeginTag and than add my styling by calling write attribute. It is nice but the problem that I have with is at the end I have to know which tag to close which is sometimes a pain. Therefore when I found this new method called RenderBeginTag, I thought it was nice because when I have to close the tag I simply have to say RenderEndTag and it automatically figures out the tag I have to close. Here is a simple code showing two different ways of doing it.

  • Overriding ConnectionString in DataContext

    Today at work, one of my co-workers mentioned an issue that he was loosing ConnectionString every time he would modify the linq to SQL model in the dbml file. What he was essentially doing was getting rid of the default constructor and adding a partial class where in the default constructor based on business rules, he would fetch the right ConnectionString either from web.config, app.config or some other xml file. Obviously the problem here was, you cant have two default constructors. Because every time you would modify the dmbl file any changes you have made to the designer.cs would get lost. But a simpler solution to this would simply be creating your own partial DataContext class and adding the OnCreated partial method which gets called in the default constructor. Here is a simply implementation where I am reading the ConnectionString from web.config ConnectionString section.

  • Working With Expression Trees

    Now I am not going to be talking about expression trees in depth. I will be covering expression trees at basic level. The reason I started playing around with expression tree was not to write a provider but basically to be able express my intent at runtime. Linq is so much at compile time that sometimes it is not feasible for dynamic queries. This is where you can use expression trees that allows you define your intent based on runtime decisions.

  • ConvertAll delegate

    Today I had the need to convert a list of objects in a collection to a list of different objects in a collection. Although its a pretty simple use case which would not require too much manual code to do it. But surely enough I got introduced to a build in method Convert that is available on List<T>. Convert pretty much tells you by name what its purpose is. It converts one collection to another collection by taking a delegate which specifies how to to transform one object to another. Here is an example that shows how to use ConvertAll.

  • Implicit Arrays

    One of new features in C# 3.0 that I am really fond of, is not having to declare what type of array it is. It starts by checking all the values in the array and seeing if they can be implicitly converted to one type. If it can, than it is considered the array type.

  • Var cannot be used as instance level variable

    Today I was playing around with Var and I decided to try to implicitly declared some of my instance level variables. Unfortunately compiler raises an error saying Var can only be used in the scope of local variable. Here is an example.

  • Unique Behavior when querying using primary key column in linq to SQL

    I was working on a query earlier in the day when I came across this behavior when searching for a record using primary key in linq to SQL. Basically when you query for an object the first time using linq to SQL, datacontext caches the object in its tracking service. Therefore next time around when you query for the object again, it retrieves the modified object from the cache. It is worth noting that if you are querying based anything that is not a primary key column than linq to SQL would make a database call the second time around as well. After completing the database call, it will than check with the data tracking service to see if that object is available in its cache. If it is available, it will return the object from its tracking service. However when you are querying based on primary key column, it does not bother making the database call, it simple ask the object from the tracking service first and if it is found it simply returns it. If the tracking service does not have the object than linq to SQL goes and makes a database.

  • Useful tip to infer types in Visual Studio.

    There are times when your linq query would return complex data. If you are not exactly sure what the return type is, than simply assign the result to var and hover over the var to see its exact type. Here are two examples that illustrates the beauty.