Implementing Nullable Types in Existing Objects

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()
    {
        //Datalayer calls and other props omitted
        SqlParameter sqlParm = new SqlParameter();
        sqlParm.Value = (this.UnitPrice == Constants.nullInt) ? DBNull.Value : (object)this.UnitPrice;

    }

, . Nullable, , ? , ?

    public decimal? UnitPrice { get; set; }

    private void Save()
    {
        //Datalayer calls and other props omitted
        SqlParameter sqlParm = new SqlParameter();
        sqlParm.Value = this.UnitPrice ?? DBNull.Value;
    }

EDIT: , , SET . , , , ?

+5
2
public decimal? UnitPrice { get; set; }

private void Save()
{
    //Datalayer calls and other props omitted
    SqlParameter sqlParm = new SqlParameter();
    sqlParm.Value = this.UnitPrice ?? DBNull.Value;
}

, . .

+5

, , , . , , .

- . .

, , , . plug-n-play, , , , , , .

using System;
using System.Data.SqlClient;

namespace NullableTypes
{
    class Program
    {
        static class Constants
        {
            public static decimal NullDecimal
            {
                get { return decimal.MinValue; }
            }
        }

        public class ProductTheOldWay
        {
            public string Name { get; set; }
            public decimal UnitPrice { get; set; }

            public ProductTheOldWay()
            {
                Name = string.Empty;
                UnitPrice = Constants.NullDecimal;
            }

            public override string ToString()
            {
                return "Product: " + Name + " Price: " + 
                       ((UnitPrice == Constants.NullDecimal) 
                         ? "Out of stock" 
                         : UnitPrice.ToString());
            }

            public void Save()
            {
                //Datalayer calls and other props omitted
                var sqlParm = new SqlParameter
                {
                    Value = (UnitPrice == Constants.NullDecimal) 
                             ? DBNull.Value 
                             : (object)UnitPrice
                };

                //save to the database...
                Console.WriteLine("Value written to the database: " + sqlParm.Value);
            }
        }

        public class ProductTheNewWay
        {
            public string Name { get; set; }
            public decimal? UnitPrice { get; set; }

            public ProductTheNewWay()
            {
                Name = string.Empty;
            }

            public override string ToString()
            {
                return "Product: " + Name + " Price: " + 
                       ((UnitPrice.HasValue) 
                         ? UnitPrice.ToString() 
                         : "Out of stock");
            }

            public void Save()
            {
                //Datalayer calls and other props omitted
                var sqlParm = new SqlParameter
                {
                    Value = UnitPrice
                };

                //save to the database...
                Console.WriteLine("Value written to the database: " + sqlParm.Value);
            }
        }

        static void Main()
        {
            var oldProduct1 = new ProductTheOldWay
            {
                Name = "Widget",
                UnitPrice = 5.99M
            };

            var oldProduct2 = new ProductTheOldWay
            {
                Name = "Rare Widget",
                UnitPrice = Constants.NullDecimal // out of stock
            };

            Console.WriteLine(oldProduct1);
            Console.WriteLine(oldProduct2);

            Console.WriteLine("Saving...");
            oldProduct1.Save();
            oldProduct2.Save();

            Console.ReadLine();

            var newProduct1 = new ProductTheNewWay
            {
                Name = "Widget",
                UnitPrice = 5.99M
            };

            var newProduct2 = new ProductTheNewWay
            {
                Name = "Rare Widget"
                /* UnitPrice = null by default */
            };

            Console.WriteLine(newProduct1);
            Console.WriteLine(newProduct2);

            Console.WriteLine("Saving...");
            newProduct1.Save();
            newProduct2.Save();

            Console.ReadLine();

            // as a further example of the new property usage..

            if (newProduct1.UnitPrice > 5)
                Console.WriteLine(newProduct1);

            Console.WriteLine("Using nullable types is a great way to simplify code...");

            Console.ReadLine();    
        }
    }
}

Product: Widget Price: 5.99  
Product: Rare Widget Price: Out of stock  
Saving...  
Value written to the database: 5.99  
Value written to the database:  

Product: Widget Price: 5.99  
Product: Rare Widget Price: Out of stock  
Saving...  
Value written to the database: 5.99  
Value written to the database:  

Product: Widget Price: 5.99  

- ...

, , , .

0

All Articles