Start Implementation Generics - Repository

This commit is contained in:
Francesco 2020-07-22 13:32:23 +02:00
parent afbabd7869
commit 14346596b5
9 changed files with 246 additions and 14 deletions

View File

@ -78,7 +78,13 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Model\Model.cs" />
<Compile Include="Model\Location.cs" />
<Compile Include="Model\SeverityComboBoxItem.cs" />
<Compile Include="Repository\IRepositoryBase.cs" />
<Compile Include="Repository\LocationRepository.cs" />
<Compile Include="Repository\LoggingRepository.cs" />
<Compile Include="Repository\RepositoryBase.cs" />
<Compile Include="Validators\IntRangeValidationRule.cs" />
<Compile Include="ViewModel\Commands\RelayCommand.cs" />
<Compile Include="Validators\StringRangeValidationRule.cs" />
@ -93,7 +99,7 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Model\LogModel.cs" />
<Compile Include="Model\Logging.cs" />
<Compile Include="ViewModel\LogViewModel.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoggingClient.Model
{
class Location : Model
{
}
}

View File

@ -1,9 +1,8 @@
using System;
using DuplicateCheckerLib;
namespace LoggingClient.Model
{
public class LogModel : IEntity
public class Logging : Model
{
public int Id { get; set; }
public string Pod { get; set; }
@ -13,7 +12,7 @@ namespace LoggingClient.Model
public DateTime Timestamp { get; set; }
public string Message { get; set; }
public LogModel(int id, string pod, string location, string hostname, int severity, DateTime timestamp, string message)
public Logging(int id, string pod, string location, string hostname, int severity, DateTime timestamp, string message)
{
this.Id = id;
this.Pod = pod;
@ -23,7 +22,7 @@ namespace LoggingClient.Model
this.Timestamp = timestamp;
this.Message = message;
}
public bool Equals(LogModel secondLogModel)
public bool Equals(Logging secondLogModel)
{
if (Object.ReferenceEquals(null, secondLogModel)) return false;
if (Object.ReferenceEquals(this, secondLogModel)) return true;
@ -32,7 +31,7 @@ namespace LoggingClient.Model
}
public override bool Equals(object value)
{
return Equals(value as LogModel);
return Equals(value as Logging);
}
public override int GetHashCode()
{

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DuplicateCheckerLib;
namespace LoggingClient.Model
{
public class Model : IEntity
{
}
}

View File

@ -0,0 +1,76 @@
using System.Collections.Generic;
namespace LoggingClient.Repository
{
public interface IRepositoryBase<M>
{
/// <summary>
/// Liefert ein einzelnes Model-Objekt vom Typ M zurück,
/// welches anhand dem übergebenen PrimaryKey geladen wird.
/// </summary>
/// <typeparam name="P">Type des PrimaryKey</typeparam>
/// <param name="pkValue">Wert des PrimaryKey</param>
/// <returns>gefundenes Model-Objekt, ansonsten null</returns>
M GetSingle<P>(P pkValue);
/// <summary>
/// Fügt das Model-Objekt zur Datenbank hinzu (Insert)
/// </summary>
/// <param name="entity">zu speicherndes Model-Object</param>
void Add(M entity);
/// <summary>
/// Löscht das Model-Objekt aus der Datenbank (Delete)
/// </summary>
/// <param name="entity">zu löschendes Model-Object</param>
void Delete(M entity);
/// <summary>
/// Aktualisiert das Model-Objekt in der Datenbank hinzu (Update)
/// </summary>
/// <param name="entity">zu aktualisierendes Model-Object</param>
void Update(M entity);
/// <summary>
/// Gibt eine Liste von Model-Objekten vom Typ M zurück,
/// die gemäss der WhereBedingung geladen wurden. Die Werte der
/// Where-Bedingung können als separat übergeben werden,
/// damit diese für PreparedStatements verwendet werden können.
/// (Verhinderung von SQL-Injection)
/// </summary>
/// <param name="whereCondition">WhereBedingung als string
/// z.B. "NetPrice > @netPrice and Active = @active and Description like @desc</param>
/// <param name="parameterValues">Parameter-Werte für die Wherebedingung
/// bspw: {{"netPrice", 10.5}, {"active", true}, {"desc", "Wolle%"}}</param>
/// <returns></returns>
List<M> GetAll(string whereCondition, Dictionary<string, object> parameterValues);
/// <summary>
/// Gibt eine Liste aller in der DB vorhandenen Model-Objekte vom Typ M zurück
/// </summary>
/// <returns></returns>
List<M> GetAll();
/// <summary>
/// Zählt in der Datenbank die Anzahl Model-Objekte vom Typ M, die der
/// Where-Bedingung entsprechen
/// </summary>
/// <param name="whereCondition">WhereBedingung als string
/// z.B. "NetPrice > @netPrice and Active = @active and Description like @desc</param>
/// <param name="parameterValues">Parameter-Werte für die Wherebedingung
/// bspw: {{"netPrice", 10.5}, {"active", true}, {"desc", "Wolle%"}}</param>
/// <returns></returns>
long Count(string whereCondition, Dictionary<string, object> parameterValues);
/// <summary>
/// Zählt alle Model-Objekte vom Typ M
/// </summary>
/// <returns></returns>
long Count();
/// <summary>
/// Gibt den Tabellennamen zurück, auf die sich das Repository bezieht
/// </summary>
string TableName { get; }
}
}

View File

@ -0,0 +1,50 @@
using LoggingClient.Model;
using System.Collections.Generic;
namespace LoggingClient.Repository
{
class LocationRepository : RepositoryBase<Location>
{
public override string TableName => throw new System.NotImplementedException();
public override void Add(Location entity)
{
throw new System.NotImplementedException();
}
public override long Count(string whereCondition, Dictionary<string, object> parameterValues)
{
throw new System.NotImplementedException();
}
public override long Count()
{
throw new System.NotImplementedException();
}
public override void Delete(Location entity)
{
throw new System.NotImplementedException();
}
public override List<Location> GetAll(string whereCondition, Dictionary<string, object> parameterValues)
{
throw new System.NotImplementedException();
}
public override List<Location> GetAll()
{
throw new System.NotImplementedException();
}
public override Location GetSingle<P>(P pkValue)
{
throw new System.NotImplementedException();
}
public override void Update(Location entity)
{
throw new System.NotImplementedException();
}
}
}

View File

@ -0,0 +1,54 @@
using LoggingClient.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoggingClient.Repository
{
class LoggingRepository : RepositoryBase<Logging>
{
public override string TableName => throw new NotImplementedException();
public override void Add(Logging entity)
{
throw new NotImplementedException();
}
public override long Count(string whereCondition, Dictionary<string, object> parameterValues)
{
throw new NotImplementedException();
}
public override long Count()
{
throw new NotImplementedException();
}
public override void Delete(Logging entity)
{
throw new NotImplementedException();
}
public override List<Logging> GetAll(string whereCondition, Dictionary<string, object> parameterValues)
{
throw new NotImplementedException();
}
public override List<Logging> GetAll()
{
throw new NotImplementedException();
}
public override Logging GetSingle<P>(P pkValue)
{
throw new NotImplementedException();
}
public override void Update(Logging entity)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoggingClient.Repository
{
public abstract class RepositoryBase<M> : IRepositoryBase<M>
{
public abstract string TableName { get; }
public abstract void Add(M entity);
public abstract long Count(string whereCondition, Dictionary<string, object> parameterValues);
public abstract long Count();
public abstract void Delete(M entity);
public abstract List<M> GetAll(string whereCondition, Dictionary<string, object> parameterValues);
public abstract List<M> GetAll();
public abstract M GetSingle<P>(P pkValue);
public abstract void Update(M entity);
}
}

16
LoggingClient/LoggingClient/ViewModel/LogViewModel.cs Normal file → Executable file
View File

@ -26,7 +26,7 @@ namespace LoggingClient.ViewModel
private ICommand _btnAdddataClick;
private ICommand _btnFindDuplicateClick;
public ObservableCollection<LogModel> Logs
public ObservableCollection<Logging> Logs
{
get => _logs;
set
@ -35,15 +35,15 @@ namespace LoggingClient.ViewModel
OnPropertyChanged("Logs");
}
}
private ObservableCollection<LogModel> _logs;
private ObservableCollection<Logging> _logs;
public ObservableCollection<SeverityComboBoxItem> SeverityComboBox { get; set; }
public LogViewModel()
{
TxtConnectionString = "Server=localhost;Database=inventarisierungsloesung;Uid=root;Pwd=foekicoe9i4kpos;";
TxtConnectionString = "Server=localhost;Database=inventarisierungsloesung;Uid=root;Pwd=MySQLPassword1234!;";
_enterSeverity = 1;
Logs = new ObservableCollection<LogModel>();
Logs = new ObservableCollection<Logging>();
SeverityComboBox = new ObservableCollection<SeverityComboBoxItem>(){
new SeverityComboBoxItem(){Id=1, Severity= 1},
new SeverityComboBoxItem(){Id=2, Severity= 2},
@ -51,7 +51,7 @@ namespace LoggingClient.ViewModel
};
_duplicateChecker = new DuplicateChecker();
}
public LogModel MySelectedItem { get; set; }
public Logging MySelectedItem { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public SeverityComboBoxItem SetSeverity { get; set; }
@ -177,7 +177,7 @@ namespace LoggingClient.ViewModel
{
location = reader.GetString("location");
}
Logs.Add(new LogModel(
Logs.Add(new Logging(
reader.GetInt32("id"),
reader.GetString("pod"),
location,
@ -247,12 +247,12 @@ namespace LoggingClient.ViewModel
MessageBox.Show("Error occurred: " + ex.Message);
}
}
public ObservableCollection<LogModel> BtnFindDuplicate_Click()
public ObservableCollection<Logging> BtnFindDuplicate_Click()
{
LoadData();
var duplicateList = _duplicateChecker.FindDuplicates(Logs);
Logs = new ObservableCollection<LogModel>(duplicateList.Cast<LogModel>());
Logs = new ObservableCollection<Logging>(duplicateList.Cast<Logging>());
return Logs;
}