Null values

Posts   
 
    
damienj
User
Posts: 4
Joined: 26-Nov-2003
# Posted on: 29-Dec-2003 16:37:45   

Hi,

I need help with null values

I have tables with int foreign keys that I want to be able to set to Null but I could not find a way to do this with llblgen pro.

Initially the value in the new Entity is 0 and I get a Null in the database when saving. But when i set the field value to zero manually an Exception occurs because the save method try to save a 0.

Is there a way to save Null values with LLBLgen pro ?

Thanks disappointed

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 29-Dec-2003 16:51:18   

See the documentation: "Entities, NULL values and defaults" in Generating code - using the entity classes.

Frans Bouma | Lead developer LLBLGen Pro
ctadlock avatar
ctadlock
User
Posts: 60
Joined: 12-Feb-2004
# Posted on: 25-Feb-2004 23:57:15   

A couple questions....

(1) Would it be possible to generate Is<columnName>Null() methods on the entities? It makes the API cleaner and easier to understand.

public bool IsContactTitleNull() { return customer.TestOriginalFieldValueForNull(CustomerFieldIndex.ContactTitle); }

for the code...

CustomerEntity customer = new CustomerEntity("CHOPS"); DataAccessAdapter adapter = new DataAccessAdapter(); adapter.FetchEntity(customer); bool contactTitleIsNull = customer.TestOriginalFieldValueForNull(CustomerFieldIndex.ContactTitle);

(2) Is there a way to have entities use a custom ITypeDefaultValue class to get the default values for the columns instead of the generated class? The DataAccessAdapter has a property, TypeDefaultValueSupplier, which allows you to set the class to get the defaults from.

I think because the default value for Guids is 'Guid.NewGuid()', there is no way to tell if a Guid column is null, or has a default value; TestOriginalFieldValueForNull will always fail. 'Guid.Empty' seems a better value to represent a null/default value.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Feb-2004 11:13:34   

ctadlock wrote:

A couple questions.... (1) Would it be possible to generate Is<columnName>Null() methods on the entities? It makes the API cleaner and easier to understand.

public bool IsContactTitleNull() { return customer.TestOriginalFieldValueForNull(CustomerFieldIndex.ContactTitle); }

for the code...

CustomerEntity customer = new CustomerEntity("CHOPS"); DataAccessAdapter adapter = new DataAccessAdapter(); adapter.FetchEntity(customer); bool contactTitleIsNull = customer.TestOriginalFieldValueForNull(CustomerFieldIndex.ContactTitle);

Is<bla>Null gives the suggestion that you can test for null also for the current value, which is not the case. I then also have to add Set<bla>ToNull(), which is then a conveniance method for entity.SetNewFieldValue(<fieldindex>, null);

However, fields which do not have a value set initially, in a new entity, will end up with value NULL in the database as well. Testing for Is<bla>Null on a new entity should return true for fields which do not have NULL as a value. It's perhaps me, but I find Is<bla>Null quite weird. simple_smile Say, you read an entity. It has the value NULL for ContactName. You overwrite that value with "". Still you want to test if the entity initially had NULL. You can't with IsContactNameNull, because that will return false (or better: it SHOULD return false, because it is not NULL anymore, it's "" simple_smile ). That's why there is that method Test_Original_FieldValueForNull simple_smile . So you always have to use 2 methods then, you can't use TestOriginalFieldValueForNull as the method to call for Is<bla>Null. The design is: you should never worry about NULL's, but at the same time, you should be aware of the fact that NULLs should be avoided as much as possible. So when you pull an entity from the database, you can read every property without a problem, there is no need for testing every property if it's null. In the rare circumstance you want some column to be SET to NULL (in an update), you can by using SetNewFieldValue().

(2) Is there a way to have entities use a custom ITypeDefaultValue class to get the default values for the columns instead of the generated class? The DataAccessAdapter has a property, TypeDefaultValueSupplier, which allows you to set the class to get the defaults from. I think because the default value for Guids is 'Guid.NewGuid()', there is no way to tell if a Guid column is null, or has a default value; TestOriginalFieldValueForNull will always fail. 'Guid.Empty' seems a better value to represent a null/default value.

The TypeDefaultValue class is a template, you can alter it if you want to. These values are there for conveniance: to give each column a value. You shouldn't interpret these values as valid values, because they aren't. If you do, you are in fact reading a column which isn't initialized. This is ok, in a loop where you walk the fields and you are not using all of them, or when you bind a set of rows to a control and you want to be sure binding works, it matters when you want to interpret the value. I can change the default value, however it should never be used as a valid value for NULL. It's like assuming a string variable in C# is automatically initialized with String.Empty, which is not the case.

Could you please elaborate a bit on "TestOriginalFieldValueForNull will always fail. " ? Setting a GUID column to NULL in the database will not result in 'true' when using that method for that column after the entity is fetched? It should.

NULL values is a thing where humanity as a whole will never agree on. One person will say NULLs are evil, the other will say they're required, another person will say "" is useful for NULL equivalent for string typed fields, others don't want that because "" is a valid value etc. As .NET doesn't have nullable value types, it's a bit hard to use native types and still be able to work with NULL values. That's why I opted to not use NULL values whatsoever in the API, only by using methods to test.

To avoid further discussion on this, I'll change the default value for GUID typed fields into Guid.Empty. For every person using this value as a check if the column was NULL: don't, use the proper methods to test. If these don't work, report it as a bug simple_smile .

Frans Bouma | Lead developer LLBLGen Pro