move more logic into DataAccessManager
This commit is contained in:
parent
2a03bc6bc8
commit
aab741d1f0
@ -17,7 +17,7 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
|
||||
|
||||
public bool Update(T item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return true;
|
||||
}
|
||||
|
||||
public T Get(int id)
|
||||
|
@ -5,9 +5,9 @@ using System.Text;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess
|
||||
{
|
||||
public static class DataAccessManager
|
||||
public class DataAccessManager<T> where T : class, IRecord
|
||||
{
|
||||
private static readonly IDictionary<string, string[]> activeDataAccess = new Dictionary<string, string[]>
|
||||
private static readonly IDictionary<string, string[]> enabledDataAccess = new Dictionary<string, string[]>
|
||||
{
|
||||
#region Creditor
|
||||
|
||||
@ -30,17 +30,32 @@ namespace COA.EnterpriseServices.DataAccess
|
||||
#endregion
|
||||
};
|
||||
|
||||
public static ICollection<IDataAccess<T>> GetDataAccess<T>() where T : class, IRecord
|
||||
public TResult Invoke<TResult>(Func<IDataAccess<T>, TResult> func)
|
||||
{
|
||||
var results = new List<TResult>();
|
||||
var dataAccessInstances = GetEnabledDataAccess();
|
||||
|
||||
foreach (var dataAccessInstance in dataAccessInstances)
|
||||
{
|
||||
results.Add(func(dataAccessInstance));
|
||||
}
|
||||
|
||||
// if the "EF" version invokes first, return that value
|
||||
return results.FirstOrDefault(r => r != null && !r.Equals(default(TResult)));
|
||||
}
|
||||
|
||||
private ICollection<IDataAccess<T>> GetEnabledDataAccess()
|
||||
{
|
||||
var type = typeof(T);
|
||||
|
||||
// load data access based on active configuration
|
||||
return Dependencies.Container
|
||||
.GetAllInstances<IDataAccess<T>>()
|
||||
.Where(i =>
|
||||
{
|
||||
var dataAccessType = i.GetType().FullName;
|
||||
|
||||
return activeDataAccess.ContainsKey(type.FullName) && activeDataAccess[type.FullName].Any(a => dataAccessType.StartsWith(a));
|
||||
return enabledDataAccess.ContainsKey(type.FullName) && enabledDataAccess[type.FullName].Any(a => dataAccessType.StartsWith(a));
|
||||
})
|
||||
.OrderBy(i => i.GetType().Name.StartsWith("QuickBase", StringComparison.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
|
@ -5,7 +5,7 @@ namespace COA.EnterpriseServices.DataAccess.Entities
|
||||
public class Client : IRecord
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public DateTime Created { get; set; }
|
||||
public DateTime Created { get; set; } = DateTime.Now;
|
||||
public DateTime Modified { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
|
@ -7,7 +7,7 @@ namespace COA.EnterpriseServices.DataAccess.Entities
|
||||
public class Creditor : IRecord
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public DateTime Created { get; set; }
|
||||
public DateTime Created { get; set; } = DateTime.Now;
|
||||
public DateTime Modified { get; set; }
|
||||
public string Status { get; set; }
|
||||
public string ClientFirstName { get; set; }
|
||||
|
@ -5,7 +5,7 @@ namespace COA.EnterpriseServices.DataAccess.Entities
|
||||
public class SettlementAttempt : IRecord
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public DateTime Created { get; set; }
|
||||
public DateTime Created { get; set; } = DateTime.Now;
|
||||
public DateTime Modified { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
public class BaseHelper<T> where T : class, IRecord
|
||||
{
|
||||
private readonly ICollection<IDataAccess<T>> dataAccessInstances;
|
||||
protected DataAccessManager<T> DataAccessManager { get; } = new DataAccessManager<T>();
|
||||
|
||||
public BaseHelper()
|
||||
{
|
||||
dataAccessInstances = DataAccessManager.GetDataAccess<T>();
|
||||
}
|
||||
public virtual T Get(int id) => DataAccessManager.Invoke(d => d.Get(id));
|
||||
|
||||
protected TResult Invoke<TResult>(Func<IDataAccess<T>, TResult> func)
|
||||
{
|
||||
var results = new List<TResult>();
|
||||
|
||||
foreach (var dataAccessInstance in dataAccessInstances)
|
||||
{
|
||||
results.Add(func(dataAccessInstance));
|
||||
}
|
||||
|
||||
// if the "EF" version invokes first, return that value
|
||||
return results.FirstOrDefault(r => r != null && !r.Equals(default(TResult)));
|
||||
}
|
||||
|
||||
public virtual T Get(int id)
|
||||
{
|
||||
return Invoke(d => d.Get(id));
|
||||
}
|
||||
|
||||
public virtual bool Add(T item)
|
||||
{
|
||||
return Invoke(d => d.Add(item));
|
||||
}
|
||||
public virtual bool Add(T item) => DataAccessManager.Invoke(d => d.Add(item));
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
public ICollection<Client> FindByEmail(string email)
|
||||
{
|
||||
return Invoke(d => d.Get(c => c.Email.Equals(email)));
|
||||
return DataAccessManager.Invoke(d => d.Get(c => c.Email.Equals(email)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
using COA.EnterpriseServices.DataAccess.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
@ -8,12 +7,12 @@ namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
public ICollection<Creditor> FindByName(string name)
|
||||
{
|
||||
return Invoke(d => d.Get(c => c.ClientFirstName.Contains(name) || c.ClientLastName.Contains(name)));
|
||||
return DataAccessManager.Invoke(d => d.Get(c => c.ClientFirstName.Contains(name) || c.ClientLastName.Contains(name)));
|
||||
}
|
||||
|
||||
public ICollection<Creditor> FindByStatus(string status)
|
||||
{
|
||||
return Invoke(d => d.Get(c => c.Status == status));
|
||||
return DataAccessManager.Invoke(d => d.Get(c => c.Status == status));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user