private void LoadTableMetaData() { // Try to load the partition meta data from the existing table (which contains a list of the partition keys in the table). _partitionMetaDataEntityWrapper = _tableMetaDataContext.FindAsync(CtConstants.TableMetaDataPartitionKey, CtConstants.PartitionSchemasRowKey).Result; // Set the default PartitionKey using the combination below in case there are more than one CloudTableContext objects // on the same table. _defaultIndexDefinitionName = $"DefaultIndex_ofType_{typeof(TDomainEntity).Name}"; if (_partitionMetaDataEntityWrapper != null) { /* This is going through and populating the local PartitionKeysInTable property with the list of keys retrieved * from the Azure table. * This also checks to see if there is a PartitionKey for the table meta data and the DefaultPartition * and adds that if there isn't*/ var metaDataPkIsInList = false; foreach (var partitionKeyString in _partitionMetaDataEntityWrapper.DomainObjectInstance.PartitionKeys) { if (partitionKeyString == CtConstants.TableMetaDataPartitionKey) { metaDataPkIsInList = true; } var isInList = IndexNameKeysInTable.Contains(partitionKeyString); if (!isInList) { IndexNameKeysInTable.Add(partitionKeyString); } } if (!metaDataPkIsInList) { IndexNameKeysInTable.Add(CtConstants.TableMetaDataPartitionKey); } // The RowKey for the DefaultSchema is set by the given ID property of the TDomainEntity object DefaultIndex = CreateIndexDefinition(_defaultIndexDefinitionName) .DefineIndexCriteria(entity => true) .SetIndexedPropertyCriteria(entity => entity.GetType().Name); // Enables searching directly on the type. if (IndexDefinitions.All(indexDefinition => indexDefinition.IndexNameKey != DefaultIndex.IndexNameKey)) { AddIndexDefinition(DefaultIndex); } } else { /* Creates a new partition meta data entity and adds the appropriate default partitions and metadata partitions*/ _partitionMetaDataEntityWrapper = new TableEntityWrapper <PartitionMetaData>(CtConstants.TableMetaDataPartitionKey, CtConstants.PartitionSchemasRowKey); DefaultIndex = CreateIndexDefinition(_defaultIndexDefinitionName) .DefineIndexCriteria(entity => true) .SetIndexedPropertyCriteria(entity => entity.GetType().Name); // Enables searching directly on the type AddIndexDefinition(DefaultIndex); } }
/// <summary> /// Asynchronously gets a domain entity by the ID using the given entityId and based on the index defined by the given indexNameKey. /// If the indexNameKey parameter is left null then the <see cref="DefaultIndex"/> is used. /// </summary> /// <param name="entityId">Value of the ID property to be used in finding by the ID. This object will get serialized to JSON before being used in the query</param> /// <param name="indexNameKey">Optional name of the index used when searching for items by ID. The <see cref="DefaultIndex"/> is usually the one that holds the ID index</param> /// <returns></returns> public async Task <TDomainEntity> GetByIdAsync(object entityId, string indexNameKey = null) { if (entityId == null) { return(null); } var serializedEntityId = JsonConvert.SerializeObject(entityId); if (indexNameKey == null) { indexNameKey = DefaultIndex.IndexNameKey; } var tableEntity = await TableOperationsService.FindAsync(indexNameKey, serializedEntityId).ConfigureAwait(false); return(tableEntity.DomainObjectInstance); }