예제 #1
0
        async Task <IEntityStore <string, TwinStoreEntity> > GetTwinStore(IComponentContext context)
        {
            string entityName = "EdgeTwin";
            Option <IEntityStore <string, TwinStoreEntity> > twinStoreOption = Option.None <IEntityStore <string, TwinStoreEntity> >();
            var storeProvider = await context.Resolve <Task <IStoreProvider> >();

            if (this.encryptTwinStore)
            {
                Option <IEncryptionProvider> encryptionProvider = await context.Resolve <Task <Option <IEncryptionProvider> > >();

                twinStoreOption = encryptionProvider.Map(
                    e =>
                {
                    IEntityStore <string, string> underlyingEntityStore = storeProvider.GetEntityStore <string, string>($"underlying{entityName}");
                    IKeyValueStore <string, string> es                     = new UpdatableEncryptedStore <string, string>(underlyingEntityStore, e);
                    ITypeMapper <string, string> keyMapper                 = new JsonMapper <string>();
                    ITypeMapper <TwinStoreEntity, string> valueMapper      = new JsonMapper <TwinStoreEntity>();
                    IKeyValueStore <string, TwinStoreEntity> dbStoreMapper = new KeyValueStoreMapper <string, string, TwinStoreEntity, string>(es, keyMapper, valueMapper);
                    IEntityStore <string, TwinStoreEntity> tes             = new EntityStore <string, TwinStoreEntity>(dbStoreMapper, entityName);
                    return(tes);
                });
            }

            IEntityStore <string, TwinStoreEntity> twinStore = twinStoreOption.GetOrElse(
                () => storeProvider.GetEntityStore <string, TwinStoreEntity>(entityName));

            return(twinStore);
        }
예제 #2
0
        public async Task BasicMapperTest <TK, TK1, TV, TV1>(
            IKeyValueStore <TK, TV> underlyingStore,
            ITypeMapper <TK1, TK> keyMapper,
            ITypeMapper <TV1, TV> valueMapper,
            IDictionary <TK1, TV1> items)
        {
            // Arrange
            var keyValueMapper = new KeyValueStoreMapper <TK1, TK, TV1, TV>(underlyingStore, keyMapper, valueMapper);

            // Act
            KeyValuePair <TK1, TV1> item = items.First();
            await keyValueMapper.Put(item.Key, item.Value);

            // Assert
            Option <TV1> value = await keyValueMapper.Get(item.Key);

            Assert.True(value.HasValue);
            Option <TV> underlyingValue = await underlyingStore.Get(keyMapper.From(item.Key));

            Assert.True(underlyingValue.HasValue);
            Assert.Equal(underlyingValue.OrDefault(), valueMapper.From(value.OrDefault()));
        }