dotnet-object-storage/PoweredSoft.ObjectStorage.MongoDB/MongoObjectStorageContext.cs
2019-02-12 23:39:55 -05:00

29 lines
920 B
C#

using System;
using System.Reflection;
using MongoDB.Driver;
using PoweredSoft.ObjectStorage.Core;
namespace PoweredSoft.ObjectStorage.MongoDB
{
public class MongoObjectStorageContext : IObjectStorageContext
{
public MongoObjectStorageContext(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;
}
}
}