Update the repository pattern

This commit is contained in:
Sébastien André
2019-11-10 19:31:55 +01:00
parent 3f1d648fa9
commit b95eedbf7c
19 changed files with 112 additions and 73 deletions

View File

@@ -11,7 +11,7 @@ namespace skydiveLogs_api.Data
{
public class AircraftRepository : IAircraftRepository
{
public IEnumerable<Aircraft> GetAllAircrafts()
public IEnumerable<Aircraft> GetAll()
{
IEnumerable<Aircraft> result = new List<Aircraft>();
@@ -25,7 +25,7 @@ namespace skydiveLogs_api.Data
return result;
}
public Aircraft GetAircraftById(int id)
public Aircraft GetById(int id)
{
Aircraft result;

View File

@@ -11,21 +11,18 @@ namespace skydiveLogs_api.Data
{
public class DropZoneRepository : IDropZoneRepository
{
public IEnumerable<DropZone> GetAllDzs()
public DropZoneRepository(IDataProvider dataProvider)
{
IEnumerable<DropZone> result = new List<DropZone>();
using (var db = new LiteDatabase(@".\Data\MyData.db"))
{
var col = db.GetCollection<DropZone>("DropZone");
result = col.FindAll().ToList();
}
return result;
_dataProvider = dataProvider;
_col = _dataProvider.GetCollection<DropZone>();
}
public DropZone GetDzById(int id)
public IEnumerable<DropZone> GetAll()
{
return _col.FindAll().ToList();
}
public DropZone GetById(int id)
{
DropZone result;
@@ -38,5 +35,9 @@ namespace skydiveLogs_api.Data
return result;
}
private readonly IDataProvider _dataProvider;
private readonly LiteCollection<DropZone> _col;
}
}

View File

@@ -11,7 +11,7 @@ namespace skydiveLogs_api.Data
{
public class GearRepository : IGearRepository
{
public IEnumerable<Gear> GetAllGears()
public IEnumerable<Gear> GetAll()
{
IEnumerable<Gear> result = new List<Gear>();
@@ -25,7 +25,7 @@ namespace skydiveLogs_api.Data
return result;
}
public Gear GetGearById(int id)
public Gear GetById(int id)
{
Gear result;

View File

@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using skydiveLogs_api.Model;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Data.Interface
{
public interface IAircraftRepository
public interface IAircraftRepository : IRepository<Aircraft>
{
IEnumerable<Aircraft> GetAllAircrafts();
Aircraft GetAircraftById(int id);
}
}

View File

@@ -0,0 +1,12 @@
using LiteDB;
namespace skydiveLogs_api.Data.Interface
{
public interface IDataProvider
{
LiteCollection<T> GetCollection<T>();
void Close();
}
}

View File

@@ -1,14 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using skydiveLogs_api.Model;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Data.Interface
{
public interface IDropZoneRepository
public interface IDropZoneRepository : IRepository<DropZone>
{
IEnumerable<DropZone> GetAllDzs();
DropZone GetDzById(int id);
}
}

View File

@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Data.Interface
{
public interface IGearRepository
public interface IGearRepository : IRepository<Gear>
{
}
}

View File

@@ -1,14 +1,10 @@
using System.Collections.Generic;
using skydiveLogs_api.Model;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Data.Interface
{
public interface IJumpRepository
public interface IJumpRepository : IRepository<Jump>
{
IEnumerable<Jump> GetAllJumps();
Jump GetJumpById(int id);
bool AddJump(Jump newJump);
}
}

View File

@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using skydiveLogs_api.Model;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Data.Interface
{
public interface IJumpTypeRepository
public interface IJumpTypeRepository : IRepository<JumpType>
{
IEnumerable<JumpType> GetAllJumpTypes();
JumpType GetJumpTypeById(int id);
}
}

View File

@@ -0,0 +1,12 @@
using System.Collections.Generic;
namespace skydiveLogs_api.Data.Interface
{
public interface IRepository<T>
{
IEnumerable<T> GetAll();
T GetById(int id);
}
}

View File

@@ -11,7 +11,7 @@ namespace skydiveLogs_api.Data
{
public class JumpRepository : IJumpRepository
{
public IEnumerable<Jump> GetAllJumps()
public IEnumerable<Jump> GetAll()
{
IEnumerable<Jump> result = new List<Jump>();
@@ -25,7 +25,7 @@ namespace skydiveLogs_api.Data
return result;
}
public Jump GetJumpById(int id)
public Jump GetById(int id)
{
Jump result;

View File

@@ -11,7 +11,7 @@ namespace skydiveLogs_api.Data
{
public class JumpTypeRepository : IJumpTypeRepository
{
public IEnumerable<JumpType> GetAllJumpTypes()
public IEnumerable<JumpType> GetAll()
{
IEnumerable<JumpType> result = new List<JumpType>();
@@ -25,7 +25,7 @@ namespace skydiveLogs_api.Data
return result;
}
public JumpType GetJumpTypeById(int id)
public JumpType GetById(int id)
{
JumpType result;

View File

@@ -0,0 +1,27 @@
using LiteDB;
using skydiveLogs_api.Data.Interface;
namespace skydiveLogs_api.Data
{
public class LiteDbProvider : IDataProvider
{
public LiteDbProvider(string connectionString)
{
_db = new LiteDatabase(connectionString);
}
public LiteCollection<T> GetCollection<T>()
{
return _db.GetCollection<T>();
}
public void Close()
{
_db.Dispose();
}
private readonly LiteDatabase _db;
}
}