søndag, januar 14, 2007

Using the ObjectDataSource with Singleton and/or Interface instances

When using Visual Studio 2005 to configure the ObjectDataSource, you will only be presented the option to bind against classes that implement the class System.Web.UI.WebControls.ObjectDataSource. The ObjectDataSource will pr. default create its own instance of your ObjectDataSource class using reflection and use this that instance to retrieve objects that are to be bounded to you data control (for example the GridView).


As a consultant I often see developers having their own data retrieval classes, but when they want to use them in an ObjectDataSource, the class is wrapped in another class implementing the System.Web.UI.WebControls.ObjectDataSource only to satisfy Visual Studio. This leads to twice the complexity – and replication of code. The technique cowered in this blog post might be trivial to some, but I believe that the point cannot be made clear enough.


A better approach is to configure the ObjectDataSource tag to make use of the already present data retrieval class and tell the ObjectDataSource to use exact the instance of you class that you want it to use. This can be necessary if you have decided to only expose interfaces to you client, or if you data access class is implemented as a GoF Singleton or Factory design pattern (thus not letting the ObjectDataSource create an instance of your data retrieval class).


<asp:ObjectDataSource ID="_transactionData" runat="server"
    
TypeName="MailMonitorServer.Locator.ITransactionLocator"
    
DataObjectTypeName="LinkTrackerServer.ITransaction"
    
SelectMethod="Find" 
    
OnObjectCreating="ObjectDataSource_ObjectCreating" 
    
OnSelecting="ObjectDataSource_Selecting" 
    
EnableCaching="false" >
</asp:ObjectDataSource>


This code shows how I use the ITransactionLocator to locate transactions. The thing to notice here is that the ITransactionLocator (TypeName attribute of the ObjectDataSource) is in fact an interface (we cannot create instances of it from the client application) and the only way of getting an instance of this interface is through a Singleton pattern (more about that later).


The DataObjectTypeName defines that the type of the objects that are returned by the ITransactionLocator are of type ITransaction. In order to instruct the ObjectDataSource to use exactly your instance of the ITransactionLocator, and therefore not use reflection to create its own instance, I hook into the OnObjectCreating event.


protected void ObjectDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e) {
    
e.ObjectInstance = InstanceHelper.Instance.TransactionLocator;
}


Here I assign the singleton instance of my locator class to the arguments ObjectInstance property. This way the ObjectDataSource uses only this instance.


In this case the ITransactionLocator’s Find method needs a special parameter (not covered by the standard <selectparameter> tags. To customize the parameter sent to the Find method I hook into the OnSelecting event.


protected void ObjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) {
    
IDealer dealer = (IDealer) Session["dealer"];
    e.InputParameters.Add("account", dealer.Account);
}


In this manner I also have complete control of the parameters that are sent to my ITransactionLocator.


I hope this little code will give you a new idea of now flexible the ObjectDataSource implantation really is.

tirsdag, november 14, 2006

Visual Studio 2005 Build Performance

I had some performance issues with my Visual Studio 2005, where one of my C# ASP.NET web sites simply took forever to build and compile. I knew that there was an issue in VS 2005 with large Visual Basic projects due to a compiler error in the Visual Basic compiler(http://blogs.msdn.com/webdevtools/archive/2006/07/24/677180.aspx) but I am not using VB!

Then it hit me, ASP.NET web sites can run with both C# and VB code mixed so the Visual Basic compiler must run each time you build your website. To test this, I simply removed Visual Basic from my installation (seldom use it anyway) and bang ! – My website builds lightning fast again.

The lesson learned is this:
- Performance problems with compiling your website?
- Remove Visual Basic from your Visual Studio installation! (or apply the MS hotfix)

Resume of a lecture

I attended Anders Hejlsberg’s guest lecture at ITU Copenhagen last Friday and want to share with all of you the information he revealed.

First of all – it was a great lecture. He showed how all the language features of C# 3.0 together made LINQ possible and explained how they implemented LINQ using every one of these new language features. I must say that C# 3.0 is innovation on a high level, surely a something I will be waiting for with anticipation.

Besides from a great lecture, Anders revealed some information I want to share with you. (I hope MS don’t mind)

Concurrent Programming
The next step, after the release of C# 3.0 and LINQ – the language team will probably focus on concurrent programming. Anders believes that the available constructs for concurrent programming simply are to complex for the average programmer (Threads and so fourth) Therefore they will look at how they can improve the runtime and/or the language to better support concurrent programming, making it easier for developers to take advantage of multiple CPU’s that are getting more and more common in standard PC.

Release of C# 3.0 and LINQ
Anders revealed that the language team currently is improving some details of the compiler for C# 3.0 and we (the general public) will have a beta version of the next Visual Studio codename “Orcas” in about six months. He also ensured that there will be no more CTP – only Betas from now on.

UPDATE 2007-03-09
-----
Seams that Anders Hejlsberg was wrong :-)Last week a new CTP (March CTP) was released after all. I wonder if the Beta still will be available six months after the lecture ?
------------

Last note
There will finally be a Set collection implementation available in Orcas. Jubii.

tirsdag, oktober 31, 2006

Anders Hejlsberg in Copenhagen (IT University)

I just learned from my former professor (Peter Sestoft) that Anders Hejlsberg will host a guest lecture at ITU, Copenhagen on November 10th at 14:00 hours.

As you probably already know, Anders is the man behind C# and this lecture will contain a walkthrough of the new language features of C# 3.0 as well as his reasons for including these language features in this future release of C#.

C# 3.0 will include really cool features like:
- Implicitly typed local variable declaration: (for example var i = 42)
- Extension methods (the possibility to extend existing types with additional methods)
- Lambda expressions (cool SML like syntax for methods: x=> x + 42 )
- Greater type inference
- Object and collection initializers ( shorter syntax for creating instances)
- Anonymous types (like: new{Name = “Runi”, Language=”C sharp”})
- Implicity types arrays
- And the LINQ (declarative type safe access to query relational and hieratical data)

To register for this FREE event, register here:
http://www.microsoft.dk/MSDN/Hejlsberg@ITU

I will surely be there - see you all.

/Runi

fredag, oktober 27, 2006

Sitecore Instructur at last

I have yesterday reached the level of higher existence (or competence) - Sitecore V5 Certified Instructor, Level 2.

I just completed my first instruction in the Sitecore API course. It was actually quite giving and interesting to teach Sitecore on a lower level, surely something I want to do again.

Actually, if everything goes well, my next instruction session will be in Holland sometime late next month.

onsdag, oktober 25, 2006

Dont trust your debugger

I just waisted a couple of hours because of a strange behaviour in the Visual Studio debugger.
Consider the following code:

object
obj = new String('a',1); //"a"
string str = new String('a',1); //"a"
Console.Out.WriteLine(obj == str); //ln1
Console.Out.WriteLine(str == obj); //ln2
Console.Out.WriteLine((string)obj == str);//ln3
Console.Out.WriteLine(str ==(string)obj); //ln4
Console.Out.WriteLine(obj.Equals(str)); //ln5
Console.Out.WriteLine(str.Equals(obj)); //ln6

Now, add breakpoints in Visulal Studio on line 1 to 6 - and make the debugger evaluate the expression before it is printet to the console.


What would you (the debugger) expect the output to be? The Visual Studio Debugger says:
true
true
true
true
true
true


This is however not the case. The actual output is:
false
false
true
true
true
true


There is really nothing strange about the result. The == operator does not just call the Equals method blindly and virtually as one might expect. It is actually an operator that compares references unless overridden.

By default the == operator compares for reference equality. So if == tests for reference equality, this would explain the behavior of the result of ln1 and ln2, but not ln 3 and 4. This is because the == operator has been overridden in the String class to call the Equals method, thus comparing the value of the string rather than its reference. (this explains ln 3 and 4).

The runtime system determines which operator to call based on the runtime type (the type of the reference rather than the type of the instance). In ln 1 and 2 the == operator of the Object class is used, where in ln3 and ln4 the operator as defined in the String class is used.

So much for the == operator. The strange thing however, is that the Visual Studio debugger does not seam to get this. (VS2003). I used a lot of time debugging an expression inside an 'if' statement, which evaluated to false (by the debugger) but the if - statement - body was entered anyway. Grrr – I’m a bit pissed at the debugger.

torsdag, august 31, 2006

Media Library Posts Removed

Sitecore has asked me to remove my Sitecore 5.3 Media Library Part 1 - 5 blog posts which gave a preview of the Media Library in Sitecore 5.3 beta because of changes in the final version. I will of course honor this request, whereas these posts no longer are available. For info on Sitecore – contact Sitecore