STRHM
1.0.0
Introduction article
Example entity will be a Book class, that has couple of basic fields.
public class Book
{
public int Id { get; set; }
public int? Rating { get; set; }
public DateTime PublishedOn { get; set; }
[SerializableRedisProperty]
public List<Author> Authors { get; set; }
}
That’s right, you noticed a custom attribute. This attribute is responsible for particular property being serialized when it is being set into Redis hash as a value.
Book repository inherits from BaseRedisHashSetRepository, as it exposes most common needed functionality and database connection, in case if you want to extend functionality in Book repository itself.
public class BookRepository : BaseRedisHashSetRepository<Book>
{
public BookRepository(IRedisConnection redisConnection, IStronglyTypedRedisSerializer serializer, RedisHashSetOptions configurationOptions)
: base(redisConnection, serializer, configurationOptions)
{
}
}
Constructor parameters:
Examples of getting values:
// Check if value is set on property
if (updatedBook.HasValue(b => b.Rating))
// Get values
updatedBook.Get<IEnumerable<Author>>(b => b.Authors)
updatedBook.Get<int?>(b => b.Rating)
or
var book = await _bookRepository.GetAsync(book.Id.ToString());
Examples of setting values:
await _bookRepository.SaveAsync(book.Id.ToString(), book);
or
await _bookRepository.HashSetAsync(book.Id.ToString(), new StronglyTypedDictionary<Book>(new StronglyTypedRedisNewtonsoftSerializer())
{
{ b => b.Rating, 10 },
{ b => b.Authors, new List<Author>()},
{ b => b.PublishedOn, DateTime.Now }
});