I am updating an existing application that has implemented the brew home class in its business and datalayer objects.
I want to replace this with Nullable types and discard the class of constants, which looks like this, but with all types not allowing a null data type:
class Constants
{
public static int nullInt
{
get { return int.MinValue; }
}
}
These vaule constants are used as default values for almost all object properties, such as:
private decimal _unitPrice = Constants.nullInt;
public decimal UnitPrice
{
get { return _unitPrice; }
set { _unitPrice = (value == null) ? Constants.nullInt : value; }
}
This causes some confusion when storing object properties in Db, since all decimal numbers and int must be checked for psudo null values, otherwise you will save things like int.MinValue in Db.
private void Save()
{
SqlParameter sqlParm = new SqlParameter();
sqlParm.Value = (this.UnitPrice == Constants.nullInt) ? DBNull.Value : (object)this.UnitPrice;
}
, . Nullable, , ? , ?
public decimal? UnitPrice { get; set; }
private void Save()
{
SqlParameter sqlParm = new SqlParameter();
sqlParm.Value = this.UnitPrice ?? DBNull.Value;
}
EDIT: , , SET . , , , ?