If we have a need to use nullable types (which we do), are they pretty safe to use with the framework? For example, I know that the Validation.CommonRules.MaxValue<T> will break on the type of the parameter being a nullable type...such as CommonRules.maxValue<Int32?> because this is not an IComparable I get a compile error. Now, this makes perfect sense to me but I need to know if there are other locations within the framework where it would not be such a trivial matter to add my own code and use that instead to handle the nullable type.
I hope I was clear enough, any questions just ask.
Thanks in advance,
David
I am new to using the framework (and the book) so forgive me if I am doing something incorrectly. That said I believe that I have encountered a very significant issue with using nullable types. Specifically, in a property that utlizes a nullable backing field and property the wrong overload of both GetProperty and SetProperty gets called and the data is lost.
For example: I have a nullable stateID backing field and a nullable property. When calling GetProperty(StateIDProperty, _stateID) I always get 0, even when _stateID <> 0. Stepping into the code reveals that I am stepping into the wrong overload. It is calling GetProperty<P>(PropertyInfo<P> propertyInfo, Security.NoAccessBehavior noAccess) and passing the value into the enum instead which causes the data loss.
Further, similar issues are occurring with SetProperty(StateIDProperty, _StateID, value), causing data loss. I am now calling the SetProperty<P>(string propertyName, ref P field, P newValue, Security.NoAccessBehavior noAccess) overload as a workaround.
I verified that this is specific to nullable types and also that it is not just Integer? types.
Needless to say, if I am correct this is a very significant issue for my company. Any information would be appreciated.
Interesting. And unfortunate.
You may be able to resolve this with a cast – force the
null to a specific type:
SetProperty(MyProperty, (P)_stateId);
Otherwise the answer is probably to do what you are doing and
call the non-ambiguous overloads of GetProperty() and SetProperty().
The only broader alternative would be for me to eliminate most
of the short-cut overloads and make EVERYONE use the long-form version, which
is clearly not a good idea.
As you can imagine, the .NET runtime is unable to infer a type
for null, and so the overload rules just map it to an arbitrary overload that
has the correct number of parameters. You need to somehow constrain the value
so the runtime knows that the null value is actually of a specific type. I
suspect the forced cast above will do that for you.
Rocky
From: Eric
[mailto:cslanet@lhotka.net]
Sent: Friday, January 23, 2009 2:43 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Nullable types in CSLA.NET 3.6
I am new to using the framework (and the book) so forgive me if I am doing
something incorrectly. That said I believe that I have encountered a
very significant issue with using nullable types. Specifically, in a
property that utlizes a nullable backing field and property the wrong overload
of both GetProperty and SetProperty gets called and the data is lost.
For example: I have a nullable stateID backing field and a nullable
property. When calling GetProperty(StateIDProperty, _stateID) I always get 0, even when _stateID
<> 0. Stepping into the code reveals that I am stepping into the
wrong overload. It is calling GetProperty<P>(PropertyInfo<P>
propertyInfo, Security.NoAccessBehavior noAccess) and passing the value into
the enum instead which causes the data loss.
Further, similar issues are occurring with SetProperty(StateIDProperty,
_StateID, value), causing data loss. I am now calling the
SetProperty<P>(string propertyName, ref P field, P newValue,
Security.NoAccessBehavior noAccess) overload as a workaround.
I verified that this is specific to nullable types and also that it is not
just Integer? types.
Needless to say, if I am correct this is a very significant issue for my
company. Any information would be appreciated.
Copyright (c) Marimer LLC