Skip to content

Commit

Permalink
Formatted all files
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Apr 12, 2022
1 parent af769c6 commit 18ea1bd
Show file tree
Hide file tree
Showing 67 changed files with 2,175 additions and 2,263 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;

namespace YuckQi.Data.DocumentDb.MongoDb.Attributes
namespace YuckQi.Data.DocumentDb.MongoDb.Attributes;

[AttributeUsage(AttributeTargets.Class)]
public class CollectionAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Class)]
public class CollectionAttribute : Attribute
{
public String Name { get; }
public String Name { get; }

public CollectionAttribute(String name)
{
Name = name;
}
public CollectionAttribute(String name)
{
Name = name;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;

namespace YuckQi.Data.DocumentDb.MongoDb.Attributes
namespace YuckQi.Data.DocumentDb.MongoDb.Attributes;

[AttributeUsage(AttributeTargets.Class)]
public class DatabaseAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Class)]
public class DatabaseAttribute : Attribute
{
public String Name { get; }
public String Name { get; }

public DatabaseAttribute(String name)
{
Name = name;
}
public DatabaseAttribute(String name)
{
Name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,62 @@
using MongoDB.Driver;
using YuckQi.Data.DocumentDb.MongoDb.Attributes;

namespace YuckQi.Data.DocumentDb.MongoDb.Extensions
namespace YuckQi.Data.DocumentDb.MongoDb.Extensions;

public static class DocumentModelExtensions
{
public static class DocumentModelExtensions
{
#region Constants
#region Constants

private const String DefaultObjectIdPropertyName = "_id";
private const String DefaultObjectIdPropertyName = "_id";

#endregion
#endregion


#region Private Members
#region Private Members

private static readonly ConcurrentDictionary<Type, String> CollectionNameByType = new ConcurrentDictionary<Type, String>();
private static readonly ConcurrentDictionary<Type, String> DatabaseNameByType = new ConcurrentDictionary<Type, String>();
private static readonly ConcurrentDictionary<Type, PropertyInfo> KeyByType = new ConcurrentDictionary<Type, PropertyInfo>();
private static readonly ConcurrentDictionary<Type, String> CollectionNameByType = new();
private static readonly ConcurrentDictionary<Type, String> DatabaseNameByType = new();
private static readonly ConcurrentDictionary<Type, PropertyInfo> KeyByType = new();

#endregion
#endregion


#region Extension Methods
#region Extension Methods

public static String GetCollectionName(this Type type) => type != null ? CollectionNameByType.GetOrAdd(type, key => GetCollectionAttribute(key)?.Name ?? key.Name) : null;
public static String GetCollectionName(this Type type) => type != null ? CollectionNameByType.GetOrAdd(type, key => GetCollectionAttribute(key)?.Name ?? key.Name) : null;

public static String GetDatabaseName(this Type type) => type != null ? DatabaseNameByType.GetOrAdd(type, key => GetDatabaseAttribute(key)?.Name) : null;
public static String GetDatabaseName(this Type type) => type != null ? DatabaseNameByType.GetOrAdd(type, key => GetDatabaseAttribute(key)?.Name) : null;

public static TKey? GetKey<TDocument, TKey>(this TDocument document) where TKey : struct => document != null ? GetKeyPropertyInfo(typeof(TDocument))?.GetValue(document) as TKey? : null;
public static TKey? GetKey<TDocument, TKey>(this TDocument document) where TKey : struct => document != null ? GetKeyPropertyInfo(typeof(TDocument))?.GetValue(document) as TKey? : null;

public static StringFieldDefinition<TDocument, TKey?> GetKeyFieldDefinition<TDocument, TKey>(this Type type) where TKey : struct
{
if (type == null)
return null;
// This isn't great since it should be enforced at compile time
if (type != typeof(TDocument))
throw new ArgumentException($"Type of '{type.FullName}' must match '{typeof(TDocument)}'.");
public static StringFieldDefinition<TDocument, TKey?> GetKeyFieldDefinition<TDocument, TKey>(this Type type) where TKey : struct
{
if (type == null)
return null;
// This isn't great since it should be enforced at compile time
if (type != typeof(TDocument))
throw new ArgumentException($"Type of '{type.FullName}' must match '{typeof(TDocument)}'.");

var propertyInfo = GetKeyPropertyInfo(typeof(TDocument));
var field = new StringFieldDefinition<TDocument, TKey?>(propertyInfo?.Name);
var propertyInfo = GetKeyPropertyInfo(typeof(TDocument));
var field = new StringFieldDefinition<TDocument, TKey?>(propertyInfo?.Name);

return field;
}
return field;
}

#endregion
#endregion


#region Supporting Methods
#region Supporting Methods

private static CollectionAttribute GetCollectionAttribute(MemberInfo type) => type.GetCustomAttribute(typeof(CollectionAttribute)) as CollectionAttribute;
private static CollectionAttribute GetCollectionAttribute(MemberInfo type) => type.GetCustomAttribute(typeof(CollectionAttribute)) as CollectionAttribute;

private static DatabaseAttribute GetDatabaseAttribute(MemberInfo type) => type.GetCustomAttribute(typeof(DatabaseAttribute)) as DatabaseAttribute;
private static DatabaseAttribute GetDatabaseAttribute(MemberInfo type) => type.GetCustomAttribute(typeof(DatabaseAttribute)) as DatabaseAttribute;

private static PropertyInfo GetKeyPropertyInfo(Type type) => type != null ? KeyByType.GetOrAdd(type, KeyPropertyInfoValueFactory) : null;
private static PropertyInfo GetKeyPropertyInfo(Type type) => type != null ? KeyByType.GetOrAdd(type, KeyPropertyInfoValueFactory) : null;

private static PropertyInfo KeyPropertyInfoValueFactory(Type type) => type.GetProperties()
.Select(t => t.GetCustomAttribute<BsonIdAttribute>() != null ? t : null)
.SingleOrDefault(t => t != null) ?? type.GetProperty(DefaultObjectIdPropertyName);
private static PropertyInfo KeyPropertyInfoValueFactory(Type type) => type.GetProperties()
.Select(t => t.GetCustomAttribute<BsonIdAttribute>() != null ? t : null)
.SingleOrDefault(t => t != null) ?? type.GetProperty(DefaultObjectIdPropertyName);

#endregion
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,46 @@
using MongoDB.Driver;
using YuckQi.Data.Filtering;

namespace YuckQi.Data.DocumentDb.MongoDb.Extensions
namespace YuckQi.Data.DocumentDb.MongoDb.Extensions;

public static class FilterDefinitionExtensions
{
public static class FilterDefinitionExtensions
public static FilterDefinition<TDocument> ToFilterDefinition<TDocument>(this IEnumerable<FilterCriteria> parameters)
{
public static FilterDefinition<TDocument> ToFilterDefinition<TDocument>(this IEnumerable<FilterCriteria> parameters)
{
if (parameters == null)
return null;
if (parameters == null)
return null;

var builder = Builders<TDocument>.Filter;
var result = new List<FilterDefinition<TDocument>>();
var builder = Builders<TDocument>.Filter;
var result = new List<FilterDefinition<TDocument>>();

foreach (var parameter in parameters)
foreach (var parameter in parameters)
{
var field = new StringFieldDefinition<TDocument, Object>(parameter.FieldName);
switch (parameter.Operation)
{
var field = new StringFieldDefinition<TDocument, Object>(parameter.FieldName);
switch (parameter.Operation)
{
case FilterOperation.Equal:
result.Add(builder.Eq(field, parameter.Value));
break;
case FilterOperation.GreaterThan:
result.Add(builder.Gt(field, parameter.Value));
break;
case FilterOperation.GreaterThanOrEqual:
result.Add(builder.Gte(field, parameter.Value));
break;
case FilterOperation.LessThan:
result.Add(builder.Lt(field, parameter.Value));
break;
case FilterOperation.LessThanOrEqual:
result.Add(builder.Lte(field, parameter.Value));
break;
case FilterOperation.NotEqual:
result.Add(builder.Not(builder.Eq(field, parameter.Value)));
break;
default:
throw new ArgumentOutOfRangeException();
}
case FilterOperation.Equal:
result.Add(builder.Eq(field, parameter.Value));
break;
case FilterOperation.GreaterThan:
result.Add(builder.Gt(field, parameter.Value));
break;
case FilterOperation.GreaterThanOrEqual:
result.Add(builder.Gte(field, parameter.Value));
break;
case FilterOperation.LessThan:
result.Add(builder.Lt(field, parameter.Value));
break;
case FilterOperation.LessThanOrEqual:
result.Add(builder.Lte(field, parameter.Value));
break;
case FilterOperation.NotEqual:
result.Add(builder.Not(builder.Eq(field, parameter.Value)));
break;
default:
throw new ArgumentOutOfRangeException();
}

return builder.And(result);
}

return builder.And(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
using YuckQi.Domain.Aspects.Abstract;
using YuckQi.Domain.Entities.Abstract;

namespace YuckQi.Data.DocumentDb.MongoDb.Handlers
namespace YuckQi.Data.DocumentDb.MongoDb.Handlers;

public class ActivationHandler<TEntity, TKey, TScope> : ActivationHandlerBase<TEntity, TKey, TScope> where TEntity : IEntity<TKey>, IActivated, IRevised where TKey : struct where TScope : IClientSessionHandle
{
public class ActivationHandler<TEntity, TKey, TScope> : ActivationHandlerBase<TEntity, TKey, TScope> where TEntity : IEntity<TKey>, IActivated, IRevised where TKey : struct where TScope : IClientSessionHandle
{
public ActivationHandler(IRevisionHandler<TEntity, TKey, TScope> reviser) : base(reviser) { }
}
public ActivationHandler(IRevisionHandler<TEntity, TKey, TScope> reviser) : base(reviser) { }
}
57 changes: 28 additions & 29 deletions src/YuckQi.Data.DocumentDb.MongoDb/Handlers/CreationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,49 @@
using YuckQi.Domain.Entities.Abstract;
using YuckQi.Extensions.Mapping.Abstractions;

namespace YuckQi.Data.DocumentDb.MongoDb.Handlers
{
public class CreationHandler<TEntity, TKey, TScope, TDocument> : CreationHandlerBase<TEntity, TKey, TScope, TDocument> where TEntity : IEntity<TKey>, ICreated where TKey : struct where TScope : IClientSessionHandle
{
#region Private Members
namespace YuckQi.Data.DocumentDb.MongoDb.Handlers;

private static readonly Type DocumentType = typeof(TDocument);
public class CreationHandler<TEntity, TKey, TScope, TDocument> : CreationHandlerBase<TEntity, TKey, TScope, TDocument> where TEntity : IEntity<TKey>, ICreated where TKey : struct where TScope : IClientSessionHandle
{
#region Private Members

#endregion
private static readonly Type DocumentType = typeof(TDocument);

#endregion

#region Constructors

public CreationHandler(IMapper mapper) : base(mapper) { }
#region Constructors

public CreationHandler(IMapper mapper, CreationOptions options) : base(mapper, options) { }
public CreationHandler(IMapper mapper) : base(mapper) { }

#endregion
public CreationHandler(IMapper mapper, CreationOptions options) : base(mapper, options) { }

#endregion

#region Protected Methods

protected override TKey? DoCreate(TEntity entity, TScope scope)
{
var database = scope.Client.GetDatabase(DocumentType.GetDatabaseName());
var collection = database.GetCollection<TDocument>(DocumentType.GetCollectionName());
var document = Mapper.Map<TDocument>(entity);
#region Protected Methods

collection.InsertOne(scope, document);
protected override TKey? DoCreate(TEntity entity, TScope scope)
{
var database = scope.Client.GetDatabase(DocumentType.GetDatabaseName());
var collection = database.GetCollection<TDocument>(DocumentType.GetCollectionName());
var document = Mapper.Map<TDocument>(entity);

return document.GetKey<TDocument, TKey>();
}
collection.InsertOne(scope, document);

protected override async Task<TKey?> DoCreateAsync(TEntity entity, TScope scope)
{
var database = scope.Client.GetDatabase(DocumentType.GetDatabaseName());
var collection = database.GetCollection<TDocument>(DocumentType.GetCollectionName());
var document = Mapper.Map<TDocument>(entity);
return document.GetKey<TDocument, TKey>();
}

await collection.InsertOneAsync(scope, document);
protected override async Task<TKey?> DoCreateAsync(TEntity entity, TScope scope)
{
var database = scope.Client.GetDatabase(DocumentType.GetDatabaseName());
var collection = database.GetCollection<TDocument>(DocumentType.GetCollectionName());
var document = Mapper.Map<TDocument>(entity);

return document.GetKey<TDocument, TKey>();
}
await collection.InsertOneAsync(scope, document);

#endregion
return document.GetKey<TDocument, TKey>();
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
using YuckQi.Domain.Aspects.Abstract;
using YuckQi.Domain.Entities.Abstract;

namespace YuckQi.Data.DocumentDb.MongoDb.Handlers
namespace YuckQi.Data.DocumentDb.MongoDb.Handlers;

public class LogicalDeletionHandler<TEntity, TKey, TScope> : LogicalDeletionHandlerBase<TEntity, TKey, TScope> where TEntity : IEntity<TKey>, IDeleted, IRevised where TKey : struct where TScope : IClientSessionHandle
{
public class LogicalDeletionHandler<TEntity, TKey, TScope> : LogicalDeletionHandlerBase<TEntity, TKey, TScope> where TEntity : IEntity<TKey>, IDeleted, IRevised where TKey : struct where TScope : IClientSessionHandle
{
public LogicalDeletionHandler(IRevisionHandler<TEntity, TKey, TScope> reviser) : base(reviser) { }
}
public LogicalDeletionHandler(IRevisionHandler<TEntity, TKey, TScope> reviser) : base(reviser) { }
}
Loading

0 comments on commit 18ea1bd

Please sign in to comment.