When it is not a nullable int I'm using the following syntax for a property CityId:
private static PropertyInfo<int> CityIdProperty = RegisterProperty<int>(typeof(Joiner), new PropertyInfo<int>("CityId"));
public int CityId#region Data Access - Fetch
LoadProperty<int>(CityIdProperty, data.numCityId);
#region Data Access - Insert
data.numCityId = ReadProperty<int>(CityIdProperty);
#region Data Access - Update
data.numCityId = ReadProperty<int>(CityIdProperty);
When CityId can be nullable in the SQL database how do I define the property in CSLA 3.5?
Is the following OK?
private static PropertyInfo<int?> CityIdProperty = RegisterProperty<int?>(typeof(Joiner), new PropertyInfo<int?>("CityId")); public int? CityId
get { return GetProperty<int?>(CityIdProperty); }
set { SetProperty<int?>(CityIdProperty, value); }
#region Data Access - Fetch
LoadProperty<int?>(CityIdProperty, data.numCityId);
#region Data Access - Insert
data.numCityId = ReadProperty<int?>(CityIdProperty);
#region Data Access - Update
data.numCityId = ReadProperty<int?>(CityIdProperty
Does somebody know how to define this in the xml document for the codesmith templates?
In the attribute NativeType int? doesn't exist.
We use nullable ints as you mention above.
One thing we did, though, for data access (we use stored procedures) is that null needs to be System.DbNull. So, we created some extension methods for nullable types.
public static object DBValue(this int? value)This allows one do to something like this when adding parameters to the sproc (in the case below, it's a nulable datetime):
database.AddInParameter(command, _incorporatedDate.DataParameterName,
DbType.DateTime, ReadProperty(_incorporatedDate).DBValue());When fetching, one needs to check the datareader for DbNull, and if it's DbNull just don't load into it - leave it the default value of null (in the general case):
if (!reader.IsDBNull(_incorporatedDate.DataColumnName))
{
LoadProperty<DateTime?>(_incorporatedDate, reader.GetDateTime(_incorporatedDate.DataColumnName));
}
(DataColumnName and DataParameterName are just an extended properties of our own PropertyInfo objects, but you get the gist0.
So long story short, we use nullable propertyinfo objects without difficulty.
Chris
Copyright (c) Marimer LLC