Search This Blog

15 February 2010

Nhibernate and mapping Enums

When using NHibernate one has to be careful in certain circumstances to avoid small errors that may have a huge impact. In one area an "error" might be hidden for a long time without anybody noticing it and everything working as it should. 

As soon as you create a mapping file with properties that are explicitly given types your should be aware what you are doing. If you for instance create a mapping file where you map a bit column and specify that the type for this column is Int32 and then in the mapping class define this property as an Enum you might be getting into problems. 

Since the class that is used for the mapping in the project might differ from the actual values in the database NHibernate will have problems understanding that nothing really has changed.

For example if you have the following in your mapping file, maybe for mapping up a user and specify gender for the user.
<property name="Gender" not-null="true" type="Int32" />

And the following class defined: 

public class Person
     public enum GenderEnum
        Male = 0,
     public GenderEnum Gender;

Once you load this element into memory the property Gender will be tranformed from being an Int32 value in the database to a GenderEnum type in the project. Later on when you either want to unload this object or load another object of the same type or any operation on the object that might trigger an update event for NHibernate such an event will indead be triggered, regardless if you have changed any values or not. The reason for this is that NHibernate detects a change of in the object, the property that is mappped as being Int32 in the database is now returned as an Enum. 

To fix this problem remove the type declaration in the mapping file and let NHibernate resolve the type itself.

A more extensive information about this problem can be found in the following links:

Reading of text files

In more or less every medium sized or bigger project there will be a need for reading text files sooner or later. Most of the time the text files will be quite good organized either as fixed post files or separated file (comma, tab and newline being the most common separator).

If the file is of type fixed post file one of the best method is to match the file structure into a class structure in the program and read line by line an instance of such an object. This supports multiple instances of subsections for elements and such too. Serializing the file becomes quite easy and goes farily smooth and if there is some error in the filecontent it will propagate and show early instead of being hidden away. Also it is possible to store information from the file in a database farily easy with this approch since a direct mappping to a database structure is possible. If you are using NHibernate (or Hibernate for Java) it is even possible to create a mapping file where the underlying class is the same for the database as for the textfile. One more advantage is the possibility to create automatic tests for the objects use to verify that the business rules are followed in the application.

With a file that is using some sort of separators to differentiate between the different post many times the easies way is to read up the entire file (or row by row) and run a split over the separators and then parse each element into a underlying object that holds the structure of the file. Using this approch measn its easy to detect errors in length of each element in the file as well as each field for each element. This also means that if the file becomes corrupt or changed in the future (not uncommon) this will be detected and can be handled correctly.

When it omes to have to actually read the text from the files it differes from project to project as well as from language to language but most of the times some simple loop expression is easy enough to use and quite effective at the same time. Don't complicate things, keep it easy and you will earn stability and readability of the source code.

09 February 2010

MSN and AVG antivirus setting

I'm using the free version of AVG antivirus ( at home and recently had to setup my computer again, reinstallation problems. When I sat up MSN i saw that my settings for virus scanning didn't work. Regarless to say I wanted to have a antivirus scan of files that arrives and searched the internet for solutions.

There is some parameters for AVG scanner that SHOULD work but to really get it to workw ith msn you need to make some extra handling, otherwise it will just say that scan completed but if you look at the logs no files have been scanned.

To make it so that AVG really scans incomming files you need to create a batchfile that is run instead of the pure exeutable. Don't ask me why!

The looks of the batch file should be as follows:

@echo off
"\avgscan?.exe" /SCAN=%1 /HEUR /ARC /PUP /CLEAN

where you either use avgscanx.exe or avgscana.exe depending on if you are using 32 or 64-bit version of the program.

Below is a more extensive presentation of this and the source of where I found this information.

05 February 2010

Test driven development

During software development one must take actions to ensure that the software contains as few errors as possible, no software will be completely error free. There are several paths to consider and they all have their advantages and disadvantages.

There are people that argue for not using testing at all since it costs to much and gives to few advantages, I for one think this is as stupid as standing on the freeway and hiding your head in the ground just hoping that there won't be any cars comming your way. At the minimum there should at least be some basic testing done before releasing a software. Either some of the users should perform test or some sort of automatic test should be written.

Unit testing is a very good method for minimising the errors within a single unit. The hard part with unit testing is to make sure that the test really are UNIT test and not integrations test. Only direct calls to an object should be tested in a single test and no dependencies of other objects implementation should be required. If one begin to couple objects and the test depends on other objects you aren't anymore using Unit testing. The reason for makeing sure that the objects only depends on themself in a test is that by doing this one can make it much easier to maintain a good test suite.

Integrations testing on the other side is a good method for making sure that all components in the project works as designed even if changes are made in the underlying code. These test should match the business cases detected to make sure that no errors are introduced into the code. It often here you need to remake the tests when a bug is detected to make sure that the test catches the bug and also can give a clear information that the bug has been removed and fixed.

User tests are a good way to detect errors in communication since the users will be the most capable persons to make sure that the application behaves and functions as it should. It can be hard to make sure that these tests really are performed.

Automatic GUI testing is good but often time consuming and hard to write so they cover all possible events and possibilities in the application. Needless its a good idé to creates such tests, especilly if there isn't any real users test in the system.

04 February 2010

Util programs and plugins to make work easier

Many of the programs that are default on a computer are inadecvat for a software developer since they are lacking in functionallity or otherwise have some limitations. I have been using many different tools to make my everyday work situation easier and more fun.

With this post I wanted to share my view of some of these programs and give you a reason why you should check them out. Since I'm a software developer with focus on .NET I'm using Visual Studio and as a IDE the environment is fairly good and one of the best things about VS is the ability register plugins, record macros and customize the look and feel of the IDE. Whenever I have the need to reinstall Visual Studio I always have some plugins athat I really need to add to the IDE before I can use the IDE to my full potential:

DPack ( A very valuable tool that help to track down methods, classes and files within the solution in an easy and quick way.

ReSharper ( One of the most know tools for Visual Studio and recognized as being well worth the money. There is also the possibility to download Nightly Builds which are free to use for 30-days and at the end of the trial period you may download a new build and reset the time limit.

WinMerge ( Not strictly a plugin for Visual Studio but a great tool in its own too. I have set it up to use for comparing files when using source control and need to resolve conflicts since Visual Studios own tool isn't near as good.

WebAii ( More of a thrid party tool than a plugin for Visual Studio. If you ever need to create complex GUI tests for you software be sure to check out this little goodie. Sure it takes a while to understand at the start but it really makes it a lot easier to make sure that nothing has been broken during the last build.

Other programs that I use to make my work situation easier and more fun that aren't integrated into Visual Studio.

soapUI ( Great tool for using beside Visual Studio to examine and create small test applications for either your own webservices or external webservices that you are using in your solution.

Notepad++ ( Sure there are several text editors out there and we all find our own favorite. One of the reasons why I like Notepad++ is the multitude of plugins created and suits more or less any task needed for text files. Also Notepad++ gives information that a current open file have been changed on disc which is invaluable during control of logging.

These are just a little bit of all tools that I use in my everyday job, we all find tools that we use either just to make it easier to create or maintain applications and perform our job.

03 February 2010

Reason for this blog

As I have now work with software development for a couple of years and often have searched for solutions on various problems during projects and found more or less good answers to my question I decided that I should at least give something back.

This blog will contain post related to development and problems and solutions with software as well as different approches to solutions in different circumstances. Most of the posts will be in english but there will probably even be posts in swedish.

If there is somebody out there that has any questions or problems which I might be able to answer don't hesitate to contact me and I'll see if there is something that I might be able to answer.