Articles → .NET DESIGN PATTERN → Null object Design Pattern

Null object Design Pattern






Scenario








public MemberEntity GetMemberInformation(int memberId){}




public class MemberEntity
{
    private int _Id;
    private string _FullName;
    private string _Email;
    public int Id
    {
        get
        {
            return _ Id;
        }
        set
        {
            _ Id = value;
        }
    }
    public string FullName
    {
        get
        {
            return _ FullName;
        }
        set
        {
            _ FullName = value;
        }
    }
    public string Email
    {
        get
        {
            return _ Email;
        }
        set
        {
            _ Email = value;
        }
    }
}




public MemberEntity GetMemberInformation(int memberId)
{ 
 	 DataSet userInformationDataSet;
  	SQLServer obj;
  	MemberEntity memberEntityObject = null;
  	userInformationDataSet = obj.GetResultSet(query);
  	if (userInformationDataSet != null)
  	{
		  if (userInformationDataSet.Tables.Count > 0)
		  {
        if (userInformationDataSet.Tables[0].Rows.Count > 0) 
		    {
			    memberEntityObject = new MemberEntity();
          //  Code to set the value into entity properties 
        } 	
      }
    }
    return memberEntityObject;
}







Null object design pattern






private class NullMemberEntity : MemberEntity
{
        public override int Id
        {
            get
            {
                return 0;
            }
        }

        public override string FullName
        {
            get
            {
                return string.Empty;
            }
        }

        public override string Email
        {
            get
            {
                return string.Empty;
            }
        }
}




public static readonly MemberEntity NULL = new NullMemberEntity();






public class MemberEntity
{
    public static readonly MemberEntity NULL = new NullMemberEntity();

    private int _Id;
    private string _FullName;
    private string _Email;
    #endregion
   
    public MemberEntity()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public virtual int Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
        }
    }

    public virtual string FullName
    {
        get
        {
            return _ FullName;
        }
        set
        {
            _ FullName = value;
        }
    }
    public virtual string Email
    {
        get
        {
            return _ Email;
        }
        set
        {
            _ Email = value;
        }
    }
    
    private class NullMemberEntity : MemberEntity
    {
        public override int Id
        {
            get
            {
                return 0;
            }
        }

        public override string FullName
        {
            get
            {
                return string.Empty;
            }
        }

        public override string Email
        {
            get
            {
                return string.Empty;
            }
        }
    }
}




public MemberEntity GetMemberInformation(int memberId)
{ 
 	  DataSet userInformationDataSet;
  	SQLServer obj;
  	MemberEntity memberEntityObject = MemberEntity.NULL;
  	userInformationDataSet = obj.GetResultSet(query);
  	if (userInformationDataSet != null)
  	{
		  if (userInformationDataSet.Tables.Count > 0)
		  {
        if (userInformationDataSet.Tables[0].Rows.Count > 0) 
		    {
			    memberEntityObject = new MemberEntity();
          //  Code to set the value into entity properties 
        }	
  		}
    }
    return memberEntityObject;
}





Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, August 30, 2011

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250