Update IoC and the "GET" function in the repository (lock issue)
This commit is contained in:
@@ -11,32 +11,31 @@ namespace skydiveLogs_api.Data
|
||||
{
|
||||
public class AircraftRepository : IAircraftRepository
|
||||
{
|
||||
public AircraftRepository(IDataProvider dataProvider)
|
||||
{
|
||||
_dataProvider = dataProvider;
|
||||
_col = _dataProvider.GetCollection<Aircraft>();
|
||||
}
|
||||
|
||||
public IEnumerable<Aircraft> GetAll()
|
||||
{
|
||||
IEnumerable<Aircraft> result = new List<Aircraft>();
|
||||
var results = _col.FindAll().ToList();
|
||||
_dataProvider.Close();
|
||||
|
||||
using (var db = new LiteDatabase(@".\Data\MyData.db"))
|
||||
{
|
||||
var col = db.GetCollection<Aircraft>("Aircraft");
|
||||
|
||||
result = col.FindAll().ToList();
|
||||
}
|
||||
|
||||
return result;
|
||||
return results;
|
||||
}
|
||||
|
||||
public Aircraft GetById(int id)
|
||||
{
|
||||
Aircraft result;
|
||||
|
||||
using (var db = new LiteDatabase(@".\Data\MyData.db"))
|
||||
{
|
||||
var col = db.GetCollection<Aircraft>("Aircraft");
|
||||
|
||||
result = col.FindById(new BsonValue(id));
|
||||
}
|
||||
var result = _col.FindById(new BsonValue(id));
|
||||
_dataProvider.Close();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private readonly IDataProvider _dataProvider;
|
||||
|
||||
private readonly LiteCollection<Aircraft> _col;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,16 +24,7 @@ namespace skydiveLogs_api.Data
|
||||
|
||||
public DropZone GetById(int id)
|
||||
{
|
||||
DropZone result;
|
||||
|
||||
using (var db = new LiteDatabase(@".\Data\MyData.db"))
|
||||
{
|
||||
var col = db.GetCollection<DropZone>("DropZone");
|
||||
|
||||
result = col.FindById(new BsonValue(id));
|
||||
}
|
||||
|
||||
return result;
|
||||
return _col.FindById(new BsonValue(id)); ;
|
||||
}
|
||||
|
||||
private readonly IDataProvider _dataProvider;
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace skydiveLogs_api.Ioc
|
||||
{
|
||||
public class IocService
|
||||
{
|
||||
public IocService(IServiceCollection services)
|
||||
public IocService(IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_services = services;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public void Configure()
|
||||
@@ -28,9 +31,12 @@ namespace skydiveLogs_api.Ioc
|
||||
_services.AddScoped<Data.Interface.IJumpTypeRepository, Data.JumpTypeRepository>();
|
||||
_services.AddScoped<Data.Interface.IGearRepository, Data.GearRepository>();
|
||||
|
||||
_services.AddScoped<Data.Interface.IDataProvider, Data.LiteDbProvider>();
|
||||
string connectionString = _configuration.GetConnectionString("DefaultConnection");
|
||||
_services.AddScoped<Data.Interface.IDataProvider>(c => new Data.LiteDbProvider(connectionString));
|
||||
}
|
||||
|
||||
private readonly IServiceCollection _services;
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Extensions.Configuration.Abstractions">
|
||||
<HintPath>..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration.abstractions\2.2.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
|
||||
<HintPath>..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.dependencyinjection.abstractions\2.2.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
|
||||
7
Back/skydiveLogs-api.Model/DatabaseOptions.cs
Normal file
7
Back/skydiveLogs-api.Model/DatabaseOptions.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace skydiveLogs_api.Model
|
||||
{
|
||||
public class DatabaseOptions
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ namespace skydiveLogs_api
|
||||
});
|
||||
|
||||
// IoC
|
||||
var iocService = new IocService(services);
|
||||
var iocService = new IocService(services, Configuration);
|
||||
iocService.Configure();
|
||||
|
||||
services.AddAutoMapper(typeof(Mapper.ModelProfile));
|
||||
|
||||
@@ -6,6 +6,6 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"JumpsDatabase": "Filename=./Data/JumpsDb.db"
|
||||
"DefaultConnection": "Filename=./Data/JumpsDb.db"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Data\MyData.db" />
|
||||
<None Remove="Data\JumpsDb.db" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="Data\MyData.db">
|
||||
<Content Include="Data\JumpsDb.db">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user