COA.EnterpriseServices/COA.EnterpriseServices.DataAccess.QuickBase/FieldMapRegistry.cs

64 lines
2.4 KiB
C#
Raw Normal View History

2020-10-14 12:35:14 +00:00
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()
{
// Map to QB field IDs. Obviously we will use actual IDs
2020-10-14 12:35:14 +00:00
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);
AddFieldMap<Client>(c => c.Id, 1);
AddFieldMap<Client>(c => c.Created, 2);
AddFieldMap<Client>(c => c.Modified, 3);
AddFieldMap<Client>(c => c.Email, 10);
AddFieldMap<Client>(c => c.Phone, 11);
AddFieldMap<Client>(c => c.FirstName, 20);
AddFieldMap<Client>(c => c.LastName, 21);
2020-10-14 12:35:14 +00:00
}
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;
}
}
}