Skip to content

Commit

Permalink
Fix collection manual naming issue (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
alveflo authored Aug 26, 2020
1 parent c443108 commit 3be7988
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
15 changes: 7 additions & 8 deletions Spinit.CosmosDb/CosmosDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private protected virtual DatabaseModel CreateModel()
.Select(x => new CollectionModel
{
DatabaseId = databaseId,
PropertyName = x.Name,
CollectionId = x.GetCollectionId(),
Analyzer = new DefaultAnalyzer()
})
Expand All @@ -156,16 +157,14 @@ private void SetupCollectionProperties()
private void SetupCollectionProperty<TEntity>(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<TEntity>(CosmosClient.GetContainer(collectionModel.DatabaseId, collectionId), _documentClient, collectionModel);
collectionProperty.SetValue(this, collection);
}
if (string.IsNullOrEmpty(collectionModel.CollectionId))
collectionModel.CollectionId = collectionProperty.GetCollectionId();

var collection = new CosmosDbCollection<TEntity>(CosmosClient.GetContainer(collectionModel.DatabaseId, collectionModel.CollectionId), _documentClient, collectionModel);
collectionProperty.SetValue(this, collection);
}

private IEnumerable<PropertyInfo> GetCollectionProperties()
Expand Down
8 changes: 8 additions & 0 deletions Spinit.CosmosDb/Internals/CollectionPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ namespace Spinit.CosmosDb
{
internal static class CollectionPropertyExtensions
{
internal static string GetCollectionPropertyName<TDatabase, TEntity>(this Expression<Func<TDatabase, ICosmosDbCollection<TEntity>>> collectionSelector)
where TDatabase : CosmosDatabase
where TEntity : class, ICosmosEntity
{
var collectionProperty = GetCollectionPropertyInfo(collectionSelector);
return collectionProperty.Name;
}

internal static string GetCollectionId<TDatabase, TEntity>(this Expression<Func<TDatabase, ICosmosDbCollection<TEntity>>> collectionSelector)
where TDatabase : CosmosDatabase
where TEntity : class, ICosmosEntity
Expand Down
2 changes: 2 additions & 0 deletions Spinit.CosmosDb/Modeling/CollectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
4 changes: 2 additions & 2 deletions Spinit.CosmosDb/Modeling/DatabaseModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public DatabaseModelBuilder<TDatabase> DatabaseId(string databaseId)
public CollectionModelBuilder<TEntity> Collection<TEntity>(Expression<Func<TDatabase, ICosmosDbCollection<TEntity>>> 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<TEntity>(collectionModel);
}

Expand Down

0 comments on commit 3be7988

Please sign in to comment.