From 3be79883b73b40753142e59aa73852b8e7f9bad9 Mon Sep 17 00:00:00 2001 From: victor Date: Wed, 26 Aug 2020 16:35:35 +0200 Subject: [PATCH] Fix collection manual naming issue (#36) --- Spinit.CosmosDb/CosmosDatabase.cs | 15 +++++++-------- .../Internals/CollectionPropertyExtensions.cs | 8 ++++++++ Spinit.CosmosDb/Modeling/CollectionModel.cs | 2 ++ Spinit.CosmosDb/Modeling/DatabaseModelBuilder.cs | 4 ++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Spinit.CosmosDb/CosmosDatabase.cs b/Spinit.CosmosDb/CosmosDatabase.cs index bbc67d3..c448d36 100644 --- a/Spinit.CosmosDb/CosmosDatabase.cs +++ b/Spinit.CosmosDb/CosmosDatabase.cs @@ -130,6 +130,7 @@ private protected virtual DatabaseModel CreateModel() .Select(x => new CollectionModel { DatabaseId = databaseId, + PropertyName = x.Name, CollectionId = x.GetCollectionId(), Analyzer = new DefaultAnalyzer() }) @@ -156,16 +157,14 @@ private void SetupCollectionProperties() private void SetupCollectionProperty(PropertyInfo collectionProperty) where TEntity : class, ICosmosEntity { - var collectionId = collectionProperty.GetCollectionId(); var collectionModel = Model.CollectionModels - .Where(x => x.CollectionId != null) - .SingleOrDefault(x => x.CollectionId == collectionId); // TODO: add indexed property => Model.CollectionModels[collectionId] + .SingleOrDefault(x => x.PropertyName == collectionProperty.Name); // TODO: add indexed property => Model.CollectionModels[collectionId] - if (collectionModel != null) - { - var collection = new CosmosDbCollection(CosmosClient.GetContainer(collectionModel.DatabaseId, collectionId), _documentClient, collectionModel); - collectionProperty.SetValue(this, collection); - } + if (string.IsNullOrEmpty(collectionModel.CollectionId)) + collectionModel.CollectionId = collectionProperty.GetCollectionId(); + + var collection = new CosmosDbCollection(CosmosClient.GetContainer(collectionModel.DatabaseId, collectionModel.CollectionId), _documentClient, collectionModel); + collectionProperty.SetValue(this, collection); } private IEnumerable GetCollectionProperties() diff --git a/Spinit.CosmosDb/Internals/CollectionPropertyExtensions.cs b/Spinit.CosmosDb/Internals/CollectionPropertyExtensions.cs index 066659e..5dd61ed 100644 --- a/Spinit.CosmosDb/Internals/CollectionPropertyExtensions.cs +++ b/Spinit.CosmosDb/Internals/CollectionPropertyExtensions.cs @@ -6,6 +6,14 @@ namespace Spinit.CosmosDb { internal static class CollectionPropertyExtensions { + internal static string GetCollectionPropertyName(this Expression>> collectionSelector) + where TDatabase : CosmosDatabase + where TEntity : class, ICosmosEntity + { + var collectionProperty = GetCollectionPropertyInfo(collectionSelector); + return collectionProperty.Name; + } + internal static string GetCollectionId(this Expression>> collectionSelector) where TDatabase : CosmosDatabase where TEntity : class, ICosmosEntity diff --git a/Spinit.CosmosDb/Modeling/CollectionModel.cs b/Spinit.CosmosDb/Modeling/CollectionModel.cs index 332c40e..005b6f5 100644 --- a/Spinit.CosmosDb/Modeling/CollectionModel.cs +++ b/Spinit.CosmosDb/Modeling/CollectionModel.cs @@ -4,6 +4,8 @@ public sealed class CollectionModel { public string DatabaseId { get; internal set; } + public string PropertyName { get; internal set; } + public string CollectionId { get; internal set; } public Analyzer Analyzer { get; internal set; } diff --git a/Spinit.CosmosDb/Modeling/DatabaseModelBuilder.cs b/Spinit.CosmosDb/Modeling/DatabaseModelBuilder.cs index 88b1913..9ef3634 100644 --- a/Spinit.CosmosDb/Modeling/DatabaseModelBuilder.cs +++ b/Spinit.CosmosDb/Modeling/DatabaseModelBuilder.cs @@ -27,8 +27,8 @@ public DatabaseModelBuilder DatabaseId(string databaseId) public CollectionModelBuilder Collection(Expression>> collectionSelector) where TEntity : class, ICosmosEntity { - var collectionId = collectionSelector.GetCollectionId(); - var collectionModel = _databaseModel.CollectionModels.Single(x => x.CollectionId == collectionId); // TODO: add indexed property => Model.CollectionModels[collectionId] + var propertyName = collectionSelector.GetCollectionPropertyName(); + var collectionModel = _databaseModel.CollectionModels.Single(x => x.PropertyName == propertyName); // TODO: add indexed property => Model.CollectionModels[collectionId] return new CollectionModelBuilder(collectionModel); }