Saturday, September 23, 2006

Today I have played a bit with Microsoft's new command line tool called Windows PowerShell. Other names I have heard over the years include Microsoft Shell. The code name for the project was Monad. As usual, Microsoft cannot make it's mind and so, expect a lot of confusion. As a simple example: what would you expect the abbreviation for Windows PowerShell would be? WPS or PS maybe? But what when you install the tool, it has an icon like this: . If you look closely you will see the "MSH"...

Putting the naming inconsistency aside, I have decided to give it a try. It took few seconds to start for the first time, and I was presented with a message asking me if I want to run a software from untrusted publisher, which happens to be non other than Micorsoft Corporation:

Since this is one of my favourite companies, I just had to agree :-). It wasn't enough to agree just once ("[R] Run Once") because I was asked the same thing for a couple of times, so I have decided to trust MS always. Few seconds later, I had a command prompt ready.

The first impression is that it is very slow. The auto-completion function is slow, moving between directories is slow. But by slow I do not mean few seconds. I just mean it is slower than the plain old CMD. Fortunately, the slowness does not impact the usability of the tool, which brings you the power of .NET to the command line.

Just for a taste of how it works I have tried using some familiar objects and methods I know from programming in Visual Studio. So first off I called the ordinary "dir" command in the root folder of the C: drive and the output was more or less what would be expected:

Nothing unusual here. So where is the power of the PowerShell? The next command will say it all: "dir | foreach { $_.GetType().ToString() }" and the output for the same directory as above:

The command basically does some kind of operation: "GetType().ToString()" on each object thanks to the "foreach" command. As you can see, everything is an object in the PowerShell and as such you can execute methods on it or use it's properties. That is what I call Power Shell! Where are the linux shells hiding now :-)

As a side note. I really think Microsoft should do something to make the standard black window for the shell somewhat more usable - whatever that means. One feature that I would like the most would be the autocompletion of command parameters. I have seen this on one of the unix-like systems so it certainly is possible! Nevertheless PowerShell will rock even without it!

kick it on DotNetKicks.com

Saturday, September 23, 2006 1:56:22 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [1]  | 
 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]  |