Skip to content

Commit

Permalink
Merge pull request #58 from TurnerSoftware/mapping-overhaul
Browse files Browse the repository at this point in the history
Mapping Overhaul
  • Loading branch information
Turnerj committed May 13, 2019
2 parents 27a84c9 + 2a0ea49 commit 35c5b5e
Show file tree
Hide file tree
Showing 119 changed files with 1,508 additions and 1,558 deletions.
15 changes: 7 additions & 8 deletions README.md
Expand Up @@ -70,12 +70,8 @@ Populates the property with the current date/time on insert. _Note: The property

Populates the property with the current date/time on update. _Note: The property must be of type `DateTime`_

`[IncrementNumber(int incrementAmount = 1, bool onUpdateOnly = false)]`

Updates the value of a property by the defined increment amount on insert or update.

## Example
```
```csharp
using MongoFramework;
using System.ComponentModel.DataAnnotations;

Expand All @@ -88,16 +84,19 @@ public class MyEntity

public class MyContext : MongoDbContext
{
public MyContext() : base("MyContext") { }
public MyContext(IMongoDbConnection connection) : base(connection) { }
public MongoDbSet<MyEntity> MyEntities { get; set; }
public MongoDbSet<MyOtherEntity> MyOtherEntities { get; set; }
}

using (var myContext = new MyContext())
...

var connection = MongoDbConnection.FromConnectionString("YOUR_CONNECTION_STRING");
using (var myContext = new MyContext(connection))
{
var myEntity = myContext.MyEntities.Where(myEntity => myEntity.Name == "James").FirstOrDefault();
myEntity.Address = "123 SomeAddress Road, SomeSuburb";
myContext.SaveChanges();
await myContext.SaveChangesAsync();
}

```
Expand Up @@ -7,6 +7,7 @@
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoFramework.Infrastructure;
using MongoFramework.Infrastructure.Diagnostics;
using StackExchange.Profiling;

namespace MongoFramework.Profiling.MiniProfiler
Expand Down
3 changes: 2 additions & 1 deletion src/MongoFramework/Attributes/CreatedDateAttribute.cs
@@ -1,12 +1,13 @@
using System;
using System.Reflection;
using MongoFramework.Infrastructure.Mapping;

namespace MongoFramework.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class CreatedDateAttribute : MutatePropertyAttribute
{
public override void OnInsert(object target, PropertyInfo property)
public override void OnInsert(object target, IEntityProperty property)
{
if (property.PropertyType != typeof(DateTime))
{
Expand Down
48 changes: 0 additions & 48 deletions src/MongoFramework/Attributes/IncrementNumberAttribute.cs

This file was deleted.

5 changes: 2 additions & 3 deletions src/MongoFramework/Attributes/IndexAttribute.cs
@@ -1,10 +1,9 @@
using MongoFramework.Infrastructure.Indexing;
using System;
using System;

namespace MongoFramework.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class IndexAttribute : Attribute, IEntityIndex
public class IndexAttribute : Attribute
{
public string Name { get; private set; }
public bool IsUnique { get; set; }
Expand Down
7 changes: 4 additions & 3 deletions src/MongoFramework/Attributes/MutatePropertyAttribute.cs
@@ -1,12 +1,13 @@
using System;
using System.Reflection;
using MongoFramework.Infrastructure.Mapping;

namespace MongoFramework.Attributes
{
public abstract class MutatePropertyAttribute : Attribute
{
public virtual void OnInsert(object target, PropertyInfo property) { }
public virtual void OnUpdate(object target, PropertyInfo property) { }
public virtual void OnSelect(object target, PropertyInfo property) { }
public virtual void OnInsert(object target, IEntityProperty property) { }
public virtual void OnUpdate(object target, IEntityProperty property) { }
public virtual void OnSelect(object target, IEntityProperty property) { }
}
}
3 changes: 2 additions & 1 deletion src/MongoFramework/Attributes/UpdatedDateAttribute.cs
@@ -1,12 +1,13 @@
using System;
using System.Reflection;
using MongoFramework.Infrastructure.Mapping;

namespace MongoFramework.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class UpdatedDateAttribute : CreatedDateAttribute
{
public override void OnUpdate(object target, PropertyInfo property)
public override void OnUpdate(object target, IEntityProperty property)
{
if (property.PropertyType != typeof(DateTime))
{
Expand Down
6 changes: 2 additions & 4 deletions src/MongoFramework/IMongoDbConnection.cs
@@ -1,15 +1,13 @@
using System;
using MongoDB.Driver;
using MongoFramework.Infrastructure;
using MongoFramework.Infrastructure.Indexing;
using MongoFramework.Infrastructure.Mapping;

namespace MongoFramework
{
public interface IMongoDbConnection : IEntityMapperFactory, IEntityIndexMapperFactory, IDisposable
public interface IMongoDbConnection : IDisposable
{
IMongoClient Client { get; }
IMongoDatabase GetDatabase();
IDiagnosticListener DiagnosticListener { get; }
IDiagnosticListener DiagnosticListener { set; get; }
}
}
22 changes: 22 additions & 0 deletions src/MongoFramework/Infrastructure/Commands/AddEntityCommand.cs
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;

namespace MongoFramework.Infrastructure.Commands
{
public class AddEntityCommand<TEntity> : IWriteCommand<TEntity> where TEntity : class
{
private EntityEntry<TEntity> EntityEntry { get; }

public AddEntityCommand(EntityEntry<TEntity> entityEntry)
{
EntityEntry = entityEntry;
}

public IEnumerable<WriteModel<TEntity>> GetModel()
{
yield return new InsertOneModel<TEntity>(EntityEntry.Entity);
}
}
}
12 changes: 12 additions & 0 deletions src/MongoFramework/Infrastructure/Commands/IWriteCommand.cs
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;

namespace MongoFramework.Infrastructure.Commands
{
public interface IWriteCommand<TEntity> where TEntity : class
{
IEnumerable<WriteModel<TEntity>> GetModel();
}
}
33 changes: 33 additions & 0 deletions src/MongoFramework/Infrastructure/Commands/UpdateEntityCommand.cs
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;

namespace MongoFramework.Infrastructure.Commands
{
public class UpdateEntityCommand<TEntity> : IWriteCommand<TEntity> where TEntity : class
{
private EntityEntry<TEntity> EntityEntry { get; }

public UpdateEntityCommand(EntityEntry<TEntity> entityEntry)
{
EntityEntry = entityEntry;
}

public IEnumerable<WriteModel<TEntity>> GetModel()
{
//var idFieldValue = EntityMapper.GetIdValue(entry.Entity);
//var filter = Builders<TEntity>.Filter.Eq(idFieldName, idFieldValue);
//var updateDefintion = UpdateDefinitionHelper.CreateFromDiff<TEntity>(EntityEntry.OriginalValues, EntityEntry.CurrentValues);

////MongoDB doesn't like it if an UpdateDefinition is empty.
////This is primarily to work around a mutation that may set an entity to its default state.
//if (updateDefintion.HasChanges())
//{
// writeModel.Add(new UpdateOneModel<TEntity>(filter, updateDefintion));
//}
//yield return new InsertOneModel<TEntity>(EntityEntry.Entity);
yield break;
}
}
}
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using MongoFramework.Infrastructure.Internal;

namespace MongoFramework.Infrastructure.DefinitionHelpers
{
Expand Down
Expand Up @@ -3,7 +3,7 @@
using MongoDB.Driver;
using MongoFramework.Infrastructure.Linq;

namespace MongoFramework.Infrastructure
namespace MongoFramework.Infrastructure.Diagnostics
{
public abstract class DiagnosticCommand
{
Expand Down
@@ -1,6 +1,6 @@
using System;

namespace MongoFramework.Infrastructure
namespace MongoFramework.Infrastructure.Diagnostics
{
public class NoOpDiagnosticListener : IDiagnosticListener
{
Expand Down
7 changes: 2 additions & 5 deletions src/MongoFramework/Infrastructure/EntityBucketCollection.cs
@@ -1,24 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MongoFramework.Infrastructure.Mapping;

namespace MongoFramework.Infrastructure
{
public class EntityBucketCollection<TGroup, TSubEntity> where TGroup : class
{
private Dictionary<TGroup, List<TSubEntity>> SubEntityStaging { get; }
private IEntityReader<EntityBucket<TGroup, TSubEntity>> EntityReader { get; }
private IEntityMapper EntityMapper { get; }

public int BucketSize { get; }

public EntityBucketCollection(IEntityReader<EntityBucket<TGroup, TSubEntity>> entityReader, int bucketSize, IEntityMapper entityMapper)
public EntityBucketCollection(IEntityReader<EntityBucket<TGroup, TSubEntity>> entityReader, int bucketSize)
{
SubEntityStaging = new Dictionary<TGroup, List<TSubEntity>>(new ShallowPropertyEqualityComparer<TGroup>());
EntityReader = entityReader;
BucketSize = bucketSize;
EntityMapper = entityMapper;
}

public void AddEntity(TGroup group, TSubEntity entity)
Expand All @@ -35,7 +32,7 @@ public void AddEntity(TGroup group, TSubEntity entity)

public IEntityCollection<EntityBucket<TGroup, TSubEntity>> AsEntityCollection()
{
var entityCollection = new EntityCollection<EntityBucket<TGroup, TSubEntity>>(EntityMapper);
var entityCollection = new EntityCollection<EntityBucket<TGroup, TSubEntity>>();

foreach (var grouping in SubEntityStaging)
{
Expand Down
3 changes: 0 additions & 3 deletions src/MongoFramework/Infrastructure/EntityChangeTracker.cs
@@ -1,12 +1,9 @@
using System.Linq;
using MongoFramework.Infrastructure.Mapping;

namespace MongoFramework.Infrastructure
{
public class EntityChangeTracker<TEntity> : EntityCollection<TEntity>, IEntityChangeTracker<TEntity> where TEntity : class
{
public EntityChangeTracker(IEntityMapper entityMapper) : base(entityMapper) { }

public void DetectChanges()
{
var entries = Entries.Where(e => e.State == EntityEntryState.NoChanges || e.State == EntityEntryState.Updated);
Expand Down
24 changes: 16 additions & 8 deletions src/MongoFramework/Infrastructure/EntityCollection.cs
Expand Up @@ -10,11 +10,11 @@ public class EntityCollection<TEntity> : IEntityCollection<TEntity> where TEntit
{
protected List<EntityEntry<TEntity>> Entries { get; } = new List<EntityEntry<TEntity>>();

private IEntityMapper EntityMapper { get; }
private IEntityDefinition EntityDefinition { get; }

public EntityCollection(IEntityMapper entityMapper)
public EntityCollection()
{
EntityMapper = entityMapper;
EntityDefinition = EntityMapping.GetOrCreateDefinition(typeof(TEntity));
}

public int Count => Entries.Count;
Expand All @@ -23,8 +23,8 @@ public EntityCollection(IEntityMapper entityMapper)

public EntityEntry<TEntity> GetEntry(TEntity entity)
{
var entityId = EntityMapper.GetIdValue(entity);
var defaultIdValue = EntityMapper.GetDefaultId();
var entityId = EntityDefinition.GetIdValue(entity);
var defaultIdValue = EntityDefinition.GetDefaultId();

foreach (var entry in Entries)
{
Expand All @@ -34,7 +34,7 @@ public EntityEntry<TEntity> GetEntry(TEntity entity)
}
else
{
var entryEntityId = EntityMapper.GetIdValue(entry.Entity);
var entryEntityId = EntityDefinition.GetIdValue(entry.Entity);
if (!Equals(entryEntityId, defaultIdValue) && entryEntityId.Equals(entityId))
{
return entry;
Expand Down Expand Up @@ -89,8 +89,16 @@ public void Clear()

public void Add(TEntity item)
{
//TODO: Check the ID value is a default value - if not, mark it as non-changed
Update(item, EntityEntryState.Added);
var defaultId = EntityDefinition.GetDefaultId();
var entityId = EntityDefinition.GetIdValue(item);
if (Equals(entityId, defaultId))
{
Update(item, EntityEntryState.Added);
}
else
{
Update(item, EntityEntryState.NoChanges);
}
}

public bool Contains(TEntity item)
Expand Down

0 comments on commit 35c5b5e

Please sign in to comment.