working with QB field mapping registry
This commit is contained in:
9
COA.EnterpriseServices.DataAccess.QuickBase/FieldMap.cs
Normal file
9
COA.EnterpriseServices.DataAccess.QuickBase/FieldMap.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace COA.EnterpriseServices.DataAccess.QuickBase
|
||||
{
|
||||
internal class FieldMap
|
||||
{
|
||||
internal string DeclaringTypeName { get; set; }
|
||||
internal string PropertyName { get; set; }
|
||||
internal int FieldId { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
using COA.EnterpriseServices.DataAccess.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.QuickBase
|
||||
{
|
||||
internal static class FieldMapRegistry
|
||||
{
|
||||
private static readonly ICollection<FieldMap> registry = new List<FieldMap>();
|
||||
|
||||
static FieldMapRegistry()
|
||||
{
|
||||
AddFieldMap<Creditor>(c => c.Id, 1);
|
||||
AddFieldMap<Creditor>(c => c.Created, 2);
|
||||
AddFieldMap<Creditor>(c => c.Modified, 3);
|
||||
AddFieldMap<Creditor>(c => c.Status, 10);
|
||||
AddFieldMap<Creditor>(c => c.ClientFirstName, 20);
|
||||
AddFieldMap<Creditor>(c => c.ClientLastName, 21);
|
||||
}
|
||||
|
||||
internal static void AddFieldMap<TEntity>(Expression<Func<TEntity, object>> property, int fieldId)
|
||||
{
|
||||
if (property.Body is MemberExpression memberExpression)
|
||||
{
|
||||
registry.Add(new FieldMap
|
||||
{
|
||||
DeclaringTypeName = memberExpression.Member.DeclaringType.FullName,
|
||||
PropertyName = memberExpression.Member.Name,
|
||||
FieldId = fieldId
|
||||
});
|
||||
}
|
||||
else if (property.Body is UnaryExpression unaryExpression && unaryExpression.Operand is MemberExpression unaryMemberExpression)
|
||||
{
|
||||
registry.Add(new FieldMap
|
||||
{
|
||||
DeclaringTypeName = unaryMemberExpression.Member.DeclaringType.FullName,
|
||||
PropertyName = unaryMemberExpression.Member.Name,
|
||||
FieldId = fieldId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
internal static int? GetFieldId(PropertyInfo info)
|
||||
{
|
||||
var map = registry.FirstOrDefault(m => m.DeclaringTypeName == info.DeclaringType.FullName && m.PropertyName == info.Name);
|
||||
|
||||
return map != null ? map.FieldId : default;
|
||||
}
|
||||
}
|
||||
}
|
@ -4,10 +4,14 @@ using System.Linq.Expressions;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.QuickBase
|
||||
{
|
||||
public class QuickBaseDataAccess<T> : IDataAccess<T> where T : class, IRecord
|
||||
public class QuickBaseDataAccess<T> : IDataAccess<T> where T : class, IRecord, new()
|
||||
{
|
||||
public bool Add(T item)
|
||||
{
|
||||
var fieldData = GetFieldData(item);
|
||||
|
||||
// use fieldData to create mapped data to push to quickbase API
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -16,14 +20,32 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ICollection<T> Get(int id)
|
||||
public T Get(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
// we'll never get data from QB
|
||||
return null;
|
||||
}
|
||||
|
||||
public ICollection<T> Get(Expression<Func<T, bool>> query)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
// we'll never get data from QB
|
||||
return null;
|
||||
}
|
||||
|
||||
private IEnumerable<KeyValuePair<int, object>> GetFieldData(T item)
|
||||
{
|
||||
var properties = item.GetType().GetProperties();
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var fieldId = FieldMapRegistry.GetFieldId(property);
|
||||
var value = property.GetValue(item);
|
||||
|
||||
if (fieldId != null)
|
||||
{
|
||||
yield return new KeyValuePair<int, object>(fieldId.Value, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user