This commit is contained in:
David Lebee
2019-02-12 17:39:16 -05:00
commit b789b367c2
15 changed files with 364 additions and 0 deletions
@@ -0,0 +1,14 @@
using System;
namespace PoweredSoft.ObjectStorage.MongoDB
{
[AttributeUsage(AttributeTargets.Class)]
public class MongoCollectionAttribute : Attribute
{
public MongoCollectionAttribute(string name)
{
Name = name;
}
public string Name { get; }
}
}
@@ -0,0 +1,28 @@
using System;
using System.Reflection;
using MongoDB.Driver;
using PoweredSoft.ObjectStorage.Core;
namespace PoweredSoft.ObjectStorage.MongoDB
{
public class MongoObjectStorageClient : IObjectStorageClient
{
public MongoObjectStorageClient(IMongoDatabase database)
{
Database = database;
}
public IMongoDatabase Database { get; }
public IObjectStorageCollection<TEntity> GetCollection<TEntity>()
{
var attribute = typeof(TEntity).GetCustomAttribute<MongoCollectionAttribute>();
if (attribute == null)
throw new Exception("Must add MongoCollectionAttribute on entity class to use this method.");
var mongoCollection = Database.GetCollection<TEntity>(attribute.Name);
var ret = new MongoObjectStorageCollection<TEntity>(mongoCollection);
return ret;
}
}
}
@@ -0,0 +1,67 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using PoweredSoft.DynamicLinq.Helpers;
using PoweredSoft.ObjectStorage.Core;
namespace PoweredSoft.ObjectStorage.MongoDB
{
public class MongoObjectStorageCollection<TEntity> : IObjectStorageCollection<TEntity>
{
public MongoObjectStorageCollection(IMongoCollection<TEntity> collection)
{
if (collection == null)
throw new ArgumentNullException("collection");
Collection = collection;
}
public string CollectionName => Collection.CollectionNamespace.CollectionName;
public IMongoCollection<TEntity> Collection { get; }
public async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
await Collection.InsertOneAsync(entity, cancellationToken: cancellationToken);
return entity;
}
public IQueryable<TEntity> AsQueryable()
{
return Collection.AsQueryable();
}
protected virtual Expression<Func<TEntity, bool>> CreateEntityExpression(TEntity entity)
{
var objectKey = typeof(TEntity)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(t => t.GetCustomAttribute<BsonIdAttribute>() != null);
if (objectKey == null)
throw new Exception("Must have a BsonIdAttribute on one of the properties.");
var constant = objectKey.GetValue(entity);
var expression = QueryableHelpers.CreateConditionExpression<TEntity>(objectKey.Name,
DynamicLinq.ConditionOperators.Equal, constant, DynamicLinq.QueryConvertStrategy.LeaveAsIs);
return expression;
}
public async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
var expression = CreateEntityExpression(entity);
await Collection.DeleteOneAsync(expression);
}
public async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
var expression = CreateEntityExpression(entity);
await Collection.ReplaceOneAsync(expression, entity);
return entity;
}
}
}
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PoweredSoft.ObjectStorage.Core\PoweredSoft.ObjectStorage.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.7.3" />
<PackageReference Include="PoweredSoft.DynamicLinq" Version="1.1.7" />
</ItemGroup>
</Project>