TypeDefaultValueSupplier

Posts   
 
    
Dja
User
Posts: 29
Joined: 22-Jul-2010
# Posted on: 22-Jul-2010 13:53:35   

Hi, I have been using version 1.1. In that version, class SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase had a property named TypeDefaultValueSupplier. There is no such property in V3, and I was wondering what can be used instead of this, or what should be done to achieve this functionality?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 22-Jul-2010 21:36:52   

Can you give us an example of what you need to achieve with this ? This may help us point you in the right direction.

Thanks

Matt

Dja
User
Posts: 29
Joined: 22-Jul-2010
# Posted on: 23-Jul-2010 08:51:33   

I need to somehow tell my data access component which values should be default for which type, that is, I need to tell which values should be saved to database as NULL values, and vice versa. For example, when I have property that is type of long, if it's value is -1, I want it to be saved as NULL. When I read it from the database, I want the NULL value to be mapped as -1.

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Jul-2010 09:33:36   

There is a class called TypeDefaultValue generated into the HelperClasses folder of the generated project. This class decides the return value of each type.

Dja
User
Posts: 29
Joined: 22-Jul-2010
# Posted on: 23-Jul-2010 10:56:52   

Thank you for the answer, but can you tell me how to use that class in order to achieve mentioned functionallity?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 23-Jul-2010 11:31:13   

In the databasegeneric project, helper classes, the class TypeDefaultValue is the one which provides default values for NULL valued properties. This works for reads.

It's highly discouraged to use magic values for null. If you don't want to set a field, simply don't set it to a value. So if a field of type int has to be null as you didn't set it to a valid value, simply don't set it to any value, NULL will be inserted for that field.

Frans Bouma | Lead developer LLBLGen Pro
Dja
User
Posts: 29
Joined: 22-Jul-2010
# Posted on: 23-Jul-2010 11:46:10   

Ok, but what if I have value "5" in database for some int field, and I want it to be NULL again? I would have to read it, create an object from that row, and change the value for mapped property to a value that would be saved as NULL. How can I do this?

Dja
User
Posts: 29
Joined: 22-Jul-2010
# Posted on: 23-Jul-2010 14:12:48   

There is method

ITypeDefaultValue CreateTypeDefaultValueProvider()

on EntityBase2 which should be overridden. I suppose that's the way to do this...

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Jul-2010 17:21:42   

Dja wrote:

Ok, but what if I have value "5" in database for some int field, and I want it to be NULL again? I would have to read it, create an object from that row, and change the value for mapped property to a value that would be saved as NULL. How can I do this?

CustomerEntity customer = new CustomerEntity("ALFKI");
myAdapter.FetchEntity(customer);
customer.Address = null;
myAdapter.SaveEntity(customer);
David Elizondo | LLBLGen Support Team
Dja
User
Posts: 29
Joined: 22-Jul-2010
# Posted on: 24-Jul-2010 19:05:39   

simple_smile Cool, but my field is not FK, so there is no related object, just int property in object model, which is not nullable. I guess I would have to have default value which would be saved as null. I will try as I wrote in my previous post. Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 25-Jul-2010 12:14:38   

If it's not nullable in the entity, it's also not nullable in the DB. Or am I overlooking something obvious? simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Dja
User
Posts: 29
Joined: 22-Jul-2010
# Posted on: 25-Jul-2010 14:00:10   

I understand, and totally agree. simple_smile But, I have a project written and built in .Net 1.1, and LLBL 1.1. I think, if I'm not mistaken, that there were no nullable fields back then, thus not in the project, too. If I wanted to use LLBL V3 to generate existing code for that project, instead of V1, I would like to avoid changing other code that works with the entities, so I don't want nullable properties. I would like to have default values for them, to be saved as null, because that's the way the project works.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Jul-2010 04:03:20   

This is copied from V3's Breaking Changes section:

.NET 2.0: if an entity field is nullable, by default it is generated as a Nullable(of T) field. This means that if you read that field's property, you have to read the value into a Nullable(of T) typed variable as well. You can control this through the preferences and project properties by using the preference/project property GenerateNullableFieldsAsNullableTypes. This setting controls every new field's Generate as Nullable type setting. By default all the fields in your project will have that setting set to true, if they're nullable and a value type. To disable nullable fields for some fields, you can disable that for those fields in the entity editor. It's recommended to have all fields which are nullable as Nullable(Of T), and only disable this feature if your current code misuses the default value feature of the entity fields. Please see below as well how to detect that. A plug-in is included in the designer which allows you to set all the generate as nullable type flags of all the entity fields selected to a given value (true/false) so you can migrate your code from 1.0.2005.1 to v2.0 more easily, as having a lot of nullable types can break a lot of code.
David Elizondo | LLBLGen Support Team