STRHM
1.0.0
紹介記事
エンティティの例は、いくつかの基本フィールドを持つ Book クラスになります。
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; }
}
そうです、カスタム属性に気づきました。この属性は、Redis ハッシュに値として設定されるときに特定のプロパティがシリアル化されることを担当します。
Book リポジトリ自体の機能を拡張したい場合に備えて、Book リポジトリは BaseRedisHashSetRepository を継承します。これは、最も一般的に必要な機能とデータベース接続を公開するためです。
public class BookRepository : BaseRedisHashSetRepository<Book>
{
public BookRepository(IRedisConnection redisConnection, IStronglyTypedRedisSerializer serializer, RedisHashSetOptions configurationOptions)
: base(redisConnection, serializer, configurationOptions)
{
}
}
コンストラクターのパラメーター:
値の取得の例:
// 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)
または
var book = await _bookRepository.GetAsync(book.Id.ToString());
設定値の例:
await _bookRepository.SaveAsync(book.Id.ToString(), book);
または
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 }
});