dotnet-object-storage/PoweredSoft.ObjectStorage.MongoDB/MongoObjectStorageClient.cs
David Lebee b789b367c2 initial
2019-02-12 17:39:16 -05:00

29 lines
917 B
C#

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;
}
}
}