Replaced LiteDB's initialization with every DB call with Dependency Injection instead.
This commit is contained in:
@@ -1,46 +1,37 @@
|
||||
using CarCareTracker.External.Interfaces;
|
||||
using CarCareTracker.Helper;
|
||||
using CarCareTracker.Models;
|
||||
using LiteDB;
|
||||
|
||||
namespace CarCareTracker.External.Implementations
|
||||
{
|
||||
public class VehicleDataAccess: IVehicleDataAccess
|
||||
public class VehicleDataAccess : IVehicleDataAccess
|
||||
{
|
||||
private static string dbName = StaticHelper.DbName;
|
||||
private LiteDatabase db { get; set; }
|
||||
private static string tableName = "vehicles";
|
||||
public VehicleDataAccess(ILiteDBInjection liteDB)
|
||||
{
|
||||
db = liteDB.GetLiteDB();
|
||||
}
|
||||
public bool SaveVehicle(Vehicle vehicle)
|
||||
{
|
||||
using (var db = new LiteDatabase(dbName))
|
||||
{
|
||||
var table = db.GetCollection<Vehicle>(tableName);
|
||||
var result = table.Upsert(vehicle);
|
||||
return true;
|
||||
};
|
||||
var table = db.GetCollection<Vehicle>(tableName);
|
||||
var result = table.Upsert(vehicle);
|
||||
return true;
|
||||
}
|
||||
public bool DeleteVehicle(int vehicleId)
|
||||
{
|
||||
using (var db = new LiteDatabase(dbName))
|
||||
{
|
||||
var table = db.GetCollection<Vehicle>(tableName);
|
||||
return table.Delete(vehicleId);
|
||||
};
|
||||
var table = db.GetCollection<Vehicle>(tableName);
|
||||
return table.Delete(vehicleId);
|
||||
}
|
||||
public List<Vehicle> GetVehicles()
|
||||
{
|
||||
using (var db = new LiteDatabase(dbName))
|
||||
{
|
||||
var table = db.GetCollection<Vehicle>(tableName);
|
||||
return table.FindAll().ToList();
|
||||
};
|
||||
var table = db.GetCollection<Vehicle>(tableName);
|
||||
return table.FindAll().ToList();
|
||||
}
|
||||
public Vehicle GetVehicleById(int vehicleId)
|
||||
{
|
||||
using (var db = new LiteDatabase(dbName))
|
||||
{
|
||||
var table = db.GetCollection<Vehicle>(tableName);
|
||||
return table.FindById(vehicleId);
|
||||
};
|
||||
var table = db.GetCollection<Vehicle>(tableName);
|
||||
return table.FindById(vehicleId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user