This commit is contained in:
David Lebee 2019-02-12 23:39:55 -05:00
parent 2bf96b1588
commit 581f17f448
7 changed files with 22 additions and 12 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -17,6 +18,7 @@ namespace PoweredSoft.ObjectStorage.Core
Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
IQueryable<TEntity> AsQueryable(); IQueryable<TEntity> AsQueryable();
List<PropertyInfo> GetObjectKeys();
} }
} }

View File

@ -2,7 +2,7 @@
namespace PoweredSoft.ObjectStorage.Core namespace PoweredSoft.ObjectStorage.Core
{ {
public interface IObjectStorageClient public interface IObjectStorageContext
{ {
IObjectStorageCollection<TEntity> GetCollection<TEntity>(); IObjectStorageCollection<TEntity> GetCollection<TEntity>();
} }

View File

@ -11,7 +11,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests
[Fact] [Fact]
public void TestingGetCollection() public void TestingGetCollection()
{ {
var objectStorageClient = MongoDatabaseFactory.GetObjectStorageClient(); var objectStorageClient = MongoDatabaseFactory.GetObjectStorageContext();
var collection = objectStorageClient.GetCollection<Contact>(); var collection = objectStorageClient.GetCollection<Contact>();
Assert.NotNull(collection); Assert.NotNull(collection);
Assert.NotNull(collection.CollectionName); Assert.NotNull(collection.CollectionName);

View File

@ -12,7 +12,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests
[Fact] [Fact]
public async Task CreateUpdateThenDelete() public async Task CreateUpdateThenDelete()
{ {
var osc = MongoDatabaseFactory.GetObjectStorageClient(); var osc = MongoDatabaseFactory.GetObjectStorageContext();
var collection = osc.GetCollection<Contact>(); var collection = osc.GetCollection<Contact>();
var contact = await collection.AddAsync(new Contact var contact = await collection.AddAsync(new Contact
{ {
@ -37,7 +37,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests
[Fact] [Fact]
public async Task TestGetAsync() public async Task TestGetAsync()
{ {
var osc = MongoDatabaseFactory.GetObjectStorageClient(); var osc = MongoDatabaseFactory.GetObjectStorageContext();
var collection = osc.GetCollection<Contact>(); var collection = osc.GetCollection<Contact>();
var contact = await collection.AddAsync(new Contact var contact = await collection.AddAsync(new Contact
{ {

View File

@ -12,9 +12,9 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests.Mock
return db; return db;
} }
public static MongoObjectStorageClient GetObjectStorageClient() public static MongoObjectStorageContext GetObjectStorageContext()
{ {
return new MongoObjectStorageClient(GetDatabase()); return new MongoObjectStorageContext(GetDatabase());
} }
public static IMongoClient GetClient() public static IMongoClient GetClient()

View File

@ -39,7 +39,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB
protected virtual Expression<Func<TEntity, bool>> CreateEntityExpression(TEntity entity) protected virtual Expression<Func<TEntity, bool>> CreateEntityExpression(TEntity entity)
{ {
var objectKey = GetKeyProperty(); var objectKey = GetBsonIdProperty();
var constant = objectKey.GetValue(entity); var constant = objectKey.GetValue(entity);
var expression = QueryableHelpers.CreateConditionExpression<TEntity>(objectKey.Name, var expression = QueryableHelpers.CreateConditionExpression<TEntity>(objectKey.Name,
DynamicLinq.ConditionOperators.Equal, constant, DynamicLinq.QueryConvertStrategy.LeaveAsIs); DynamicLinq.ConditionOperators.Equal, constant, DynamicLinq.QueryConvertStrategy.LeaveAsIs);
@ -47,7 +47,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB
return expression; return expression;
} }
protected virtual PropertyInfo GetKeyProperty() protected virtual PropertyInfo GetBsonIdProperty()
{ {
var objectKey = typeof(TEntity) var objectKey = typeof(TEntity)
.GetProperties(BindingFlags.Public | BindingFlags.Instance) .GetProperties(BindingFlags.Public | BindingFlags.Instance)
@ -82,11 +82,19 @@ namespace PoweredSoft.ObjectStorage.MongoDB
public Task<TEntity> GetAsync(object key, CancellationToken cancellationToken = default(CancellationToken)) public Task<TEntity> GetAsync(object key, CancellationToken cancellationToken = default(CancellationToken))
{ {
var keyProp = GetKeyProperty(); var keyProp = GetBsonIdProperty();
var expression = QueryableHelpers.CreateConditionExpression<TEntity>(keyProp.Name, var expression = QueryableHelpers.CreateConditionExpression<TEntity>(keyProp.Name,
DynamicLinq.ConditionOperators.Equal, key, DynamicLinq.QueryConvertStrategy.LeaveAsIs); DynamicLinq.ConditionOperators.Equal, key, DynamicLinq.QueryConvertStrategy.LeaveAsIs);
var result = Collection.Find(expression).FirstOrDefaultAsync(); var result = Collection.Find(expression).FirstOrDefaultAsync();
return result; return result;
} }
public List<PropertyInfo> GetObjectKeys()
{
return new List<PropertyInfo>()
{
GetBsonIdProperty()
};
}
} }
} }

View File

@ -5,9 +5,9 @@ using PoweredSoft.ObjectStorage.Core;
namespace PoweredSoft.ObjectStorage.MongoDB namespace PoweredSoft.ObjectStorage.MongoDB
{ {
public class MongoObjectStorageClient : IObjectStorageClient public class MongoObjectStorageContext : IObjectStorageContext
{ {
public MongoObjectStorageClient(IMongoDatabase database) public MongoObjectStorageContext(IMongoDatabase database)
{ {
Database = database; Database = database;
} }