Creating, Reading, Updating and Deleting - adhtalbo/RedisContext GitHub Wiki

Basics

This page assumes you have an entity created called BasicEntity which looks like this:

public class BasicEntity : RedisEntity
{
    public string SomeValue {get; set;}
    
    public int AnotherValue {get; set;}
}

And a context that looks like this:

public class ExampleContext : RedisContext
{
    // Can be connection string OR the name of a connection string in the config
    public ExampleContext(string connectionString) : base(connectionString)
    {
    }
    
    public RedisSet<BasicEntity> BasicEntity {get; set;}
}

Creating

To create an entity in the database first you must create a new entity instance. This can be done with a constructor or by simply setting parameters. However there must be an empty constructor for entity fetching to work correctly.

Also an Id must be set in order to store an item. Ids are strings and objects are indexed by Id. This means using a Guid as an Id would produce a psudo-random order. You can order your entities by date by prefixing a guid with a data/time stamp.

// Entity that would be ordered by date in the index
var entity = new BasicEntity{
    Id = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Guid.NewGuid().ToString("N")
}; 

From here entities can be inserted in two ways. The first way is to use the Insert method. This method only inserts if an entity with the same id does not exists.

var canInsert = context.BasicEntity.Insert(entity); // True
var canReplace = context.BasicEntity.Insert(entity); // False

To insert an element regardless of whether one already exists in the database you can use InsertOrReplace.

context.BasicEntity.InsertOrReplace(entity);

Reading

To fetch an entity you can pass an Id to the Fetch method.

var entity = context.BasicEntity.Fetch("SomeIdHere");

You can also fetch a batch of items in multiple ways. The first method is to simply pass an array of Ids

var entities = context.BasicEntity.Fetch(new string[]{"OneId", "AnotherId"});

The second method is to pass a starting Id, a Limit and an Offset. If the Id is an empty string then the starting id will be the first id in the database.

var entities = context.BasicEntity.Fetch("SomeIdHere", 10, 10);

The final way is to pass a minimum and maximum Ids. All Entities with Ids between these Ids (and including these Ids) will be returned.

var entities = context.BasicEntity.Fetch("A_SomeStartingId", "Z_SomeEndingId");

Updating

The method to update an entity is Update. You cannot update the database with freshly initialized entities, they must be entities fetched from the database.

var entity = context.BasicEntity.Fetch("SomeIdHere");
entity.SomeValue = "New Value";
var success = context.BasicEntity.Update(entity);

This method uses optimistic concurrency in order to stop collisions. The way this works is each entity has an Etag property. If this Etag property differs from the one in the database then the entity has already been changed by another process and the update will not occur. If you do not care if the entity has been changed then InsertOrReplace (which will also insert if needed) or Replace (which will not insert new entities) can be used to simply replace the entity in the database.

// Increment int value using optimistic concurrency (10 attempts)
for(var i = 0; i < 10; i++)
{
    var entity = context.BasicEntity.Fetch("SomeIdHere");
    entity.AnotherValue++;
    if(context.BasicEntity.Update(entity))
    {
        break;
    }
}

Deleting

You can delete an entity by passing an entity or an id to the Delete method.

context.BasicEntity.Delete("SomeIdHere");