Ejemplo n.º 1
0
        public async Task <TValue> AddOrUpdate(
            TKey key,
            Func <TKey, Task <TValue> > addFactory,
            Func <TKey, TValue, Task <TValue> > updateFactory,
            CancellationToken token)
        {
            var keyContent = await KeyConverter.Convert(key, token);

            var keyRecord = new KeyRecord(
                id: keyContent.GetSha1(),
                type: KeyType,
                content: keyContent);
            var valueRecord = await backedStorage.AddOrUpdate(
                keyRecord,
                addFactory : async _ =>
            {
                var value   = await addFactory(key);
                var content = await ValueConverter.Convert(value, token);
                var audit   = new Audit(diagnosticContext.CorrelationId, diagnosticContext.User);
                return(new ValueRecord(ValueType, content, audit));
            },
                updateFactory : async(_, old) =>
            {
                var oldValue = await ValueConverter.Convert(old.Content, token);
                var newValue = await updateFactory(key, oldValue);
                var content  = await ValueConverter.Convert(newValue, token);
                var audit    = new Audit(diagnosticContext.CorrelationId, diagnosticContext.User);
                return(new ValueRecord(ValueType, content, audit));
            },
                token);

            return(await ValueConverter.Convert(valueRecord.Content, token));
        }