Friday, September 22, 2006

Have You ever wondered how you can add a link somewhere on your page, that will enable your users to submit your article to digg.com? It is rather easy to do it the simple way, by copying the digg link from digg.com, and putting it somewhere on your site for example as a hyperlink on some image. The digg url usually looks like this:

http://digg.com/programming/Microsoft_s_Unit_Testing_Framework_is_useles

Using it with image you get the following:

digg iconDigg This!

Most of the sites on the Internet currently provide you with the above. Is it perfect? I thought not, so I have searched for the better solution. What I wanted is to get the same digg link that is used on the digg.com. And after a while I have found few pages that explain how to do it. It's really easy. Just use the code below:

<div class="diggbutton">
<script type="text/javascript">
digg_url = 'http://digg.com/programming/Microsoft_s_Unit_Testing_Framework_is_useles';
</script>
<script type="text/javascript" src="http://digg.com/api/diggthis.js"></script>
</div>

And what you get is:

kick it on DotNetKicks.com

Friday, September 22, 2006 1:18:18 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, September 21, 2006

After almost two months I have decided to change the theme of my blog. The reason for this was simple: the previous theme was layed out using tables and that made it very difficult to make changes. Additionally I should now rank higher on Google since the structure of the documents is much cleaner. Time will tell.

Just for a reminder of how the blog looked like before:

Thursday, September 21, 2006 8:57:26 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, September 14, 2006

Every once in a while when I'm doing some WebServices I get to the point where I need to use the same class on both the server and a client. There are voices that say that you should not do it and that is against the idea of web services. In my opinion though it is not so! What I want to get is to use the same Data Transfer Object on both ends of the line. The reason for this is that I already have it defined in one place and there is no reason to define it once again IF I can reuse the implementation. Other clients - those that do not have the access to my classes may still use the definition from the WSDL language to generate their own classes - nothing is lost here.

The really good article explaining how and what you can do to make your life easier can be found here. A nice read and good to know stuff - really.

Now I wanted to give it a try. Unfortunatelly I was unable to deploy the solution to the PrivateAssemblies folder of the devenv.exe. I got an error when I tried add a web reference. Installing assemblies in the GAC helped.

Next step was to try to debug this stuff. This is actually pretty easy once you know the trick which is to use 2 instances of Visual Studio. One VS with the sources of the SchemaImporterExtension code and the other one which you will use to add web references. In the first one go to Debug->Attach To Process... menu item. Select Devenv.exe from the list (there should be one if you have only 2 visual studios) and attach to it. Now in the second Visual Studio you can add a web reference and... if you have a breakpoint somewhere in the first VS it should break the execution there. In the example file from the above mentioned site put a breakpoint on the line where you have: "public override string ImportSchemaType" and it should work :-).

You can debug almost anything in such a way - by attaching to the projects. What is not so obvious is that you can also attach to the Visual Studio itself. All you need is a second instance :-). This was pointed to me by some of the readers of my blog, when there was an issue of debugging ASP.NET BuildProviders.

kick it on DotNetKicks.com

Thursday, September 14, 2006 12:14:38 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 

Today I have encoutered a problem with debugging my web projects in Visual Studio 2005. The message was:

"Unable to start debugging on the web server. The server committed a protocol violation. Section=ResponseStatusLine".

I haven't changed anything in my software configuration since the beginning of the procject so I thought that something may be wrong with my IIS. I have searched the Internet for the error message and I have found the following article describing the problem. Basically it was Skype that made a conflict. I have done as the author suggests and it worked! Many thanks to Martin for investigating the issue. Just remember to start the IIS after changing options in Skype because it is now running (at least such was my case).

The strange thing is that I have allwasy have Skype running so why it conflicted today and not before? Strange...

 

Thursday, September 14, 2006 8:48:32 AM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Monday, September 11, 2006

Today I have tried creating unit tests using the brand new stuff from Microsoft - their unit framework that comes with the Visual Studio Team Edition. Now that was a major disappointment. I wonder how Microsoft comes out with a crappy stuff like that all the time.

The problem I have encountered is that it is not possible to use inheritance in your test classes. Take for example a base abstract class in which you define tests for your objects hierarchy. One of the method is an abstract method which returns a concrete object and the other methods are test methods as in the example below:

public abstract class BaseTest
{
[
TestMethod]
public void ConstructorParameterShouldBeSaved()
{
Guid id = Guid.NewGuid();
BaseClass obj = CreateInstance(id);
Assert.AreEqual<Guid>(id, obj.Id);
}

protected abstract BaseClass CreateInstance(Guid id);

}

Now I create another test class that inherits from the BaseTest and provides an implementation for the abstract method. Nothing an NUnit framework cannot do. But guess what? It is not supported!!! The Visual Studio complains about the TestClass attribute defined on abstract class - error UTA003.

That defeats all my desire to use MS technology so I switched to NUnit.

kick it on DotNetKicks.com

Monday, September 11, 2006 9:53:38 AM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [1]  | 
 Wednesday, September 06, 2006

For a long time now I have been aware of this feature but still I think many users of Internet Explorer do not know about it. The feature that I'm talking about allows you to save a complete web page INCLUDING pictures to a single file so it can be used later without having to connect to the internet. This feature is accessible from the File->Save As... menu after you select the Web Archive (mht file) as a type of the saved file:

Note that there is also another option to save an entire page using the same dialog box and selecting Webpage, Complete option. This however creates one file for the page and a folder containing the rest of the required files such as images. This is not a very ellegant solution in my opinion.

As good as this feature is, there are occasionally problems with saving the page and the only information is that there was some kind of error (don't we love this kind of messages?). Other than that I think you will love it :-)

BTW: I haven't seen such a feature in the standard installation of the FireFox but I suppose that there is some plugin just for that.

Wednesday, September 06, 2006 9:41:13 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Tuesday, August 29, 2006

Every now and then somebody just has to do it. They need to override the GetHashCode method either because overriding Equals method or for some other reason. Doing so, you should follow the guidelines provided to us by one and only Microsoft:

http://msdn2.microsoft.com/en-us/library/system.object.gethashcode.aspx

The main problem with guidelines regarding the GetHashCode method is that the change from time to time which causes some confusion.

The last change I have noticed is the fact that now GetHashCode can return different values for the same object if the state of the object that the GetHashCode relies on, has been modified. Previously GetHashCode allways had to return the same value for a given object (which caused it alomost impossible to override both the GetHashCode method and the Equals method and at the same time have a mutable object). The change to the rule makes it easier, but has some other implications...

The most important implication of having an object return a different hash code during its lifetime is the fact that no it is possible to put someting in the hashtable never to be seen again (at least not in a way we would expect it to). When this happens is when we put some value in the hashtable using some object as a key. Then we change the object so that it returns a different hash code and we have a situation where the value in the hashtable is inaccessible using the same key we have used to put it there.

As a quick example try the following code:

public class Class1
{
public override int GetHashCode()
{
return new Random().Next();
}

public static void Test()
{
Hashtable t = new Hashtable();
Class1 c1 = new Class1();
t.Add(c1, 1);
Class1 c2 = new Class1();
t.Add(c2, 2);
object o1 = t[c1];
object o2 = t[c2];
}
}

What would you expect o1 and o2 to be? Given no knowledge of the hashtable object I would expect to get what I have put earlier i.e.: 1 and 2, but I get nulls. That is because the Hashtable uses the GetHashCode method as a key to find values. Keep this in mind when overriding the nasty method.

kick it on DotNetKicks.com

Tuesday, August 29, 2006 2:04:18 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Wednesday, August 23, 2006

In my two previous article on relational database schemas I have described problems that arise when dealing with sql database schema that needs to be changed. I have also provided a quick tip on using Enterprise Manager to get the schema of your database. Those are the problems of relational world...

As described in the mentioned articles there are problems when it comes to keeping your database schema up to date etc. There are also problems when you need to keep the application code in sync with a database. Either you do a database-driven design and add fields in a database and then adjust the model to reflect this change, or you do a more model-driven design i.e.: the other way around - change the model and then adjust the database you always have the problem of synchronization. Tools are available that generate either a model or a database from eiher the database or the model, but is it perfect?

Far from it. One thing that I really appreciate when using Db4o is that I have no such problems. Simply because there is no database schema or in other words: the database schema reflects what I have in my model.

Now I'm not suggesting that object databases are the solution to all your problems. There are problems with data migration also however I haven't encountered one yet.

Wednesday, August 23, 2006 1:40:57 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 

As promised in my article on upgrading database schemas, I will show you the trick that I have learned when I was preparing SQL database upgrade scripts for my databases (that is before I thought on using some automation such as SQL Delta).

The fastes way I have found to get the database schema from a Microsoft SQL Server 2000 is to use the Enterprise Manager. Just navigate to a database you want, select the Tables node and select the tables you want to get schema for (you can select multiple tables using either shift or control key). As an example I have selected two tables of MyGallery database:

Having selected the desired tables use the context menu and select Copy or use the ctrl+c shortcut. Now open some kind of text editor such as Notepad. Paste the content of a clipboard and what you get is something like:

Now that is very cool isn't it? I have used this technique fo comparing two databases some time ago. Just get the schemas from two databases, save them to files on your disk and use some text diff tool.

Far from perfect but if you have no tool this is what you got. One more thing to mention is that you can use this copy and past mechanism also with stored procedures.

kick it on DotNetKicks.com

Wednesday, August 23, 2006 1:28:01 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 

Every now and then there comes a time when you release a new version of you application. Some of the new versions do not require changes to the database schema, but there are times when such a change needs to be done.

So imagine that we have an application that several hundred of our customers run. The best possible situation is that all of them have the same version of a database schema. Then we do not have to test all the possible upgrade scenarios. Mostly however there will be multiple versions of databases that are out there and you have to handle all of them.

But what is really the problem you ask? We have developers that designed the database at some point in time, created an application that was sold with that particular schema version. Then, upgrades were made, between that version and the version that we are about to release now and developers have been told to create SQL upgrade scripts so that the we can ship them with a new version. All fine if developers were doing those upgrade scripts while changing the database. Worse if you have to make a diff of the initial database schema and the desired one. Either way it was, is and always will be prone to human error. People are error prone. We, developers make mistakes, especially when doing some mindless (most of the time) activities such as comparing databases.

So, how sure you are that the database upgrade scripts that you are about to ship are the right ones? Hard question. One thing you could do is to take an initial database, run the scripts and compare the schemas. I will post a short tip on how I do it with Microsoft SQL Server 2000 Enterprise Manager. If there are errors, correct them, run the scripts again, repeat until there are no differences (or you see none).

Another problem is that the schema is not the only thing that needs to be shipped. Imagine that there is some fixed data in your database that is constant. Imagine that this data need to be altered. Same solution exists as mentioned above. Do it by hand either at the end or incrementally.

Is this solution perfect? Is it cheap? Is it time and cost effective? When you think about it you will find out that there should be some automation available. And rightfully so. There are tools that let you compare the databases, point out the differences and even create an SQL upgrade script for you. Keeping in mind what was said earlier it should be obvious that such a tool is a must have tool in every software development company that does sometime upgrade its databases.

As mentioned, there are tools that help you automate this process. I have recently found the SQL Delta application that lets you do eliminate this time consuming and error prone process of manualy generating upgrade scripts. Now if I had such a tool back when I was wasting my time making the scripts instead of solving the real business problems...

kick it on DotNetKicks.com

Wednesday, August 23, 2006 1:05:41 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |