37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
|
using Model;
|
||
|
using MongoDB.Driver;
|
||
|
|
||
|
namespace Repository
|
||
|
{
|
||
|
public class DataRepository
|
||
|
{
|
||
|
private static string COLLECTION_NAME = "data";
|
||
|
private string _connectionString = "mongodb://mongo:mongo@localhost:27017";
|
||
|
private string _databaseName = "mongo";
|
||
|
private IMongoCollection<DataModel> _dataCollection;
|
||
|
private MongoClient _mongoClient;
|
||
|
|
||
|
public DataRepository(string connectionString, string databaseName)
|
||
|
{
|
||
|
this._connectionString = connectionString;
|
||
|
this._databaseName = databaseName;
|
||
|
this._mongoClient = new MongoClient(this._connectionString);
|
||
|
|
||
|
var db = this._mongoClient.GetDatabase(this._databaseName);
|
||
|
this._dataCollection = db.GetCollection<DataModel>(COLLECTION_NAME);
|
||
|
}
|
||
|
|
||
|
public DataRepository()
|
||
|
{
|
||
|
this._mongoClient = new MongoClient(this._connectionString);
|
||
|
|
||
|
var db = this._mongoClient.GetDatabase(this._databaseName);
|
||
|
this._dataCollection = db.GetCollection<DataModel>(COLLECTION_NAME);
|
||
|
}
|
||
|
|
||
|
public async void Save(DataModel data)
|
||
|
{
|
||
|
await this._dataCollection.InsertOneAsync(data);
|
||
|
}
|
||
|
}
|
||
|
}
|