Archive for the ‘VS2005’ Category

Subversion + .Net

July 21, 2006

I set up Subversion + continous integration (Cruisecontrol.Net) + testing (NUnit) + code analysis (NCover.Net) + VS2005 Subversion IDE integration on the weekend. Seems to be running pretty sweet. Why? Well its free – because I can 😛 – and its good to practise this stuff + I find on my little pet projects I seem to edit files and then sometimes need to rollback – now with some source control I can get back to where I was yesterday 🙂

I like the subversion model for editing files in a disconnected mode – makes it really good for working on stuff, when your out and about on some random machine, that may be offline for a while. Anyway, here’s all the bits that I used:

A 1 click subversion and subversion client setup:

or alternativelly: The main Subversion page: , and the TortoiseSVN install (subversion client):





Subversion VS IDE package: (this has a merge tool but maybe TortoiseMerge might be better – I havent checked it out…)

To get it all running I used the following in my ccnet server config (a snippet of the file):

<sourcecontrol type=”svn”>
   <executable>F:\Program Files\Subversion\bin\svn.exe</executable>
   <workingDirectory>F:\Program Files\CruiseControl.NET\server

    <executable>F:\Program Files\Subversion\bin\svn.exe</executable>
    <baseDirectory>F:\Program Files\CruiseControl.NET\server

    <buildArgs>checkout svn://ghost64/TestProject/trunk/ TestProject–username ccnetbuild –password *****</buildArgs>
    <executable>F:\Program Files\nant\bin\nant.exe</executable>
    <baseDirectory>F:\Program Files\CruiseControl.NET\server\TestProject\

     <file>F:\Program Files\CruiseControl.NET\server\TestProject\WorkingDirectory

     <file>F:\Program Files\CruiseControl.NET\server\TestProject\WorkingDirectory


I found I needed a task to pull the source and build it – the main source block didn’t seem to want to do it… I don’t know what that was about… anyway I just put in a task to get a working set and away it went. After that I had the main nant build task (where the build, tests, and coverage where run) and finally I merged all the results in :-). I edited the main environment path variable to include the paths to the nunit and ncover executables and then I could run the following from my nant file (actually this was in a batch file the nant script called out too – note TestLibrary was the name of the test project in the solution):

ncover.console nunit-console TestLibrary.dll //w .\TestLibrary\bin\Debug

By wrapping ncover around the nunit tests it has something to run against… you cant just run it against a raw dll – it needs something to be running the code – this way nunit will exectue the code so you can cover a dll.

Anyway, overall it seems to be running really well 🙂 Check it out some time!


June 9, 2006

Ahhhh – DLinq if you haven't read about this or had a play with it then DO IT NOW! But be careful – you may love it as much as me and just wish it was out there now 🙂 I had a play with it way back at the first cut, liked it (but then kind of got busy on other stuff) –  and then havent got back to it until a couple of weeks ago. Finally after looking at the msi for over a month I decided that it was about time to install that puppy and see where it was at…

If your just starting then make sure you look at the help files – probably the "DLinq Overview for CSharp Developers.doc" in the Docs folder of the install. Theres a few quirks with the install but you'll get the drift and be praying for its final arrival too 🙂 I for one couldn't get the templates in – damn – yep I ran the vbs file to install it but still no luck – the only thing I really achieved was breaking the information tab guy in the ide (see msdn for an article on the reg key to edit on how to fix that) but no matter what they wouldnt go in. I ran the various scripts people had posted as fixes but still no love. Finally it was down to editing the project file 😀

If you edit the project file (in something like notepad) – just change the following line from this: <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

to this: <Import Project="$(ProgramFiles)\LINQ Preview\Misc\Linq.targets" />

 (this was with a console app)

Good times! You should be able to compile it but you may not get any ide support. Its not the "best" but it is a beta and you'll at least be able to write your own little apps for a bit of a play :-)… The other thing to check out is the SqlMetal.exe in the Bin folder of the install – this will reverse engineer the class's for you from a database. It seems to work and is the first step to getting some DLinq love! Some inital cool things I noticed was that it uses nullable types and generic collections – and does seem to work (although my database wasnt that complicated). Something I would like added though is when you do a 1-1 table mapping is a constructor on each class that takes in each of the "non-nullable" arguments for that table… this in my opinion is one of the most helpful things out and is something I have had to put into a number of different generators/generator extensions – it would be something pretty cool to get straight out of the generation box 🙂 (maybe I'll write a template and chuck that up… – man – so much to do 🙂 )

Things I picked up on were: attaching/detaching objects is easy, the model to work against is very intuative, it works (always a good one), it supports both lazy and greedy (?terminology) fetches by specifying the mode in the query – pretty cool! – the rest you will see when you play with it 😉

Some things that I'm not in love with are: the ide support (it will come 🙂 ) and var – I know all about var but the thing I hate is that its not a type when you look at it in your code – with your eyes – you have to go fishing. Its the same kind of thing with moving from ArrayList to List of T – you immediatelly look at a typed list and understand it. var to me feels like the ArrayList of C#3.0… just my thought in terms of looking at code – not around compilation etc but just around looking at it…

Anyway – check out DLinq – its awesome! 😀


June 9, 2006

Hmmm – time for a few blog posts I think… well some short ones just on a few random bits and pieces I've played with (in amongst the busy but boring times at work lately 🙂 ). One product I had a bit of a play with about a month ago is Vanatec's OpenAccess. While I don't agree with some of there statements regarding NHibernate I did think it had a few pretty good features (they just need to be finished/extended). If your playing with NHibernate then this maybe worth a look at too just as a comparison.

One thing that is pretty cool is how smoothly it integrates into Visual Studio along with the "in the box" reverse engineering option – no need for bamming out your own generator 😉 – basically from inside Visual Studio you can point it at a database and you get your initial cut of classes to work with… thats pretty cool as you can try something pretty quickly – im not sure how good/robust it is for use in a real world scenario though – Ill leave that up to you 🙂 . Theres also a few other bits like web service support etc (its in there but to me still looks to need some work) which is nice to see but is not really as complete as I would like it to be – no doubt it will continue to be extended 🙂

The model seems pretty easy to work with and it does seem to work but one thing I didnt like was debugging it – sometimes it wasnt that intuative (and there seems to be some j# / java going on under the scenes (its been a while since I fired it up) – but its not that cool to see .java.lang… throwing out errors when your running on .net 🙂 )

 Anyways – not really here nor there on it but it is worth a bit of a play with if your looking around at O/R mappers and have been investigating NHibernate. They have a free trial download so install it, read the help, look at the samples and see what you think 🙂

log4net – Logging for your .Net Apps!

April 10, 2006

Ok I’m no log4net expert but it is a very cool package that everyone should use – believe me – it’s very quick to chuck in and will save you mountains of time and make you look like a pro without even really trying 🙂 It’s been around for along time (google for lots of tutorials etc) but I thought I’d put up my most basic x step guide to get running with log4net (this is once again so I can remember too 🙂 ):

1) Grab the log4net dll and register it in the project you want to use it in. (

2) Make up a static class / singleton and chuck something like this in it:

private static ILog _log;


_log = LogManager.GetLogger(“GlobalLogging”);


public static ILog Log
     get { return _log; }

3) Chuck a section like this in your main app.config:

 <section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” />

 <appender name=”FileAppender” type=”log4net.Appender.FileAppender”>
  <file value=”Log.txt” />
  <appendToFile value=”true” />
  <layout type=”log4net.Layout.PatternLayout”>
   <conversionPattern value=”%date [%thread] %-5level %logger – %message%newline” />

 <appender name=”ConsoleAppender” type=”log4net.Appender.ConsoleAppender” >
  <layout type=”log4net.Layout.PatternLayout”>
   <conversionPattern value=”%date [%thread] %-5level %logger – %message%newline” />

  <level value=”INFO” />
  <appender-ref ref=”FileAppender” />
  <appender-ref ref=”ConsoleAppender” />

<logger name=”GlobalLogging”>
 <level value=”INFO” />
 <appender-ref ref=”FileAppender” />

Note: Get rid of the console appender if you just want to chuck the logs to a file.

4) Use your logger: Helper.Log.Info(“This is a log message”);

Now there are different types of messages (warn, info, debug etc) so these types need to be specified in the config – just add <level /> nodes in for the ones you want to log – if they arent in there then they get ignored and wont be written to the file 🙂 That’s how you can say do logging on development (debug) and then turn those off on production without changing your code! Just remove your setting from the config!

Another trick is to use logger sections with named loggers (note the “GlobalLogging”). By getting rid of the <root /> node and only having the logger node you can filter the messages down. This is useful if other components also perform logging but you dont want there messages to come through i.e. it can be used as a filter 🙂

Anyway, its really easy to use and you will get the hang of it “just like that” 🙂 – definitelly worth much more than the 2 minutes it takes to chuck in!

Sql Exceptions and ADO.Net

March 15, 2006

Something I found the other day that confused me for a while are sql exceptions and ADO.Net. I had a test that I was expecting to throw an exception but “wouldn’t” – It would in query analyzer but if I got called Execute on the SqlCommand it would appear to go through fine – the data reader even had some rows in it – what was going on…

I would have expected the SqlCommand to throw the exception instantly – its level was high enough for it to fail! Well I found that this exception only bubbles out once you actually read the data down to the point where the exception occurred. I thought this was pretty strange since if you got a result set back then the first 3 reads from the data reader might be fine while the fourth one actually shows that an exception occurred. If you were doing a read followed by some processing counting on the execute having returned a complete and successful set then it could cause you some issues… it appears as though you should go through and get the results out / put them in a dataset or something first just to make sure that they are actually fine (?) – obviously its like that for a reason but it’s definitelly something to watch out for…


March 14, 2006

I’ve been working on a number of things over the last couple of weeks but just haven’t got around to blogging about what I’ve been finding out… lots of little notes here and there so when I get around to it I’ll have to put them up.

One tool that everyone should have is TestDriven.Net – – this is a welcome partner to both NUnit or the VS2005 Test tools. It installs a right click test option into the VS menu allowing you to run tests, run methods as tests, debug into tests, etc. It’s fast (execution and usability wise) and seems very stable (never had a crash even though the site currently lists it as Beta for 2.0). One feature I really like is it gives an error when a test fails that you can click on and it takes you straight to the part of the test that failed – Its one tool that I’m using more and more all the time.

One thing I did come across yesterday when I was running my tests from the right click menu tricked me for a while. You can tag a test with the attribute [ExpectingException(typeof(ExceptionTypeHere))] – the problem is if the class is not marked as public the test will always fail – even though the exception is thrown. Obviously the class had to be marked as public anyway but it was just something I missed when I created my class since VS normally leaves it off when you go right click new class 🙂 So if you are using ExpectingException and getting some weird failing tests then make sure the test class is marked as public! 🙂 Some stub code off the top of my head…

using NUnit.Framework;

public class TestClass // Make sure this public is here!

public void ArgumentExceptionTest()
{ … }

NHibernate – Generation, Attributes, Nullables, and Generics

March 4, 2006

Over the past few months I’ve been working on a little generator which will generate a thin layer (classes, data access objects, configuration etc) that will sit over NHibernate from a database schema and allow you to work against a particular database. Its pretty simple at the moment – Just point and click go. As I’ve learnt a bit more about it and found out some of the little tricks here and there I’ve gradually extended the template to a point where it is pretty solid (as always theres still some ironing out to do). The things I’ve been integrating into the generation template so far have been:

1) Using classes decorated with attributes (this attribute package is found in the NHibernate Contrib package) rather than the xml mapping files. I decided that the xml mapping files where just something extra to look after in the solution – I thought the less the better. The attributes on the other hand do make these base classes a bit messy but I figured I should never have to look at the generated code – The classes should just be there to use. Although generating the mapping files from the database/inserting attributes like this is really chucking out a whole heap of functionality that NHibernate will allow, I figured I could go back at a later time and extend the generator to provide a richer model. At the moment I just purely want a single class to table mapping and it should just all work.

2) Adding support for Nullable types (I’m using a dll I found called NHibernate.Nullables2.dll – not the one in the NHibernate Contrib package. This can be found here: This is a very cool package as it allows us to use the new .Net 2.0 nullable type notation eg. bool? _flag; in our code/base classes but in the attribute or mapping fies declare it as a special type like: NullableBooleanType 😀 Whats cool here is that from our program side of things these types are transparent while from the NHibernate side of things it understands what we are talking about. And using it is so easy – just reference the dll and change the type in the mapping file :-D. Here’s at example of using NHibernate.Nullables2 with the attribute package:

[NHMA.Property( Name = “RiderNumber”,Column = “RiderNumber”,NotNull = false,TypeType = typeof(NullableInt32Type) )]public int? RiderNumber{get { return _riderNumber; }set { _riderNumber = value; }}

 On the other hand when you use the Nullable package from the contrib you have to use the special types throughout your program code – not very nice 😦

3) Adding in the support for generic collections. This has been what I’ve been working on for the last week or so after work (when I can), and after playing around a bit I have it working – not as nicely as I would have liked but working none the less. The package I used to provide support for generics can be found here: (this is a cool blog too – add it to your feeds if you have one 🙂 ).

The reason putting generics in there was so important for me was that I’m working in .Net 2.0 and then suddenly with NHibernate only under 1.1. I had to go back to using IList – as the developer you have to remember what is in that collection when you come to use it. Generics on the otherhand provide a strongly typed collection IList of T so when you come to use it you know exactly what it is! No casting exceptions for me :-). To get this to work is not the easiest (not hard – but more a case of looking at an example and then just copying it) and then fixing the errors that come up when you run the app up. I’ll give an example of these bits and pieces in another post since this ones getting pretty long …

4) Working on adding enumeration suppport to the generation process – this is working but I’ll keep you posted once I have tested it and used it a couple of times in some app’s I’m working on at the moment 🙂

VS2005 Automated Dataset Generation

February 18, 2006

Something that bugged me yesterday at work was the pain involved when the database is changed and a projects datasets need to be updated (something constantly happening during an apps early dev cycle when the database is still under tweaking). In our case the datasets are effectivelly a straight mirror of the information in the database (simply drag on the tables and away you go – no internal customization) so I was like “man – there has to be a better way – surely this can be automated and how does the designer do it?”. So I went on an investigation…After a late night coding session it was done 🙂

Firstly, as always, I googled a bit and then generated some datasets (checking out all the project options/the files generated etc) and watched what was going on with good old regmon, filemon, and pmon as a way of reverse engineering and seeing what was going on when you were using the ide. After digging a bit I came across MSDataSetGenerator and xsd.exe. MSDataSetGenerator from what I can gather is a com component which handles the dataset components inside the ide (when you check the default tool on the properties of the dataset it is listed there) – I tried for a while to find where the dll was located but had no luck so I then decided to try something else – I could see that xsd.exe would generate the file which is commonly called datasetname.Designer.cs (dataset class file) from the dataset xsd so all I now needed was the xsd file. The xsc and xss just dealt with the layout properties so I figured they were just ide overhead and I could work those out later.

Firstly I wanted to avoid calling out to xsd.exe and I worked out that you could use: TypedDataSetGenerator.Generate() from the System.Data namespace to get similar functionality i.e. it would generate a dataset class file from an xsd. However, near the end of my journey I found that this generated un-compilable dataset class files from the xsd – It would ignore some of the options specified against the dataset name (msprop:Generator_TablePropName) so if your dataset had the same name as a table it would have name conflicts with properties exposed on the dataset. xsd.exe on the same xml picked these options up perfectly (and generated an identical match with what the ide would generate) – I had a winner – call out to xsd.exe with the dataset xsd (this seemed to be the fastest option to get something to work).

To get the dataset xsd I reverse engineered the xml that would be generated in the xsd if you dragged on a few tables, set up some relationships etc i.e. Where did the elements need to go? How where they named? What were the options if they were nullable? What could be stripped out? etc. I then rolled the generation all up into a class which I could then call and just tell it “these are the tables in my dataset – make it!” The datasets where being autogenerated! 🙂  As for the xsc and xss files I wrote these out as if no tables had been dragged on. The good thing about this is that when you drag them on the display settings get saved into the xsc file – so you need to go and move all the tables to get them looking pretty. By leaving them as the default, visual studio would do a pretty arrangement (like the sql management studio option)! Something I havent seen available even from in the ide – killed another problem – didnt need to lay them out now either = even less work 😀

So what are the cons – I havent implemented the mapping of all the sql types to xml types yet (just the most obvious ones) – this wont be hard at all since most of the types produce the same xml – it is more a case of just checking for the special cases and putting those in. Something I’ll do as they come up. xsd.exe needs to be called – maybe this is not too ugly…The code had been slapped together (but it seems to work – some file stuff access needs to be looked at + refactoring) 🙂 – I’ll fix it up during intergration. The dataset specification file is just a comma delimited text file (something to be upgraded to xml during intergration into the main generation tool – if the guys like the tool :-P).

The pro’s are – no more dragging, 1 click to update all datasets to a new database schema, no more arranging, we dont use the data adapters (we delete them) so those are left off (if someone wants those I could put them back in), is quicker than the ide – no lagging out :-). You just need to keep the generation file up to date (we already have one for the main tool which will have all of this info so no probs there)

I’ll post up the first cut of code but if someone wants a hand or has some questions then post them up – Hopefully you wont curse at your db designer when they do an update now 😉 Note: You will need to setup the app config with the right options for your machine (xsd.exe is under C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe if you have installed vs to your c drive) – The app also needs to be compiled 🙂 Big ups to JD for chucking this on his server for me! Thanks bro!