From fef89fb923c21c533d90680efdec058064cc0704 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Tue, 15 Sep 2020 18:53:28 -0400 Subject: [PATCH] cleanup and notes --- AggregateDataAccess.cs | 12 ++++++--- BinaryDad.AggregateDal.csproj | 2 +- Program.cs | 26 ++++++++++++++++--- .../EfSettlementAttemptDataAccess.cs | 17 ++++++++---- .../QuickBaseSettlementAttemptDataAccess.cs | 20 +++++--------- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/AggregateDataAccess.cs b/AggregateDataAccess.cs index d58ad68..a17c66d 100644 --- a/AggregateDataAccess.cs +++ b/AggregateDataAccess.cs @@ -11,6 +11,12 @@ namespace BinaryDad.AggregateDal static AggregateDataAccess() => LoadInstances(); + /// + /// Invokes a method for all instances of + /// + /// + /// + /// protected TResult Invoke(Func func) { var results = new List(); @@ -22,19 +28,19 @@ namespace BinaryDad.AggregateDal results.Add(func(instance)); } - return results.LastOrDefault(); + // if the "EF" version invokes first, return that value + return results.FirstOrDefault(r => !r.Equals(default(T))); } private static void LoadInstances() { - // this would load all instances of type T - if (dataAccessTypes == null) { var type = typeof(T); var aggregateType = typeof(AggregateDataAccess); // load all types except for 1) the interface itself, 2) any interface, and 3) is not implementing AggregateDataAccess + // NOTE: the "EF" version will load first, allowing for the "QuickBase" version to run last, in a separate thread if desired dataAccessTypes = Assembly.GetExecutingAssembly() .ExportedTypes .Where(t => type.IsAssignableFrom(t) && !t.IsInterface && !aggregateType.IsAssignableFrom(t)) diff --git a/BinaryDad.AggregateDal.csproj b/BinaryDad.AggregateDal.csproj index 798371d..e051e7f 100644 --- a/BinaryDad.AggregateDal.csproj +++ b/BinaryDad.AggregateDal.csproj @@ -48,7 +48,7 @@ - + diff --git a/Program.cs b/Program.cs index 855f368..cd1d7e4 100644 --- a/Program.cs +++ b/Program.cs @@ -14,19 +14,37 @@ namespace BinaryDad.AggregateDal // get instance via structuremap, as is typical var settlementAttemptDataAccess = container.GetInstance(); - var attempt = new SettlementAttempt + #region Add attempt + + var newAttempt = new SettlementAttempt { ClientFirstName = "Ryan", ClientLastName = "Peters", CreatedBy = "rpeters" }; - settlementAttemptDataAccess.AddAttempt(attempt); + settlementAttemptDataAccess.AddAttempt(newAttempt); // in most cases, the QuickBase DAL will update "RecordId" and // the EF DAL will update "Id" - Console.WriteLine($"Attempt ID (SQL) => {attempt.Id}"); - Console.WriteLine($"Attempt ID (QuickBase) => {attempt.RecordId}"); + Console.WriteLine($"Attempt ID (SQL) => {newAttempt.Id}"); + Console.WriteLine($"Attempt ID (QuickBase) => {newAttempt.RecordId}"); + + #endregion + + #region Get attempt + + var attempt = settlementAttemptDataAccess.GetAttempt(1234); + + // dump the attempt + Console.WriteLine($"{nameof(attempt.Id)} => {attempt.Id}"); + Console.WriteLine($"{nameof(attempt.RecordId)} => {attempt.RecordId}"); + Console.WriteLine($"{nameof(attempt.ClientFirstName)} => {attempt.ClientFirstName}"); + Console.WriteLine($"{nameof(attempt.ClientLastName)} => {attempt.ClientLastName}"); + Console.WriteLine($"{nameof(attempt.Created)} => {attempt.Created}"); + Console.WriteLine($"{nameof(attempt.CreatedBy)} => {attempt.CreatedBy}"); + + #endregion // pause Console.ReadLine(); diff --git a/SettlementAttempt/EfSettlementAttemptDataAccess.cs b/SettlementAttempt/EfSettlementAttemptDataAccess.cs index 1c27181..959a696 100644 --- a/SettlementAttempt/EfSettlementAttemptDataAccess.cs +++ b/SettlementAttempt/EfSettlementAttemptDataAccess.cs @@ -3,12 +3,15 @@ using System; namespace BinaryDad.AggregateDal { - public class EfSettlementAttemptDataAccess : ISettlementAttemptDataAccess + public class EFSettlementAttemptDataAccess : ISettlementAttemptDataAccess { public bool AddAttempt(SettlementAttempt attempt) { - // create attempt in SQL + // 1. create attempt in SQL using entity framework + // 2. update the "Id" property/PK of attempt upon insertion + // 3. return if operation is successful + // auto-generated value from PK insert attempt.Id = 12376; Console.WriteLine($"Adding attempt ID {attempt.Id} to SQL"); @@ -18,13 +21,17 @@ namespace BinaryDad.AggregateDal public SettlementAttempt GetAttempt(int recordId) { - // get the attempt from SQL - Console.WriteLine($"Getting attempt {recordId} from SQL"); + // get the attempt from SQL/EF return new SettlementAttempt { - Id = recordId + Id = recordId, + RecordId = 9999, // will already be in SQL via webhook + ClientFirstName = "Ryan", + ClientLastName = "Peters", + Created = DateTime.Parse("9/1/2020"), + CreatedBy = "rpeters" }; } } diff --git a/SettlementAttempt/QuickBaseSettlementAttemptDataAccess.cs b/SettlementAttempt/QuickBaseSettlementAttemptDataAccess.cs index 6c84052..5d0ee3f 100644 --- a/SettlementAttempt/QuickBaseSettlementAttemptDataAccess.cs +++ b/SettlementAttempt/QuickBaseSettlementAttemptDataAccess.cs @@ -7,25 +7,19 @@ namespace BinaryDad.AggregateDal { public bool AddAttempt(SettlementAttempt attempt) { - attempt.RecordId = 9999; + // 1. create attempt in QuickBase ("Id" SQL PK should already be populated) + // 2. update the "RecordId" property/PK of attempt upon insertion + // 3. return if operation is successful - // create attempt in Quickbase + // auto-generated value from QB record insert + attempt.RecordId = 9999; Console.WriteLine($"Adding attempt ID {attempt.RecordId} to Quickbase with SQL ID {attempt.Id}"); return true; } - public SettlementAttempt GetAttempt(int recordId) - { - // get the attempt from Quickbase - - Console.WriteLine($"Getting attempt {recordId} from Quickbase"); - - return new SettlementAttempt - { - Id = recordId - }; - } + // NOTE: we have no need to retrieve a record from QB, as it will come from SQL + public SettlementAttempt GetAttempt(int recordId) => null; } }