63 lines
1.5 KiB
C#
63 lines
1.5 KiB
C#
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace EnVisage.MongoDbMigration.DataAccess
|
|
{
|
|
public class AccessorBase<T> where T : class, new()
|
|
{
|
|
#region Private Members
|
|
|
|
private IMongoDatabase _database = null;
|
|
private string _collectionName = null;
|
|
|
|
#endregion
|
|
|
|
#region Protected Members
|
|
|
|
protected IMongoCollection<T> Collection
|
|
{
|
|
get
|
|
{
|
|
return _database.GetCollection<T>(_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<T> entities)
|
|
{
|
|
Collection.InsertManyAsync(entities).GetAwaiter().GetResult();
|
|
}
|
|
|
|
public List<T> GetAll()
|
|
{
|
|
return Collection.Find(x => true).ToListAsync().GetAwaiter().GetResult();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |