dotnet-object-storage/PoweredSoft.ObjectStorage.MongoDB/MongoObjectStorageContext.cs

29 lines
920 B
C#
Raw Normal View History

2019-02-12 17:39:16 -05:00
using System;
using System.Reflection;
using MongoDB.Driver;
using PoweredSoft.ObjectStorage.Core;
namespace PoweredSoft.ObjectStorage.MongoDB
{
2019-02-12 23:39:55 -05:00
public class MongoObjectStorageContext : IObjectStorageContext
2019-02-12 17:39:16 -05:00
{
2019-02-12 23:39:55 -05:00
public MongoObjectStorageContext(IMongoDatabase database)
2019-02-12 17:39:16 -05:00
{
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;
}
}
}