I am working on a requirement in which I need to map between two systems. Because this mapping is rather extensive I am using a domain model with some custom attributes to do the mapping for data retrieval and crud operations.
So my domain classes look somthing like this:
[OhtherSystemObjectName("otherSystemAccountName")]
public class Account : SomeBaseClass
{
[OhtherSystemAttributeName("otherSystsemAccountNameColumn")]
public string Name { get; set; }
[OhtherSystemAttributeName("otherSystsemAccountNumberColumn")]
public string AccountNumber { get; set; }
[OhtherSystemAttributeName("otherSystsemAddressColumn")]
public string Address { get; set; }
[OhtherSystemAttributeName("otherSystsemPostalCodeColumn")]
public string PostalCode { get; set; }
}
I use a custom attribute to specify the column names of the other system so I do not need to bother about these when data needs to be retrieved or modified. Now I would like to create a data retrieval method in which I can specify one or more columns using expressions. The method look somthings like this:
public string[] GetColumnSet<T>(Expression<Func<T, object[]>> attributes) where T : SomeBaseClass
{
return ((NewArrayExpression)attributes.Body).Expressions.Select(a =>
{
switch (a.NodeType)
{
case ExpressionType.MemberAccess:
return GetAttributeName((MemberExpression)a);
case ExpressionType.Convert:
return GetAttributeName((MemberExpression)((UnaryExpression)a).Operand);
}
return null;
}).Where(a => !string.IsNullOrEmpty(a)).ToArray();
}
private static string GetAttributeName(MemberExpression memberExpression)
{
var attribute = memberExpression.Member.GetCustomAttribute<OhtherSystemAttributeName>();
return attribute?.AttributeName;
}
I use this method in my mapping layer to create a query to the other system and I need to specify the column names as an array of string. This call to this method looks something like this:
var colums = GetColumnSet<Account>(a => new object[] {a.Name, a.AccountNumber});
No I would like it to be more like this:
var colums = GetColumnSet<Account>(a => {a.Name, a.AccountNumber});
Without the new object[]{} but Expression<Func<T, param object[]>> does not compile.
Is it possible to rewrite the input expression to accomplish a syntax like this?