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 Collection Names 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 entity, string collectionName) where T : class, new() { var collecion = GetCollection(collectionName); if (collecion == null) throw new MongoCollectionNotExistsException(collectionName); await collecion.InsertOneAsync(entity); } public static void InsertOne(T entity, string collectionName) where T : class, new() { var collecion = GetCollection(collectionName); if (collecion == null) throw new MongoCollectionNotExistsException(collectionName); collecion.InsertOneAsync(entity).GetAwaiter().GetResult(); } public static async void DropCollectionAsync() { await _db.DropCollectionAsync(typeof(T).Name); } public static void DropCollection() { _db.DropCollectionAsync(typeof(T).Name).GetAwaiter().GetResult(); } public static IMongoCollection GetCollection(string name) { if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name)); return string.IsNullOrEmpty(name) ? null : _db.GetCollection(name); } #endregion #region Collections public static IMongoCollection Mixes { get { var collection = GetCollection(COLL_NAME_MIXES); if (collection == null) throw new MongoCollectionNotExistsException(COLL_NAME_MIXES); return collection; } } public static IMongoCollection ExpenditureAllocations { get { var collection = GetCollection(COLL_NAME_EXP_ALLOCATIONS); if (collection == null) throw new MongoCollectionNotExistsException(COLL_NAME_EXP_ALLOCATIONS); return collection; } } public static IMongoCollection TeamAllocations { get { var collection = GetCollection(COLL_NAME_TEAM_ALLOCATIONS); if (collection == null) throw new MongoCollectionNotExistsException(COLL_NAME_TEAM_ALLOCATIONS); return collection; } } public static IMongoCollection ResourceAllocations { get { var collection = GetCollection(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(COLL_NAME_MIXES); //collection.Indexes.CreateOneAsync(new BsonDocumentIndexKeysDefinition(), new IndexOptionsBuilder().SetUnique(true).SetName("hash")); //#endregion } private static void DropIndexes() { var collection = GetCollection(COLL_NAME_MIXES); collection.Indexes.DropAllAsync().GetAwaiter().GetResult(); } /// /// Drops and recreates indexes. Call it only once, do not call it in context constructor all the time. /// private static void RecreateIndexes() { DropIndexes(); CreateIndexes(); } #endregion } }