May 2006 - Posts
You might have noticed in my previous post that the Linq query started with From instead of Select. While I did prefer the syntax that started with Select, because of its similarities with SQL, this change makes a lot of sense. If we want a good IntelliSense behavior we need to tell the editor what we are working on before we do any work, just like we declare variables before we use them, and starting with Select means that the IDE has no idea what we are going to query so it cannot give us any help.
And with VB wanting to be the most productive environment means it wants us to get as much help as possible so we need to give it a bit had to and tell it what we want to query.
While it wasn't my first choice I fully agree with the change as I think IntelliSense is an important productivity tool and I will get used to the syntax real quick :-)
Previously I stated that I dislike the separation between the code and the database schema. A first look at SqlMetal.exe, a utility that comes with DLinq suggested that it might ease the problems and guess what, it does :-)
I created a small sample app and added the following command to the pre-build event:
"c:\Program Files\LINQ Preview\Bin\SqlMetal.exe" /database:Northwind /code:"$(ProjectDir)Northwind.vb" /language:vb
Build the project to have the Northwind.vb file generated and include it in the project.
Next I added the following code:
Sub main()
Dim northwind AsNew Northwind(My.Settings.DataConnection)
' Build a list cities with multiple customers
Dim results = From customer In ( _
From customer In northwind.Customers _
GroupBy customer.Country, customer.City _
Select it.Key, it, Count(it)) _
Where customer.Count > 1 _
Select customer _
OrderBy customer.Count Descending, customer.Key.Country, customer.Key.City
ForEach row In results
' Print the country and city
Console.WriteLine("Country: {0} - City: {1} - Count: {2}", _
row.Key.Country, row.Key.City, row.Count)
' Print the customers for the city
ForEach customer In row.it
Console.WriteLine(" => {0}", customer.CompanyName)
Next
Next
Console.ReadKey()
EndSub
Now build and run the project and you will see the output appear.
Now go open the table schema for the Customers table and change the City column name to TheCity. Save the changes and try to run. This time you will get compile errors on every reference to the City property because its no longer valid.
Easy to do and so much better than runtime errors that the City column name is invalid.
Enjoy!
Now you don’t normally catch Don Box doing a lot of VB programming. Now he does, maybe Erik Meijer showed him the light.
One of the problems I am always having with database development is the gap between data and code. Sure it isn’t hard to move data from database to memory, manipulate it and move it back but what remains a drag is someone making schema changes to the database and only finding out at runtime that the classes no longer match the underlying database schema.
Well there might me light at the end of the tunnel :-)
One of the features of DLinq is a command line utility called SqlMetal. Using this tool you can generate the types required for DLinq to work. Basically it generates a large source file, depending on the command line parameters, that you can include in your project. Now suppose you add this command line utility to the Visual Studio projects pre-build event? This way you will always have an up to date set of entity objects matching the database schema and you will get compile errors as soon as you change the database schema.
Update: See
http://msmvps.com/blogs/theproblemsolver/archive/2006/05/30/97708.aspx for an example.
Maurice de Beijer
www.TheProblemSolver.nl
If you, like me, are into playing with the latest toys you surely don't want to miss the latest WinFX beta. And Microsoft has just release Beta 2 so hurry and download it :-)
If you do any work at all with Microsoft Office the Visual Studio Tools for Office add-on is must have. And just in case you have never done any work with it, you really need to take a look at the current version 2 of Visual Studio Tools for Office, it is seriously cool for writing small applications or a useful addition to larger projects.
Well anyway the development team is hard at work at the next version and have just release a CTP so we can start playing with it and giving them feedback on what we like and what we don't :-) And guess what, it’s the June CTP and it isn't even June yet, guess they are ahead on their schedule :-)
Red Gate make some pretty cool tools for SQL Server. Now most of them cost some money, not a lot specially considering what you get but still they aren't free.
Now they have a new add that provides IntelliSense for SQL commands. And not only the keywords but also the table, columns and pretty much everything you need. And it works with a host of environments: Query Analyzer, SQL Server 2005 Management Studio, Visual Studio 2005, Visual Studio .NET 2003, SQL Server 2000 Enterprise Manager and even UltraEdit32. And best of all, it is free until September 1st.
With version 1.* of the .NET framework you needed to override the ToString() function to get a useful display in the debugger watch window. While this still works as before it is not exactly an ideal way, after all the ToString() function can be called during other actions and isn’t meant for debugging purposes.
The primitive way of looking at a collection.
In version 2.0 of the .NET framework we have a better way of doing this and that is the DebuggerDisplay attribute. Using this attribute gives you quite a bit of control over how the debugger displays a type. And just in case you where thinking that a single like with inserted fields isn’t enough, you can use a read-only property or a function to return the string displayed in the attribute :-)
Showing the same list using the DebuggerDisplay attribute.
Module Module1
Sub Main()
Dim people AsNew List(Of Person)
people.Add(New Person("Maurice", "de Beijer"))
people.Add(New Person("John", "Doe"))
EndSub
EndModule
<DebuggerDisplay("Person: {_firstName} {_lastName}.")> _
Class Person
PublicSubNew(ByVal firstName AsString, ByVal lastName AsString)
_firstName = firstName
_lastName = lastName
EndSub
Private _firstName AsString
PublicProperty FirstName() AsString
Get
Return _firstName
EndGet
Set(ByVal Value AsString)
_firstName = Value
EndSet
EndProperty
Private _lastName AsString
PublicProperty LastName() AsString
Get
Return _lastName
EndGet
Set(ByVal Value AsString)
_lastName = Value
EndSet
EndProperty
PublicFunction DebuggerDisplayFunction() AsString
Return _firstName + " " + _lastName + " from a function"
EndFunction
<DebuggerDisplay("Person: {DebuggerDisplayFunction}.")> _
PublicReadOnlyProperty DebuggerDisplayProperty() AsString
Get
Dim name AsString
name = _firstName + " " + _lastName + " from a property"
Return name
EndGet
EndProperty
EndClass
One of the things that is sort of hard to do is change the value of parameters to constructors in my classes. Take following, very simple example:
PublicClass BaseClass
PublicSubNew(ByVal param AsInteger)
Console.WriteLine(param)
EndSub
EndClass
PublicClass DerivedClass
Inherits BaseClass
PublicSubNew(ByVal param AsInteger)
MyBase.New(param)
EndSub
EndClass
Now if I want to change the value of param in the constructor in the DerivedClass I can't simply add change it's constructor to:
PublicSubNew(ByVal param AsInteger)
param = param + 1
MyBase.New(param)
EndSub
This code won’t compile because the call to the base constructor must be the first line of code.
However it turns out you can include a call to a shared function in the call to the base constructor. This allows you to execute any code you want when the objects is instantiated before the base class constructor is called and gives you ample opportunity to change the value passed to the base class.
PublicClass DerivedClass
Inherits BaseClass
PublicSubNew(ByVal param AsInteger)
MyBase.New(ChangeParam(param))
EndSub
PrivateSharedFunction ChangeParam(ByVal param AsInteger) AsInteger
Return param + 1
EndFunction
EndClass
So what restriction do you have? Well the object isn’t instantiated yet so the function needs to be shared and you cannot pass the object reference to it and set any properties/fields.
If you are in the Netherlands a do any form of serious .NET development I expect you are here in Papendal for the annual SDC conference. If not you are surely missing one of the better events in the Netherlands this year. There are loads of excellent speakers and around 400 people attending this year.
Well we are just about to start so if you are here enjoy and have a fun and informative two days :-)
Don’t forget, next week Monday and Tuesday is the SDC conference in the
Netherlands. We have a large selection of first class speakers so it is
certainly worth your time. And there are just a few places left so make
sure you don’t miss the opportunity.
See you there next Monday!
Maurice de Beijer
www.TheProblemSolver.nl
Scott Guthrie and his team just released the Visual Studio 2005 Web Application Project. Even though I am not much of a web developer and mainly do winform development I am still pleased to see this. I did a few small ASP.NET 2.0 projects and wasn't all that happy with the project less way of working. Even though I haven’t tested this yet I hear good things about it from those in the know. One thing is certain, my next ASP.NET project will be using this.
More Posts
Next page »