0

I'm trying to add an object to a redis hash with ServiceStack. However when one of the object I'm trying to send to redis hash has a null property, it breaks, getting a String reference not set to an instance of a String exception.

Can someone point the correct way to do what I wish to do, in this case store an object as redis hash while it has some properties set as null.

Here is a snippet showing the issue:

    public interface SomeInterface
    {

       string Id {get;set;}
       string Name {get;set;}
       DateTime? DateScheduled {get;set;}
       DateTime? DateUnscheduled {get;set;}
       ISomeInterface SomeNeededObject {get;set};

     }


    public class SomeClass : SomeInterface
    { 

      public string Id {get;set;
      public string Name {get;set;}
      public DateTime? DateScheduled {get;set;}
      public DateTime? DateUnscheduled {get;set;}
      public ISomeInterface SomeNeededObject {get;set};

      // Other useful properties/methods

    }


    public class SomeService :Service
    {

      public void SomeMethod( ISomeInterface objectToSendToHash)
      {
          SomeClass c = new SomeClass() 
          {
            Id = "1",
            Name = "Name1",
            DateScheduled = DateTime.Now
          };   // creates a SomeClass with DateUnscheduled and SomeNeededObject properties set to null


            using (IRedisClient redisClient  = this.MyRedisClient)
            {
                var redis = redisClient.As<ISomeInterface>();

                redis.StoreAsHash(objectToSendToHash);  //throws exception because either DateUnscheduled, or SomeNeededObject properties are null!

            }

      }

    }
scoob
  • 1,369
  • 1
  • 17
  • 29

1 Answers1

1

Firstly you should really avoid using interfaces on DTO's which are a common source of issues during serialization.

Also the StoreAsHash API converts your type into a key/value dictionary and stores each property in a redis hash so you it should only be used with basic Poco types (i.e. not with nested complex types).

I've cleaned up your source code example to remove all the naming and syntax errors, and the following example does work as expected:

public interface ISomeInterface
{
    string Id { get; set; }
    string Name { get; set; }
    DateTime? DateScheduled { get; set; }
    DateTime? DateUnscheduled { get; set; }
    ISomeInterface SomeNeededObject { get; set; }
}

public class SomeClass : ISomeInterface
{
    public string Id { get; set; }
    public string Name { get; set; }
    public DateTime? DateScheduled { get; set; }
    public DateTime? DateUnscheduled { get; set; }
    public ISomeInterface SomeNeededObject { get; set; }
}

var c = new SomeClass
{
    Id = "1",
    Name = "Name1",
    DateScheduled = DateTime.Now,
};

Redis.StoreAsHash(c);

var fromHash = Redis.GetFromHash<SomeClass>("1");
fromHash.PrintDump();

Note there's no reason to us a Typed RedisClient to use StoreAsHash, i.e

var redis = redisClient.As<ISomeInterface>();

Which also looks like it's using the wrong type i.e. SomeClass vs ISomeInterface.

The StoreAsHash API is also available on the RedisClient, e.g:

redisClient.StoreAsHash(objectToSendToHash);  
Community
  • 1
  • 1
mythz
  • 141,670
  • 29
  • 246
  • 390