Skip to content

Commit

Permalink
Adding an additional, simplified Entity Collection interface
Browse files Browse the repository at this point in the history
(without as much of the ICollection baggage)
  • Loading branch information
Turnerj committed May 13, 2019
1 parent 9143d77 commit 35a0da0
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 49 deletions.
41 changes: 2 additions & 39 deletions src/MongoFramework/Infrastructure/EntityBucketStagingCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@

namespace MongoFramework.Infrastructure
{
public class EntityBucketStagingCollection<TGroup, TSubEntity> : IEntityCollection<EntityBucket<TGroup, TSubEntity>> where TGroup : class
public class EntityBucketStagingCollection<TGroup, TSubEntity> : IEntityCollectionBase<EntityBucket<TGroup, TSubEntity>> where TGroup : class
{
private Dictionary<TGroup, List<TSubEntity>> SubEntityStaging { get; }
private IEntityCollection<EntityBucket<TGroup, TSubEntity>> ChangeTracker { get; }
private IEntityReader<EntityBucket<TGroup, TSubEntity>> EntityReader { get; }

public int BucketSize { get; }

public int Count => throw new NotImplementedException();

public bool IsReadOnly => false;

public EntityBucketStagingCollection(IEntityReader<EntityBucket<TGroup, TSubEntity>> entityReader, int bucketSize)
{
SubEntityStaging = new Dictionary<TGroup, List<TSubEntity>>(new ShallowPropertyEqualityComparer<TGroup>());
Expand Down Expand Up @@ -115,42 +111,9 @@ public void Update(EntityBucket<TGroup, TSubEntity> entity, EntityEntryState sta
{
throw new NotImplementedException();
}

public void Add(EntityBucket<TGroup, TSubEntity> item)
{
throw new NotImplementedException();
}

public bool Contains(EntityBucket<TGroup, TSubEntity> item)
public bool Remove(EntityBucket<TGroup, TSubEntity> entity)
{
throw new NotImplementedException();
}

public void CopyTo(EntityBucket<TGroup, TSubEntity>[] array, int arrayIndex)
{
throw new NotImplementedException();
}

public bool Remove(EntityBucket<TGroup, TSubEntity> item)
{
throw new NotImplementedException();
}

public IEnumerator<EntityBucket<TGroup, TSubEntity>> GetEnumerator()
{
var result = GetEntries().Select(e => e.Entity);
using (var enumerator = result.GetEnumerator())
{
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
6 changes: 3 additions & 3 deletions src/MongoFramework/Infrastructure/EntityWriterPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class EntityWriterPipeline<TEntity> : IEntityWriterPipeline<TEntity> wher
{
public IMongoDbConnection Connection { get; }
private ICommandWriter<TEntity> CommandWriter { get; }
private List<IEntityCollection<TEntity>> ManagedCollections { get; } = new List<IEntityCollection<TEntity>>();
private List<IEntityCollectionBase<TEntity>> ManagedCollections { get; } = new List<IEntityCollectionBase<TEntity>>();
private List<IWriteCommand<TEntity>> StagedCommands { get; } = new List<IWriteCommand<TEntity>>();

public EntityWriterPipeline(IMongoDbConnection connection)
Expand All @@ -23,7 +23,7 @@ public EntityWriterPipeline(IMongoDbConnection connection)
CommandWriter = new CommandWriter<TEntity>(connection);
}

public void AddCollection(IEntityCollection<TEntity> collection)
public void AddCollection(IEntityCollectionBase<TEntity> collection)
{
if (collection == null)
{
Expand All @@ -35,7 +35,7 @@ public void AddCollection(IEntityCollection<TEntity> collection)
ManagedCollections.Add(collection);
}
}
public void RemoveCollection(IEntityCollection<TEntity> collection)
public void RemoveCollection(IEntityCollectionBase<TEntity> collection)
{
if (collection == null)
{
Expand Down
8 changes: 7 additions & 1 deletion src/MongoFramework/Infrastructure/IEntityCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

namespace MongoFramework.Infrastructure
{
public interface IEntityCollection<TEntity> : ICollection<TEntity> where TEntity : class
public interface IEntityCollectionBase<TEntity> where TEntity : class
{
EntityEntry<TEntity> GetEntry(TEntity entity);
IEnumerable<EntityEntry<TEntity>> GetEntries();
void Update(TEntity entity, EntityEntryState state);
bool Remove(TEntity entity);
}

public interface IEntityCollection<TEntity> : IEntityCollectionBase<TEntity>, ICollection<TEntity> where TEntity : class
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace MongoFramework.Infrastructure
{
public interface IEntityNavigationCollection
public interface IEntityNavigationCollectionBase
{
void SetConnection(IMongoDbConnection connection);
void AddForeignId(object foreignId);
void AddForeignIds(IEnumerable<object> foreignIds);
void LoadEntities();
}

public interface IEntityNavigationCollection<TEntity> : IEntityNavigationCollection, IEntityCollection<TEntity> where TEntity : class
public interface IEntityNavigationCollection<TEntity> : IEntityNavigationCollectionBase, IEntityCollection<TEntity> where TEntity : class
{

}
Expand Down
4 changes: 2 additions & 2 deletions src/MongoFramework/Infrastructure/IEntityWriterPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace MongoFramework.Infrastructure
public interface IEntityWriterPipeline<TEntity> where TEntity : class
{
IMongoDbConnection Connection { get; }
void AddCollection(IEntityCollection<TEntity> collection);
void RemoveCollection(IEntityCollection<TEntity> collection);
void AddCollection(IEntityCollectionBase<TEntity> collection);
void RemoveCollection(IEntityCollectionBase<TEntity> collection);
void StageCommand(IWriteCommand<TEntity> command);
void ClearStaging();
void Write();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public void MutateEntity(TEntity entity, MutatorType mutationType, IMongoDbConne
else if (mutationType == MutatorType.Create && relationship.IsCollection)
{
var navigationCollectionType = typeof(EntityNavigationCollection<>).MakeGenericType(relationship.EntityType);
var navigationCollection = Activator.CreateInstance(navigationCollectionType, relationship.IdProperty) as IEntityNavigationCollection;
var navigationCollection = Activator.CreateInstance(navigationCollectionType, relationship.IdProperty) as IEntityNavigationCollectionBase;
navigationCollection.SetConnection(connection);
relationship.NavigationProperty.SetValue(entity, navigationCollection);
}
else if (mutationType == MutatorType.Select && relationship.IsCollection)
{
if (relationship.NavigationProperty.GetValue(entity) is IEntityNavigationCollection navigationCollection)
if (relationship.NavigationProperty.GetValue(entity) is IEntityNavigationCollectionBase navigationCollection)
{
navigationCollection.SetConnection(connection);
}
Expand Down

0 comments on commit 35a0da0

Please sign in to comment.