using MongoDB.Driver; using System; using System.Collections.Generic; namespace EnVisage.MongoDbMigration.DataAccess { public class AccessorBase where T : class, new() { #region Private Members private IMongoDatabase _database = null; private string _collectionName = null; #endregion #region Protected Members protected IMongoCollection Collection { get { return _database.GetCollection(_collectionName); } } #endregion #region Constructors public AccessorBase(IMongoDatabase database, string collectionName) { if (database == null) throw new ArgumentNullException("database"); if (string.IsNullOrWhiteSpace(collectionName)) throw new ArgumentNullException("collectionName"); _database = database; _collectionName = collectionName; } #endregion #region Public Generic Methods public void Insert(T entity) { Collection.InsertOneAsync(entity).GetAwaiter().GetResult(); } public void Insert(IEnumerable entities) { Collection.InsertManyAsync(entities).GetAwaiter().GetResult(); } public List GetAll() { return Collection.Find(x => true).ToListAsync().GetAwaiter().GetResult(); } #endregion } }