Often the primary keys for tables are integer types. One problem can occur when you accidentally use the OrderID when you really meant to use CustomerID. This bug can happen inside the database and it can also arise in your code data layer.
One solution for this at the code level is to create custom types (eg. OrderIdentifier and CustomerIdentifier) so that the compiler will throw an error if you try and assign or compare different types.
The down-side to this is that because Integers are value types, you can’t just inherit from the Integer class. Instead you need to store the actual integer value inside the class and expose it through a property.
Public Class OrderIdentifier
Private _value As Integer
Public Property Value() As Integer
Set(ByVal Value As Integer)
_value = Value
Public Sub New()
Public Sub New(ByVal value As Integer)
Me.Value = value
I’ve also noticed that NHibernate doesn’t like having custom types for primary keys, as it tries to use the System.Type.IsAssignableFrom method to see if it can convert an Integer to the OrderIdentifier object (which fails).
I’m not sure that there’s a workaround for that, as I think it would require a class to inherit from the Int32 structure, which isn’t possible.
Like [NHibernateAddin]/2007/07/nhibernate-plugin-for-visual-studio.html), ActiveWriter is an add-in for Visual Studio 2005.
It leverages the DSL functionality to allow you to model classes and relationships and it generates the NHibernate mapping and class files (in VB or C#).
I think this has the potential to meet all my requirements for doing the hard work of class and mapping generation.
One thing that is missing at the moment, is that if you drop tables onto the design surface one at a time, they don’t automatically add relations with existing classes. I did discover yesterday that if you drop multiple tables at the same time, it does figure out the relations and add them in.
One of my patches adds support to clean up the class property names. If your database naming strategy means that your column names have some kind of common prefix, you probably don’t want that in the property name.
For example, given the following database table:
CREATE TABLE [dbo].[TEST_CONTENT]
[TC_ID] [int] IDENTITY(1,1) NOT NULL,
[TC_TEST] [int] NOT NULL,
[TC_CONTENT] [int] NOT NULL
If you set the model’s “Property Name Filter Expression” property to the regular expression
^\w+?_it will then remove the text up to the first underscore for each property name, so that you end up with a class like this:
- Add relations when dropping additional tables
- Support for custom types for properties
- Auto-arrange model layout
I was looking at some T-SQL that RJ had written and noticed he was using ‘True’ and ‘False’ when dealing with bit data types.
I’ve always used 1 and 0, but I think ‘True’ and ‘False’ is more obvious, and I think I’ll start using it from now on.
Sure enough, the SQL 2005 Books Online entry for bit (Transact-SQL) mentions this:
The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.