public static async Task Insert <T>(string collectionName, T value, NoSQLType type = NoSQLType.MongoDB, IClientSessionHandle clientSession = null, INoSQLDbConnection connection = null) where T : MongDBBaseModel { if (connection != null) { var collection = connection.Database.GetCollection <T>(collectionName); await collection.InsertOneAsync(value); } else { switch (type) { case NoSQLType.MongoDB: using (INoSQLDbConnection conn = new MongoSqlConnection(NoConnectionString, NoSqlDatabase)) { var collection = connection.Database.GetCollection <T>(collectionName); await collection.InsertOneAsync(value); } break; default: throw new Exception("Wrong type"); } } }
public static INoSQLDbConnection GetNoSQLConnection(string connectionString = "", string database = "", NoSQLType type = NoSQLType.MongoDB) { if ("".Equals(NoConnectionString)) { connectionString = NoConnectionString; } if ("".Equals(database)) { database = NoSqlDatabase; } switch (type) { case NoSQLType.MongoDB: return(new MongoSqlConnection(connectionString, database)); default: throw new Exception("Wrong type"); } }
public static async Task <IEnumerable <T> > Query <T>(string collectionName, Func <T, bool> query, NoSQLType type = NoSQLType.MongoDB, IClientSessionHandle clientSession = null, INoSQLDbConnection connection = null) { if (connection != null) { var collection = connection.Database.GetCollection <T>(collectionName); return(await((IMongoQueryable <T>)(collection.AsQueryable().Where(query))).ToListAsync()); } else { switch (type) { case NoSQLType.MongoDB: using (INoSQLDbConnection conn = new MongoSqlConnection(NoConnectionString, NoSqlDatabase)) { var collection = conn.Database.GetCollection <T>(collectionName); return(await((IMongoQueryable <T>)(collection.AsQueryable().Where(query))).ToListAsync()); } default: throw new Exception("Wrong type"); } } }