Monday, April 28, 2008

Sorting Performance of Dynamic Linq

I have really just started scratching the surface of Linq & C# 3.5. One thing that I was instantly dissapointed was how the sorting is implemented. In my web application all of our grids are sortable and prior to Linq I wrote a little IComparer implementation which uses reflection to access any property we need.

The issue I have with the linq implementation is that if the columns we are sorting on can change, I would have to have different lambda expression for all the possible permutations. This is not acceptable.

The Dynamic Linq Library is included in the C# Linq Samples. I highly recommend looking into the dynamic library. It allows for the lambda expressions to be expressed using a very easy to learn syntax, which is stored as a string. This allows you at runtime to easily build dynamic expressions for example:


lines.AsQueryable().OrderBy(field + " " + direction.ToString());




How does this perform compared to the reflection based implementation? I have not done a full analysis and only have time for a quick look at it but from what I have it is not to bad. For the record I am running a 2.5GHZ Dual Core Intel with 3 GB of Ram running on Windows Vista. We will be sorting 100 classes in a List<> object. The classes have two integer properties which were randomly seeded.

As expected the first pass through the code is the most expensive but how expensive is it?

  • Dyanmic Linq: 15.5 MS
  • Reflection: 5.5 MS
  • Linq Query: .87 MS

This is expected since the Dyamic Linq is building the expression trees. If your only going to sort a list once then going with reflection here might be more appropiate due to the 300% overhead of the dyanmic query; however, what happens if you end up sorting the same collection 100 times. For this test, I decided to sort the same list each time only I would swap between two different integer properties.


  • Dynamic Linq: 26 MS
  • Reflection: 195 MS
  • Linq Query:1.87 MS


I was shocked by this. I had thought that by changing the dyanmic expression it would have to re-initalize itself. Looks like I was wrong. A normal linq query is definetly the fastest approach; however, I feel that due to its inflexibility, the dynamic approach is a better aproach.

Friday, April 25, 2008

Web Deployment Projects & Updateable Sites

I am growing very fond of the Web Deployment Project for Visual Studio. I have found it to be an integral piece of my web site; however, I have just ran into a small gotcha which is related to the Allow site to be updated option (Note: This option is not specific to the web deployment project but a part of the ASP.Net compilation model).

One of my web pages is a highly customized printout. I leveraged server side code in the .aspx page to simplify which elements are displayed take the following html fragment for example:



<%if (DisplayAllines)


{%>


<div style="float: right">


<asp:Table ID="lines" runat="server" />


</div>


<%}


else


{ %>


<div style="float: right">


Grand Total:


<%=Bid.GrandTotal().ToString("C") %>


</div>


<%} %>





When you publish your site as updateable, the server side code here is not compiled. I had accidently used the wrong capitlization for an abbreviation in simillar code, and while it compiled without a problem, it would not run.

If you turn off updateable site feature, and run a build, Visual Studio does catch the syntax error.

On a side note I wish Microsoft would better support server side scripting tags. I use these a lot in some of the javascript for my user controls (Particulary if the control will exist multiple times on the same page), as a way to quickly and easily distinguish btw javascript objects. For example:



var <%=this._gridAssets.ClientID %>GridSelection;


var isError=false;


function onLoad<%=this._gridAssets.ClientID %>Grid(sender,eventArgs)


{


    <%if(!ReadOnly){ %>


    <%=this._gridAssets.ClientID %>GridSelection=new selectedGrid(sender,4,null);


    <%} %>


}




In this example here, I have a custom javascript object which is associated with a grid in this user control. I use the ClientId of the grid to act as a namespace. This same trick is also handy if you need to access server side controls from javascript and those controls will exist in a naming container.

When you do this you will be flooded with syntax warnings as the parser is trying to treat the script directives as client side script. Intellisense almost never works here either (It will work outside of a <script> tag).

Enjoy!

Monday, April 14, 2008

Rebuilding Indexes in SQL2005

I have been developing on Microsoft's SQL Server since SQL 2000; however, I always had the luxury of having a dedicated IT team who was in charge of managing the production environments. Now that I am working for a startup company and am the sole developer, build engineer, and production team, I have to handle this aspect of my job.

I ran into what I will call a bug in SQL 2005 that I can't believe has made it into SP2. And while as you will see this bug is very minor it is a good example, of a poor design of a management tool and error handling.

I am setting a new nightly maintneance plan and set it up with the following steps:
  1. Check Database Integrity (Right now our DB's are so small that I am going to do this nightly but some people recommend weekly is sufficent)
  2. Rebuild Indexes
  3. Full Backup of Databases
  4. Cleanup Old Backups
  5. Notify Operator

I go ahead and run the backup, and I notice my databases are backuped to the location I specify, yet the UI tells me that the job has failed. I open up the job details and I see the following error message:

Date 4/14/2008 9:30:06 AMLog Job History (Nightly
Maintneance)
Step ID 1Server XXXXX
Job Name Nightly Maintneance
Step Name Subplan
Duration 00:01:27
Sql Severity 0Sql
Message ID 0Operator Emailed Operator Net
sent Operator Paged Retries
Attempted 0
MessageExecuted as user: XXXXXX
The package execution failed. The step failed.



All this is telling me is that a step failed. It provides no indication of what failed or why. I brough up the report from the maintneance package it told me to check the SQL Agent Job history to find out why it failed. Which as I showed is not helpful.

So I resorted to the only option I felt I had, I disabled every step and ran each one building upon each other. This is when I noticed the issue. I had setup the Rebuild Index task to "Keep index online while rebuilding" option selected.

Online indexing is a new feature in SQL 2005 that helps improve your uptime since you can do maintneance on your indexes without taking them offline. So I had though let's select this option.

This option doesn't work for my environment because we are using SQL Server Standard edition. Here's my gripe about the error handling here. Why didn't the error messages indicate that the rebuidling indexing failed? Better question is: Why is this package even allowed to be created?


Editors Note
Since writting this I have discovered that better error information is available in MSSQL\LOG directory. While this is useful, and in fact in this case it does say exactly the problem, I feel my post still holds true. The SQL Management Studio should have provided me better error information, or even better disabled that option entirely.

Thursday, March 20, 2008

Changing registration information in Windows Vista

I recently aquired a new HP laptop which I am fairly happy with. One thing that has been annoying me is the registered organization is for some reason set to HP. This is a minor annoyance but it has been nagging at me for over a month. So I set out to find where this is stored.

This setting is stored in the registry at the following key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion





The value is stored in the RegisteredOwner & RegisteredOrganization settings (See the screenshot above). You can double click on these to edit the information.

Enjoy!

Josh

Tuesday, March 11, 2008

Using the SerializationBinder for versioning

Normally when building an application I am a strong advocate for storing long term persistent data in a relational database such as SQL Server; however, with all things there always exceptions.

I am currently working on an application, that has at its core a complex object graph, with several levels of inheritance, and all sorts of references between the internal objects. At some point in the future I will be storing this object in normalized tables inside SQL server; however, because this project started out as a prototype which was loosely defined, and due to the wide variety of references within this object (And the undefined requirements of what it contains and how it works). I opted to store the object as a blob in the database.

This has been a huge win from a productivity standpoint since we can add all sorts of things to the object without worrying about the persistence tier. The negative of this comes when we want to change something. The .net framework has several hooks in place that help you modify an object which you using binary serialization with. One of these hooks is the SerializationBinder.

The SerializationBinder provides a means for you to do type replacement. Say for example you are building version 1 of an application. You spend hours designing testing and implementing it, and you ship the application, and your customers love the application and start asking for version 2. Unless you are superman you will probably not have designed everything ideally to support the features in version 2 (And if you did drop me a line, I have a job for you!).

I found myself in a similar situation, where I defined an object called MyCompany.Domain.Note. During our version 2 implementation, I realized that this shouldn't be a MyCompany.Domain.Note but instead needs to be a MyCompany.Domain.Widget.Note. So I change the class around, and run the application. This will result in all sorts of SerilizationExceptions since the Serializer is looking for MyCompany.Domain.Note which no longer exists.

This where the SerializationBindercomes into play. The SerializationBinder is an abstract class so you need to define a new class.


using System.Runtime.Serialization;
public class WidgetSerialzationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
}
}


Implementing this class is very simple. In our case we want to make sure anytime we see MyCompany.Domain.Note we replace it with MyCompany.Domain.Widget.Note. Place this code in the BindToType method.


if (typeName.Contains("MyCompany.Domain.Note"))
{
typeName = typeName.Replace("MyCompany.Domain.Note", "MyCompany.Domain.Widget.Note");
}
return Type.GetType(String.Format("{0}, {1}",typeName, assemblyName));


The reason we are using contains/replace on the typeName is that we might know for sure what the typeName should be. For example if you have a List<mycompany.domain.note> you will need to change it to List<mycompany.domain.widget.note>. The actual type name in this case is: System.Collections.Generic.List`1[[MyCompany.Domain.Widget.Note, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

To use the binder you will need to set the Binder property on your BinaryFormatter. For example:


 public static object Deserialize(byte[] data,SerializationBinder binder)
{
object obj;


MemoryStream streamMemory = new MemoryStream(data);
BinaryFormatter formatter = new BinaryFormatter();

formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
formatter.Binder = binder;
obj = formatter.Deserialize(streamMemory);
return obj;
}


Enjoy!