Refactoring Repositories

This commit is contained in:
francesco 2020-08-29 15:47:12 +02:00
parent ebcb1c5781
commit 7f6b916549
5 changed files with 116 additions and 126 deletions

View File

@ -116,7 +116,6 @@
<Compile Include="Repository\IRepositoryBase.cs" /> <Compile Include="Repository\IRepositoryBase.cs" />
<Compile Include="Repository\LocationRepository.cs" /> <Compile Include="Repository\LocationRepository.cs" />
<Compile Include="Repository\LoggingRepository.cs" /> <Compile Include="Repository\LoggingRepository.cs" />
<Compile Include="Repository\RepositoryBase.cs" />
<Compile Include="Validators\CustomerNumberValidationRule.cs" /> <Compile Include="Validators\CustomerNumberValidationRule.cs" />
<Compile Include="Validators\EmailValidationRule.cs" /> <Compile Include="Validators\EmailValidationRule.cs" />
<Compile Include="Validators\IntRangeValidationRule.cs" /> <Compile Include="Validators\IntRangeValidationRule.cs" />

View File

@ -6,44 +6,56 @@ using System.Windows;
namespace LoggingClient.Repository namespace LoggingClient.Repository
{ {
public class CustomerRepository : RepositoryBase<Customer> public class CustomerRepository : IRepositoryBase<Customer>
{ {
public CustomerRepository(string connectionString) : base(connectionString) public CustomerRepository(string connectionString)
{ {
this.ConnectionString = connectionString;
Customers = new List<Customer>(); Customers = new List<Customer>();
} }
protected string ConnectionString { get; }
public override string TableName => "customer"; public string TableName => "customer";
public override string ColumnsForSelect => "customer_id, firstname, lastname, customernumber, kundenkonto_fk, tel, email, url, password"; public string ColumnsForSelect => "customer_id, firstname, lastname, customernumber, kundenkonto_fk, tel, email, url, password";
public override string ColumnsForAdd => "firstname, lastname, customernumber, kundenkonto_fk, tel, email, url, password"; public string ColumnsForAdd => "firstname, lastname, customernumber, kundenkonto_fk, tel, email, url, password";
public override string PrimaryKeyFromTable => "customer_id"; public string PrimaryKeyFromTable => "customer_id";
public List<Customer> Customers { get; set; } public List<Customer> Customers { get; set; }
public override void Add(Customer entity) public void Add(Customer entity)
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }
public override void CallStoredProcedure(Customer entity) public void CallStoredProcedure(Customer entity)
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }
public override void Delete(Customer entity) public long Count(string whereCondition, Dictionary<string, object> parameterValues)
{
throw new NotImplementedException();
}
public long Count()
{
throw new NotImplementedException();
}
public void Delete(Customer entity)
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }
public override List<Customer> GetAll(string whereCondition, Dictionary<string, object> parameterValues) public List<Customer> GetAll(string whereCondition, Dictionary<string, object> parameterValues)
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }
public override List<Customer> GetAll() public List<Customer> GetAll()
{ {
try try
{ {
@ -78,12 +90,12 @@ namespace LoggingClient.Repository
return Customers; return Customers;
} }
public override Customer GetSingle<P>(P pkValue) public Customer GetSingle<P>(P pkValue)
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }
public override void Update(Customer entity) public void Update(Customer entity)
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }

View File

@ -6,20 +6,22 @@ using System.Windows;
namespace LoggingClient.Repository namespace LoggingClient.Repository
{ {
public class LocationRepository : RepositoryBase<Location> public class LocationRepository : IRepositoryBase<Location>
{ {
public override string TableName => "Location"; public string TableName => "Location";
public override string ColumnsForSelect => "location_id, parentId, address_fk, designation, building, room"; public string ColumnsForSelect => "location_id, parentId, address_fk, designation, building, room";
public override string ColumnsForAdd => "parentId, address_fk, designation, building, room"; public string ColumnsForAdd => "parentId, address_fk, designation, building, room";
public override string PrimaryKeyFromTable => "location_id"; public string PrimaryKeyFromTable => "location_id";
public List<Location> Locations { get; set; } public List<Location> Locations { get; set; }
public Location _Locations { get; set; } public Location _Locations { get; set; }
public LocationRepository(string connectionString) : base(connectionString) protected string ConnectionString { get; }
public LocationRepository(string connectionString)
{ {
this.ConnectionString = connectionString;
Locations = new List<Location>(); Locations = new List<Location>();
} }
public override void Add(Location location) public void Add(Location location)
{ {
try try
{ {
@ -42,7 +44,7 @@ namespace LoggingClient.Repository
} }
} }
public override void Delete(Location location) public void Delete(Location location)
{ {
try try
{ {
@ -62,7 +64,7 @@ namespace LoggingClient.Repository
} }
} }
public override List<Location> GetAll(string whereCondition, Dictionary<string, object> parameterValues) public List<Location> GetAll(string whereCondition, Dictionary<string, object> parameterValues)
{ {
var whereCon = whereCondition; var whereCon = whereCondition;
if (parameterValues.Count > 0 && whereCondition != null) if (parameterValues.Count > 0 && whereCondition != null)
@ -103,7 +105,7 @@ namespace LoggingClient.Repository
return Locations; return Locations;
} }
public override List<Location> GetAll() public List<Location> GetAll()
{ {
try try
{ {
@ -136,12 +138,12 @@ namespace LoggingClient.Repository
return Locations; return Locations;
} }
public override void CallStoredProcedure(Location entity) public void CallStoredProcedure(Location entity)
{ {
throw new System.NotSupportedException(); throw new System.NotSupportedException();
} }
public override Location GetSingle<P>(P pkValue) public Location GetSingle<P>(P pkValue)
{ {
try try
{ {
@ -174,7 +176,7 @@ namespace LoggingClient.Repository
return _Locations; return _Locations;
} }
public override void Update(Location location) public void Update(Location location)
{ {
try try
{ {
@ -194,5 +196,15 @@ namespace LoggingClient.Repository
MessageBox.Show("Error occurred: " + ex.Message); MessageBox.Show("Error occurred: " + ex.Message);
} }
} }
public long Count(string whereCondition, Dictionary<string, object> parameterValues)
{
throw new NotImplementedException();
}
public long Count()
{
throw new NotImplementedException();
}
} }
} }

View File

@ -12,22 +12,25 @@ using LoggingClient.Model;
namespace LoggingClient.Repository namespace LoggingClient.Repository
{ {
class LoggingRepository : RepositoryBase<Logging> class LoggingRepository : IRepositoryBase<Logging>
{ {
public override string TableName => "v_logentries"; public string TableName => "v_logentries";
public override string ColumnsForSelect => "id, pod, location, hostname, severity, timestamp, message"; public string ColumnsForSelect => "id, pod, location, hostname, severity, timestamp, message";
public override string ColumnsForAdd { get; } public string ColumnsForAdd { get; }
public override string PrimaryKeyFromTable => "id"; public string PrimaryKeyFromTable => "id";
public List<Logging> Logs { get; set; } public List<Logging> Logs { get; set; }
public Logging _Logs { get; set; } public Logging _Logs { get; set; }
public LoggingRepository(string connectionString) : base(connectionString) protected string ConnectionString { get; }
public LoggingRepository(string connectionString)
{ {
ConnectionString = connectionString;
Logs = new List<Logging>(); Logs = new List<Logging>();
} }
public override Logging GetSingle<P>(P pkValue) public Logging GetSingle<P>(P pkValue)
{ {
try try
{ {
@ -60,7 +63,7 @@ namespace LoggingClient.Repository
} }
return _Logs; return _Logs;
} }
public override void Add(Logging newLogModelEntry) public void Add(Logging newLogModelEntry)
{ {
try try
{ {
@ -86,15 +89,15 @@ namespace LoggingClient.Repository
MessageBox.Show("Error occurred: " + ex.Message); MessageBox.Show("Error occurred: " + ex.Message);
} }
} }
public override void Delete(Logging entity) public void Delete(Logging entity)
{ {
throw new System.NotSupportedException(); throw new System.NotSupportedException();
} }
public override void Update(Logging entity) public void Update(Logging entity)
{ {
throw new System.NotSupportedException(); throw new System.NotSupportedException();
} }
public override List<Logging> GetAll(string whereCondition, Dictionary<string, object> parameterValues) public List<Logging> GetAll(string whereCondition, Dictionary<string, object> parameterValues)
{ {
var whereCon = whereCondition; var whereCon = whereCondition;
if (parameterValues.Count > 0 && whereCondition != null) if (parameterValues.Count > 0 && whereCondition != null)
@ -135,7 +138,7 @@ namespace LoggingClient.Repository
} }
return Logs; return Logs;
} }
public override List<Logging> GetAll() public List<Logging> GetAll()
{ {
try try
{ {
@ -168,7 +171,7 @@ namespace LoggingClient.Repository
} }
return Logs; return Logs;
} }
public override void CallStoredProcedure(Logging logModelEntry) public void CallStoredProcedure(Logging logModelEntry)
{ {
try try
{ {
@ -189,5 +192,55 @@ namespace LoggingClient.Repository
MessageBox.Show(ex.ToString()); MessageBox.Show(ex.ToString());
} }
} }
public long Count(string whereCondition, Dictionary<string, object> parameterValues)
{
var whereCon = whereCondition;
if (parameterValues.Count > 0 && whereCondition != null)
{
foreach (KeyValuePair<string, object> p in parameterValues)
{
whereCon = whereCon.Replace($"@{p.Key}", p.Value.ToString());
}
}
try
{
using (var conn = new MySqlConnection(this.ConnectionString))
{
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = $"select count(*) from {this.TableName} where {whereCon}";
return (long)cmd.ExecuteScalar();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error occurred: " + ex.Message);
return -1;
}
}
public long Count()
{
try
{
using (var conn = new MySqlConnection(this.ConnectionString))
{
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = $"select count(*) from {this.TableName}";
return (long)cmd.ExecuteScalar();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error occurred: " + ex.Message);
return -1;
}
}
} }
} }

View File

@ -1,86 +0,0 @@
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace LoggingClient.Repository
{
public abstract class RepositoryBase<M> : IRepositoryBase<M>
{
protected RepositoryBase(string connectionString)
{
this.ConnectionString = connectionString;
}
protected string ConnectionString { get; }
public abstract M GetSingle<P>(P pkValue);
public abstract void Add(M entity);
public abstract void Delete(M entity);
public abstract void Update(M entity);
public abstract List<M> GetAll(string whereCondition, Dictionary<string, object> parameterValues);
public abstract List<M> GetAll();
public abstract void CallStoredProcedure(M entity);
public IQueryable<M> Query(string whereCondition, Dictionary<string, object> parameterValues)
{
throw new System.NotSupportedException();
}
public long Count(string whereCondition, Dictionary<string, object> parameterValues)
{
var whereCon = whereCondition;
if (parameterValues.Count > 0 && whereCondition != null)
{
foreach (KeyValuePair<string, object> p in parameterValues)
{
whereCon = whereCon.Replace($"@{p.Key}", p.Value.ToString());
}
}
try
{
using (var conn = new MySqlConnection(this.ConnectionString))
{
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = $"select count(*) from {this.TableName} where {whereCon}";
return (long)cmd.ExecuteScalar();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error occurred: " + ex.Message);
return -1;
}
}
public long Count()
{
try
{
using (var conn = new MySqlConnection(this.ConnectionString))
{
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = $"select count(*) from {this.TableName}";
return (long)cmd.ExecuteScalar();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error occurred: " + ex.Message);
return -1;
}
}
public abstract string TableName { get; }
public abstract string ColumnsForSelect { get; }
public abstract string ColumnsForAdd { get; }
public abstract string PrimaryKeyFromTable { get; }
}
}