added helpers to DAM, additional methods
This commit is contained in:
@ -1,9 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a wrapper around <see cref="IDataAccess{T}"/> as well as additional convenience CRUD methods
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class DataAccessManager<T> where T : class, IRecord
|
||||
{
|
||||
private static readonly IDictionary<string, string[]> enabledDataAccess = new Dictionary<string, string[]>
|
||||
@ -29,7 +34,47 @@ namespace COA.EnterpriseServices.DataAccess
|
||||
#endregion
|
||||
};
|
||||
|
||||
public TResult Invoke<TResult>(Func<IDataAccess<T>, TResult> func)
|
||||
public bool Add(T item)
|
||||
{
|
||||
return Invoke(d => d.Add(item));
|
||||
}
|
||||
|
||||
public bool Update(T item)
|
||||
{
|
||||
item.Modified = DateTime.Now;
|
||||
|
||||
return Invoke(d => d.Update(item));
|
||||
}
|
||||
|
||||
public bool Update(int id, Action<T> update)
|
||||
{
|
||||
var item = Get(id);
|
||||
|
||||
update(item);
|
||||
|
||||
return Update(item);
|
||||
}
|
||||
|
||||
public T Get(int id)
|
||||
{
|
||||
var item = Invoke(d => d.Get(i => i.Id == id));
|
||||
|
||||
if (item != null && item.Any())
|
||||
{
|
||||
return item.FirstOrDefault();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public ICollection<T> Get(Expression<Func<T, bool>> query)
|
||||
{
|
||||
return Invoke(d => d.Get(query));
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private TResult Invoke<TResult>(Func<IDataAccess<T>, TResult> func)
|
||||
{
|
||||
var results = new List<TResult>();
|
||||
var dataAccessInstances = GetEnabledDataAccess();
|
||||
@ -59,6 +104,8 @@ namespace COA.EnterpriseServices.DataAccess
|
||||
.OrderBy(i => i.Type.Name.StartsWith("QuickBase", StringComparison.OrdinalIgnoreCase))
|
||||
.Select(i => i.Instance)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Entities
|
||||
{
|
||||
@ -12,5 +10,7 @@ namespace COA.EnterpriseServices.DataAccess.Entities
|
||||
public string Status { get; set; }
|
||||
public string ClientFirstName { get; set; }
|
||||
public string ClientLastName { get; set; }
|
||||
public int CurrentCreditorProfileId { get; set; }
|
||||
public int OriginalCreditorProfileId { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
public class BaseHelper<T> where T : class, IRecord
|
||||
{
|
||||
protected DataAccessManager<T> DataAccessManager { get; } = Dependencies.Container.GetInstance<DataAccessManager<T>>();
|
||||
|
||||
public virtual T Get(int id) => DataAccessManager.Invoke(d => d.Get(i => i.Id == id).FirstOrDefault());
|
||||
|
||||
public virtual bool Add(T item) => DataAccessManager.Invoke(d => d.Add(item));
|
||||
}
|
||||
}
|
@ -3,11 +3,18 @@ using System.Collections.Generic;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
public class ClientHelper : BaseHelper<Client>
|
||||
public class ClientHelper
|
||||
{
|
||||
private readonly DataAccessManager<Client> clientDataAccess;
|
||||
|
||||
public ClientHelper(DataAccessManager<Client> clientDataAccess)
|
||||
{
|
||||
this.clientDataAccess = clientDataAccess;
|
||||
}
|
||||
|
||||
public ICollection<Client> FindByEmail(string email)
|
||||
{
|
||||
return DataAccessManager.Invoke(d => d.Get(c => c.Email.Equals(email)));
|
||||
return clientDataAccess.Get(c => c.Email.Equals(email));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
using COA.EnterpriseServices.DataAccess.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
// NOTE: This example does NOT use BaseHelper<> if you wish to manage it yourself
|
||||
// It allows for multiple DataAccessManagers to be used however needed by the helper
|
||||
public class CreditorHelper
|
||||
{
|
||||
private readonly DataAccessManager<Creditor> creditorDataAccess;
|
||||
@ -19,32 +16,41 @@ namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
|
||||
public Creditor GetCreditor(int id)
|
||||
{
|
||||
return creditorDataAccess.Invoke(d => d.Get(d => d.Id == id)?.FirstOrDefault());
|
||||
return creditorDataAccess.Get(id);
|
||||
}
|
||||
|
||||
public SettlementAttempt GetSettlementAttempt(int id)
|
||||
{
|
||||
return settlementAttemptDataAccess.Invoke(d => d.Get(d => d.Id == id)?.FirstOrDefault());
|
||||
return settlementAttemptDataAccess.Get(id);
|
||||
}
|
||||
|
||||
public ICollection<Creditor> FindByName(string name)
|
||||
{
|
||||
return creditorDataAccess.Invoke(d => d.Get(c => c.ClientFirstName.Contains(name) || c.ClientLastName.Contains(name)));
|
||||
return creditorDataAccess.Get(c => c.ClientFirstName.Contains(name) || c.ClientLastName.Contains(name));
|
||||
}
|
||||
|
||||
public ICollection<Creditor> FindByStatus(string status)
|
||||
{
|
||||
return creditorDataAccess.Invoke(d => d.Get(c => c.Status == status));
|
||||
return creditorDataAccess.Get(c => c.Status == status);
|
||||
}
|
||||
|
||||
public bool AddCreditor(Creditor creditor)
|
||||
{
|
||||
return creditorDataAccess.Invoke(d => d.Add(creditor));
|
||||
return creditorDataAccess.Add(creditor);
|
||||
}
|
||||
|
||||
public bool SetCreditorStatus(int creditorId, string status)
|
||||
{
|
||||
return creditorDataAccess.Invoke(d => d.Update(creditorId, c => c.Status = status));
|
||||
return creditorDataAccess.Update(creditorId, c => c.Status = status);
|
||||
}
|
||||
|
||||
public void SetOriginalCreditorAsPrimary(int creditorId)
|
||||
{
|
||||
var creditor = creditorDataAccess.Get(creditorId);
|
||||
|
||||
creditor.CurrentCreditorProfileId = creditor.OriginalCreditorProfileId;
|
||||
|
||||
creditorDataAccess.Update(creditor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,6 @@ namespace COA.EnterpriseServices.DataAccess
|
||||
|
||||
bool Update(T item);
|
||||
|
||||
bool Update(int id, Action<T> update);
|
||||
|
||||
ICollection<T> Get(Expression<Func<T, bool>> query);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user