From 7f6b9165498f7117b69ed9b5d6b9149343ce582d Mon Sep 17 00:00:00 2001 From: francesco Date: Sat, 29 Aug 2020 15:47:12 +0200 Subject: [PATCH] Refactoring Repositories --- .../LoggingClient/LoggingClient.csproj | 1 - .../Repository/CustomerRepository.cs | 38 +++++--- .../Repository/LocationRepository.cs | 38 +++++--- .../Repository/LoggingRepository.cs | 79 ++++++++++++++--- .../Repository/RepositoryBase.cs | 86 ------------------- 5 files changed, 116 insertions(+), 126 deletions(-) delete mode 100644 LoggingClient/LoggingClient/Repository/RepositoryBase.cs diff --git a/LoggingClient/LoggingClient/LoggingClient.csproj b/LoggingClient/LoggingClient/LoggingClient.csproj index 7732333..27b2442 100644 --- a/LoggingClient/LoggingClient/LoggingClient.csproj +++ b/LoggingClient/LoggingClient/LoggingClient.csproj @@ -116,7 +116,6 @@ - diff --git a/LoggingClient/LoggingClient/Repository/CustomerRepository.cs b/LoggingClient/LoggingClient/Repository/CustomerRepository.cs index c2c6a78..3417855 100644 --- a/LoggingClient/LoggingClient/Repository/CustomerRepository.cs +++ b/LoggingClient/LoggingClient/Repository/CustomerRepository.cs @@ -6,44 +6,56 @@ using System.Windows; namespace LoggingClient.Repository { - public class CustomerRepository : RepositoryBase + public class CustomerRepository : IRepositoryBase { - public CustomerRepository(string connectionString) : base(connectionString) + public CustomerRepository(string connectionString) { + this.ConnectionString = connectionString; Customers = new List(); } + 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 Customers { get; set; } - public override void Add(Customer entity) + public void Add(Customer entity) { throw new System.NotImplementedException(); } - public override void CallStoredProcedure(Customer entity) + public void CallStoredProcedure(Customer entity) { throw new System.NotImplementedException(); } - public override void Delete(Customer entity) + public long Count(string whereCondition, Dictionary parameterValues) + { + throw new NotImplementedException(); + } + + public long Count() + { + throw new NotImplementedException(); + } + + public void Delete(Customer entity) { throw new System.NotImplementedException(); } - public override List GetAll(string whereCondition, Dictionary parameterValues) + public List GetAll(string whereCondition, Dictionary parameterValues) { throw new System.NotImplementedException(); } - public override List GetAll() + public List GetAll() { try { @@ -78,12 +90,12 @@ namespace LoggingClient.Repository return Customers; } - public override Customer GetSingle

(P pkValue) + public Customer GetSingle

(P pkValue) { throw new System.NotImplementedException(); } - public override void Update(Customer entity) + public void Update(Customer entity) { throw new System.NotImplementedException(); } diff --git a/LoggingClient/LoggingClient/Repository/LocationRepository.cs b/LoggingClient/LoggingClient/Repository/LocationRepository.cs index 55f1635..9998ca0 100644 --- a/LoggingClient/LoggingClient/Repository/LocationRepository.cs +++ b/LoggingClient/LoggingClient/Repository/LocationRepository.cs @@ -6,20 +6,22 @@ using System.Windows; namespace LoggingClient.Repository { - public class LocationRepository : RepositoryBase + public class LocationRepository : IRepositoryBase { - public override string TableName => "Location"; - public override string ColumnsForSelect => "location_id, parentId, address_fk, designation, building, room"; - public override string ColumnsForAdd => "parentId, address_fk, designation, building, room"; - public override string PrimaryKeyFromTable => "location_id"; + public string TableName => "Location"; + public string ColumnsForSelect => "location_id, parentId, address_fk, designation, building, room"; + public string ColumnsForAdd => "parentId, address_fk, designation, building, room"; + public string PrimaryKeyFromTable => "location_id"; public List 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(); } - public override void Add(Location location) + public void Add(Location location) { try { @@ -42,7 +44,7 @@ namespace LoggingClient.Repository } } - public override void Delete(Location location) + public void Delete(Location location) { try { @@ -62,7 +64,7 @@ namespace LoggingClient.Repository } } - public override List GetAll(string whereCondition, Dictionary parameterValues) + public List GetAll(string whereCondition, Dictionary parameterValues) { var whereCon = whereCondition; if (parameterValues.Count > 0 && whereCondition != null) @@ -103,7 +105,7 @@ namespace LoggingClient.Repository return Locations; } - public override List GetAll() + public List GetAll() { try { @@ -136,12 +138,12 @@ namespace LoggingClient.Repository return Locations; } - public override void CallStoredProcedure(Location entity) + public void CallStoredProcedure(Location entity) { throw new System.NotSupportedException(); } - public override Location GetSingle

(P pkValue) + public Location GetSingle

(P pkValue) { try { @@ -174,7 +176,7 @@ namespace LoggingClient.Repository return _Locations; } - public override void Update(Location location) + public void Update(Location location) { try { @@ -194,5 +196,15 @@ namespace LoggingClient.Repository MessageBox.Show("Error occurred: " + ex.Message); } } + + public long Count(string whereCondition, Dictionary parameterValues) + { + throw new NotImplementedException(); + } + + public long Count() + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/LoggingClient/LoggingClient/Repository/LoggingRepository.cs b/LoggingClient/LoggingClient/Repository/LoggingRepository.cs index 3fbedad..92756bf 100644 --- a/LoggingClient/LoggingClient/Repository/LoggingRepository.cs +++ b/LoggingClient/LoggingClient/Repository/LoggingRepository.cs @@ -12,22 +12,25 @@ using LoggingClient.Model; namespace LoggingClient.Repository { - class LoggingRepository : RepositoryBase + class LoggingRepository : IRepositoryBase { - public override string TableName => "v_logentries"; - public override string ColumnsForSelect => "id, pod, location, hostname, severity, timestamp, message"; - public override string ColumnsForAdd { get; } - public override string PrimaryKeyFromTable => "id"; + public string TableName => "v_logentries"; + public string ColumnsForSelect => "id, pod, location, hostname, severity, timestamp, message"; + public string ColumnsForAdd { get; } + public string PrimaryKeyFromTable => "id"; public List 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(); } - public override Logging GetSingle

(P pkValue) + public Logging GetSingle

(P pkValue) { try { @@ -60,7 +63,7 @@ namespace LoggingClient.Repository } return _Logs; } - public override void Add(Logging newLogModelEntry) + public void Add(Logging newLogModelEntry) { try { @@ -86,15 +89,15 @@ namespace LoggingClient.Repository MessageBox.Show("Error occurred: " + ex.Message); } } - public override void Delete(Logging entity) + public void Delete(Logging entity) { throw new System.NotSupportedException(); } - public override void Update(Logging entity) + public void Update(Logging entity) { throw new System.NotSupportedException(); } - public override List GetAll(string whereCondition, Dictionary parameterValues) + public List GetAll(string whereCondition, Dictionary parameterValues) { var whereCon = whereCondition; if (parameterValues.Count > 0 && whereCondition != null) @@ -135,7 +138,7 @@ namespace LoggingClient.Repository } return Logs; } - public override List GetAll() + public List GetAll() { try { @@ -168,7 +171,7 @@ namespace LoggingClient.Repository } return Logs; } - public override void CallStoredProcedure(Logging logModelEntry) + public void CallStoredProcedure(Logging logModelEntry) { try { @@ -189,5 +192,55 @@ namespace LoggingClient.Repository MessageBox.Show(ex.ToString()); } } + + public long Count(string whereCondition, Dictionary parameterValues) + { + var whereCon = whereCondition; + if (parameterValues.Count > 0 && whereCondition != null) + { + foreach (KeyValuePair 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; + } + } } } \ No newline at end of file diff --git a/LoggingClient/LoggingClient/Repository/RepositoryBase.cs b/LoggingClient/LoggingClient/Repository/RepositoryBase.cs deleted file mode 100644 index f2289f9..0000000 --- a/LoggingClient/LoggingClient/Repository/RepositoryBase.cs +++ /dev/null @@ -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 : IRepositoryBase - { - protected RepositoryBase(string connectionString) - { - this.ConnectionString = connectionString; - } - protected string ConnectionString { get; } - public abstract M GetSingle

(P pkValue); - - public abstract void Add(M entity); - - public abstract void Delete(M entity); - - public abstract void Update(M entity); - public abstract List GetAll(string whereCondition, Dictionary parameterValues); - - public abstract List GetAll(); - - public abstract void CallStoredProcedure(M entity); - - public IQueryable Query(string whereCondition, Dictionary parameterValues) - { - throw new System.NotSupportedException(); - } - public long Count(string whereCondition, Dictionary parameterValues) - { - var whereCon = whereCondition; - if (parameterValues.Count > 0 && whereCondition != null) - { - foreach (KeyValuePair 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; } - } -} \ No newline at end of file