How to use Reliable State manager in Azure Fabric

Hello everybody,

today I want to describe a bit how to use Reliable State Manager in Azure Fabric.

First of all, I want to say that reliable state manager is some kind of entity which for me is very very similar to MongoDB collections. The only diffference is that it allows to have transactions, while in MongoDb you can't have them. In case if you need to use it in your class, you'll need to have reference to it's basic interface. For example like this:

class ServiceFabricStudentRepository : IStudentRepository
        private IReliableStateManager _stateManager;
        private object cancellationToken;
        public ServiceFabricProductRepository(IReliableStateManager stateManager)
            _stateManager = stateManager;

As you see, all that is needed for ReliableStateManager to work is private member, with DI of ReliableStateManager in constructor. 

Next step would be adding something to state manager. Take note on usage of transactions:

public async Task AddStudent(Student product)
    var students = await _stateManager.GetOrAddAsync<IReliableDictionary<GuidStudent>>("students");
    using (var tx = _stateManager.CreateTransaction())
        await products.AddOrUpdateAsync(tx, student.Id, student, (id, value) => student);
        await tx.CommitAsync();

Take note of the following: initially we've got collection of students, and then worked with them over transaction.

In case of failing of addition of student to our list, all information will be rolled back.

No Comments

Add a Comment