149 lines
4.4 KiB
C#
149 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using EnVisage.Code.BLL;
|
|
using EnVisage.Code.DAL.Mongo;
|
|
using EnVisage.Properties;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using MongoDB.Driver.Builders;
|
|
|
|
namespace EnVisage.Code.DAL
|
|
{
|
|
public class MongoDataContext
|
|
{
|
|
#region Fields and Constants
|
|
private static readonly MongoDataContext Instance = new MongoDataContext();
|
|
private static IMongoClient _client;
|
|
private static IMongoDatabase _db;
|
|
|
|
#region GaExport collections
|
|
public const string COLL_NAME_MIXES = "Mix";
|
|
public const string COLL_NAME_TRANSACTIONS = "Transaction";
|
|
public const string COLL_NAME_EXP_ALLOCATIONS = "ExpAllocation";
|
|
public const string COLL_NAME_TEAM_ALLOCATIONS = "TeamAllocation";
|
|
public const string COLL_NAME_RESOURCE_ALLOCATIONS = "ResourceAllocation";
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
private MongoDataContext()
|
|
{
|
|
if (string.IsNullOrEmpty(AppSettingsManager.MongoDBConnectionString))
|
|
throw new ConfigurationErrorsException("There is no MongoDBConnectionString configuration key.");
|
|
|
|
_client = new MongoClient(AppSettingsManager.MongoDBConnectionString);
|
|
_db = _client.GetDatabase(Settings.Default.MongoDBName);
|
|
}
|
|
// Explicit static constructor to tell C# compiler
|
|
// not to mark type as beforefieldinit
|
|
static MongoDataContext()
|
|
{
|
|
//RecreateIndexes();
|
|
}
|
|
#endregion
|
|
|
|
#region Generic methods
|
|
public static async void InsertOneAsync<T>(T entity, string collectionName) where T : class, new()
|
|
{
|
|
var collecion = GetCollection<T>(collectionName);
|
|
if (collecion == null)
|
|
throw new MongoCollectionNotExistsException(collectionName);
|
|
await collecion.InsertOneAsync(entity);
|
|
}
|
|
public static void InsertOne<T>(T entity, string collectionName) where T : class, new()
|
|
{
|
|
var collecion = GetCollection<T>(collectionName);
|
|
if (collecion == null)
|
|
throw new MongoCollectionNotExistsException(collectionName);
|
|
collecion.InsertOneAsync(entity).GetAwaiter().GetResult();
|
|
}
|
|
public static async void DropCollectionAsync<T>()
|
|
{
|
|
await _db.DropCollectionAsync(typeof(T).Name);
|
|
}
|
|
public static void DropCollection<T>()
|
|
{
|
|
_db.DropCollectionAsync(typeof(T).Name).GetAwaiter().GetResult();
|
|
}
|
|
public static IMongoCollection<T> GetCollection<T>(string name)
|
|
{
|
|
if (string.IsNullOrEmpty(name))
|
|
throw new ArgumentNullException("name");
|
|
|
|
return string.IsNullOrEmpty(name) ? null : _db.GetCollection<T>(name);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Collections
|
|
public static IMongoCollection<Mongo.Mix> Mixes
|
|
{
|
|
get
|
|
{
|
|
var collection = GetCollection<Mongo.Mix>(COLL_NAME_MIXES);
|
|
if (collection == null)
|
|
throw new MongoCollectionNotExistsException(COLL_NAME_MIXES);
|
|
return collection;
|
|
}
|
|
}
|
|
public static IMongoCollection<MixExpenditureAllocation> ExpenditureAllocations
|
|
{
|
|
get
|
|
{
|
|
var collection = GetCollection<MixExpenditureAllocation>(COLL_NAME_EXP_ALLOCATIONS);
|
|
if (collection == null)
|
|
throw new MongoCollectionNotExistsException(COLL_NAME_EXP_ALLOCATIONS);
|
|
return collection;
|
|
}
|
|
}
|
|
public static IMongoCollection<MixTeamAllocation> TeamAllocations
|
|
{
|
|
get
|
|
{
|
|
var collection = GetCollection<MixTeamAllocation>(COLL_NAME_TEAM_ALLOCATIONS);
|
|
if (collection == null)
|
|
throw new MongoCollectionNotExistsException(COLL_NAME_TEAM_ALLOCATIONS);
|
|
return collection;
|
|
}
|
|
}
|
|
public static IMongoCollection<MixResourceAllocation> ResourceAllocations
|
|
{
|
|
get
|
|
{
|
|
var collection = GetCollection<MixResourceAllocation>(COLL_NAME_RESOURCE_ALLOCATIONS);
|
|
if (collection == null)
|
|
throw new MongoCollectionNotExistsException(COLL_NAME_RESOURCE_ALLOCATIONS);
|
|
return collection;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Index Management
|
|
private static void CreateIndexes()
|
|
{
|
|
//#region NGramms
|
|
//var collection = GetCollection<Mix>(COLL_NAME_MIXES);
|
|
//collection.Indexes.CreateOneAsync(new BsonDocumentIndexKeysDefinition<Mix>(), new IndexOptionsBuilder().SetUnique(true).SetName("hash"));
|
|
|
|
//#endregion
|
|
}
|
|
private static void DropIndexes()
|
|
{
|
|
var collection = GetCollection<Mongo.Mix>(COLL_NAME_MIXES);
|
|
collection.Indexes.DropAllAsync().GetAwaiter().GetResult();
|
|
}
|
|
/// <summary>
|
|
/// Drops and recreates indexes. Call it only once, do not call it in context constructor all the time.
|
|
/// </summary>
|
|
private static void RecreateIndexes()
|
|
{
|
|
DropIndexes();
|
|
CreateIndexes();
|
|
}
|
|
#endregion
|
|
}
|
|
} |