refators
This commit is contained in:
		
							parent
							
								
									2bf96b1588
								
							
						
					
					
						commit
						581f17f448
					
				| @ -2,6 +2,7 @@ | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Linq.Expressions; | ||||
| using System.Reflection; | ||||
| using System.Threading; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| @ -17,6 +18,7 @@ namespace PoweredSoft.ObjectStorage.Core | ||||
|         Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); | ||||
|         Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); | ||||
|         Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); | ||||
|         IQueryable<TEntity> AsQueryable(); | ||||
|         IQueryable<TEntity> AsQueryable(); | ||||
|         List<PropertyInfo> GetObjectKeys(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -2,7 +2,7 @@ | ||||
| 
 | ||||
| namespace PoweredSoft.ObjectStorage.Core | ||||
| { | ||||
|     public interface IObjectStorageClient | ||||
|     public interface IObjectStorageContext | ||||
|     { | ||||
|         IObjectStorageCollection<TEntity> GetCollection<TEntity>(); | ||||
|     } | ||||
| @ -11,7 +11,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests | ||||
|         [Fact] | ||||
|         public void TestingGetCollection() | ||||
|         { | ||||
|             var objectStorageClient = MongoDatabaseFactory.GetObjectStorageClient(); | ||||
|             var objectStorageClient = MongoDatabaseFactory.GetObjectStorageContext(); | ||||
|             var collection = objectStorageClient.GetCollection<Contact>(); | ||||
|             Assert.NotNull(collection); | ||||
|             Assert.NotNull(collection.CollectionName); | ||||
|  | ||||
| @ -12,7 +12,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests | ||||
|         [Fact] | ||||
|         public async Task CreateUpdateThenDelete() | ||||
|         { | ||||
|             var osc = MongoDatabaseFactory.GetObjectStorageClient(); | ||||
|             var osc = MongoDatabaseFactory.GetObjectStorageContext(); | ||||
|             var collection = osc.GetCollection<Contact>(); | ||||
|             var contact = await collection.AddAsync(new Contact | ||||
|             { | ||||
| @ -37,7 +37,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests | ||||
|         [Fact] | ||||
|         public async Task TestGetAsync() | ||||
|         { | ||||
|             var osc = MongoDatabaseFactory.GetObjectStorageClient(); | ||||
|             var osc = MongoDatabaseFactory.GetObjectStorageContext(); | ||||
|             var collection = osc.GetCollection<Contact>(); | ||||
|             var contact = await collection.AddAsync(new Contact | ||||
|             { | ||||
|  | ||||
| @ -12,9 +12,9 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests.Mock | ||||
|             return db; | ||||
|         } | ||||
| 
 | ||||
|         public static MongoObjectStorageClient GetObjectStorageClient() | ||||
|         public static MongoObjectStorageContext GetObjectStorageContext() | ||||
|         { | ||||
|             return new MongoObjectStorageClient(GetDatabase()); | ||||
|             return new MongoObjectStorageContext(GetDatabase()); | ||||
|         } | ||||
| 
 | ||||
|         public static IMongoClient GetClient() | ||||
|  | ||||
| @ -39,7 +39,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB | ||||
| 
 | ||||
|         protected virtual Expression<Func<TEntity, bool>> CreateEntityExpression(TEntity entity) | ||||
|         { | ||||
|             var objectKey = GetKeyProperty(); | ||||
|             var objectKey = GetBsonIdProperty(); | ||||
|             var constant = objectKey.GetValue(entity); | ||||
|             var expression = QueryableHelpers.CreateConditionExpression<TEntity>(objectKey.Name, | ||||
|                 DynamicLinq.ConditionOperators.Equal, constant, DynamicLinq.QueryConvertStrategy.LeaveAsIs); | ||||
| @ -47,7 +47,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB | ||||
|             return expression; | ||||
|         } | ||||
| 
 | ||||
|         protected virtual PropertyInfo GetKeyProperty() | ||||
|         protected virtual PropertyInfo GetBsonIdProperty() | ||||
|         { | ||||
|             var objectKey = typeof(TEntity) | ||||
|                             .GetProperties(BindingFlags.Public | BindingFlags.Instance) | ||||
| @ -82,11 +82,19 @@ namespace PoweredSoft.ObjectStorage.MongoDB | ||||
| 
 | ||||
|         public Task<TEntity> GetAsync(object key, CancellationToken cancellationToken = default(CancellationToken)) | ||||
|         { | ||||
|             var keyProp = GetKeyProperty(); | ||||
|             var keyProp = GetBsonIdProperty(); | ||||
|             var expression = QueryableHelpers.CreateConditionExpression<TEntity>(keyProp.Name, | ||||
|                 DynamicLinq.ConditionOperators.Equal, key, DynamicLinq.QueryConvertStrategy.LeaveAsIs); | ||||
|             var result = Collection.Find(expression).FirstOrDefaultAsync(); | ||||
|             return result; | ||||
|         } | ||||
| 
 | ||||
|         public List<PropertyInfo> GetObjectKeys() | ||||
|         { | ||||
|             return new List<PropertyInfo>() | ||||
|             { | ||||
|                 GetBsonIdProperty() | ||||
|             }; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -5,9 +5,9 @@ using PoweredSoft.ObjectStorage.Core; | ||||
| 
 | ||||
| namespace PoweredSoft.ObjectStorage.MongoDB | ||||
| { | ||||
|     public class MongoObjectStorageClient : IObjectStorageClient | ||||
|     public class MongoObjectStorageContext : IObjectStorageContext | ||||
|     { | ||||
|         public MongoObjectStorageClient(IMongoDatabase database) | ||||
|         public MongoObjectStorageContext(IMongoDatabase database) | ||||
|         { | ||||
|             Database = database; | ||||
|         } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user