cleanup and notes
This commit is contained in:
parent
be9a32ce34
commit
fef89fb923
@ -11,6 +11,12 @@ namespace BinaryDad.AggregateDal
|
|||||||
|
|
||||||
static AggregateDataAccess() => LoadInstances();
|
static AggregateDataAccess() => LoadInstances();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Invokes a method for all instances of <typeparamref name="T"/>
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult"></typeparam>
|
||||||
|
/// <param name="func"></param>
|
||||||
|
/// <returns></returns>
|
||||||
protected TResult Invoke<TResult>(Func<T, TResult> func)
|
protected TResult Invoke<TResult>(Func<T, TResult> func)
|
||||||
{
|
{
|
||||||
var results = new List<TResult>();
|
var results = new List<TResult>();
|
||||||
@ -22,19 +28,19 @@ namespace BinaryDad.AggregateDal
|
|||||||
results.Add(func(instance));
|
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()
|
private static void LoadInstances()
|
||||||
{
|
{
|
||||||
// this would load all instances of type T
|
|
||||||
|
|
||||||
if (dataAccessTypes == null)
|
if (dataAccessTypes == null)
|
||||||
{
|
{
|
||||||
var type = typeof(T);
|
var type = typeof(T);
|
||||||
var aggregateType = typeof(AggregateDataAccess<T>);
|
var aggregateType = typeof(AggregateDataAccess<T>);
|
||||||
|
|
||||||
// load all types except for 1) the interface itself, 2) any interface, and 3) is not implementing AggregateDataAccess<T>
|
// load all types except for 1) the interface itself, 2) any interface, and 3) is not implementing AggregateDataAccess<T>
|
||||||
|
// NOTE: the "EF" version will load first, allowing for the "QuickBase" version to run last, in a separate thread if desired
|
||||||
dataAccessTypes = Assembly.GetExecutingAssembly()
|
dataAccessTypes = Assembly.GetExecutingAssembly()
|
||||||
.ExportedTypes
|
.ExportedTypes
|
||||||
.Where(t => type.IsAssignableFrom(t) && !t.IsInterface && !aggregateType.IsAssignableFrom(t))
|
.Where(t => type.IsAssignableFrom(t) && !t.IsInterface && !aggregateType.IsAssignableFrom(t))
|
||||||
|
@ -48,7 +48,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="AggregateDataAccess.cs" />
|
<Compile Include="AggregateDataAccess.cs" />
|
||||||
<Compile Include="Models\BaseModel.cs" />
|
<Compile Include="Models\BaseModel.cs" />
|
||||||
<Compile Include="SettlementAttempt\EfSettlementAttemptDataAccess.cs" />
|
<Compile Include="SettlementAttempt\EFSettlementAttemptDataAccess.cs" />
|
||||||
<Compile Include="SettlementAttempt\ISettlementAttemptDataAccess.cs" />
|
<Compile Include="SettlementAttempt\ISettlementAttemptDataAccess.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
26
Program.cs
26
Program.cs
@ -14,19 +14,37 @@ namespace BinaryDad.AggregateDal
|
|||||||
// get instance via structuremap, as is typical
|
// get instance via structuremap, as is typical
|
||||||
var settlementAttemptDataAccess = container.GetInstance<ISettlementAttemptDataAccess>();
|
var settlementAttemptDataAccess = container.GetInstance<ISettlementAttemptDataAccess>();
|
||||||
|
|
||||||
var attempt = new SettlementAttempt
|
#region Add attempt
|
||||||
|
|
||||||
|
var newAttempt = new SettlementAttempt
|
||||||
{
|
{
|
||||||
ClientFirstName = "Ryan",
|
ClientFirstName = "Ryan",
|
||||||
ClientLastName = "Peters",
|
ClientLastName = "Peters",
|
||||||
CreatedBy = "rpeters"
|
CreatedBy = "rpeters"
|
||||||
};
|
};
|
||||||
|
|
||||||
settlementAttemptDataAccess.AddAttempt(attempt);
|
settlementAttemptDataAccess.AddAttempt(newAttempt);
|
||||||
|
|
||||||
// in most cases, the QuickBase DAL will update "RecordId" and
|
// in most cases, the QuickBase DAL will update "RecordId" and
|
||||||
// the EF DAL will update "Id"
|
// the EF DAL will update "Id"
|
||||||
Console.WriteLine($"Attempt ID (SQL) => {attempt.Id}");
|
Console.WriteLine($"Attempt ID (SQL) => {newAttempt.Id}");
|
||||||
Console.WriteLine($"Attempt ID (QuickBase) => {attempt.RecordId}");
|
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
|
// pause
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
|
@ -3,12 +3,15 @@ using System;
|
|||||||
|
|
||||||
namespace BinaryDad.AggregateDal
|
namespace BinaryDad.AggregateDal
|
||||||
{
|
{
|
||||||
public class EfSettlementAttemptDataAccess : ISettlementAttemptDataAccess
|
public class EFSettlementAttemptDataAccess : ISettlementAttemptDataAccess
|
||||||
{
|
{
|
||||||
public bool AddAttempt(SettlementAttempt attempt)
|
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;
|
attempt.Id = 12376;
|
||||||
|
|
||||||
Console.WriteLine($"Adding attempt ID {attempt.Id} to SQL");
|
Console.WriteLine($"Adding attempt ID {attempt.Id} to SQL");
|
||||||
@ -18,13 +21,17 @@ namespace BinaryDad.AggregateDal
|
|||||||
|
|
||||||
public SettlementAttempt GetAttempt(int recordId)
|
public SettlementAttempt GetAttempt(int recordId)
|
||||||
{
|
{
|
||||||
// get the attempt from SQL
|
|
||||||
|
|
||||||
Console.WriteLine($"Getting attempt {recordId} from SQL");
|
Console.WriteLine($"Getting attempt {recordId} from SQL");
|
||||||
|
|
||||||
|
// get the attempt from SQL/EF
|
||||||
return new SettlementAttempt
|
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"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,25 +7,19 @@ namespace BinaryDad.AggregateDal
|
|||||||
{
|
{
|
||||||
public bool AddAttempt(SettlementAttempt attempt)
|
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}");
|
Console.WriteLine($"Adding attempt ID {attempt.RecordId} to Quickbase with SQL ID {attempt.Id}");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SettlementAttempt GetAttempt(int recordId)
|
// NOTE: we have no need to retrieve a record from QB, as it will come from SQL
|
||||||
{
|
public SettlementAttempt GetAttempt(int recordId) => null;
|
||||||
// get the attempt from Quickbase
|
|
||||||
|
|
||||||
Console.WriteLine($"Getting attempt {recordId} from Quickbase");
|
|
||||||
|
|
||||||
return new SettlementAttempt
|
|
||||||
{
|
|
||||||
Id = recordId
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user