I have below DTO
class in C#
public class Customer
{
public int Id { get; set;}
public string Name { get;set;}
// & so on (say 25 properties)
}
I am using DynmoDb
as Db(NoSql), to add the above item to DynamoDb table. This is how my method looks like
public async Task<bool> Add(Customer customer)
{
try
{
var res = await _dynamoClient.PutItemAsync(
tableName: _tableName, item: SetObject(customer)).ConfigureAwait(false);
return res.HttpStatusCode == System.Net.HttpStatusCode.OK
}
catch (Exception ex)
{
Logger.Log(ex, eventObj);
return false;
}
}
Private SetObject() method:-
private Dictionary<string, AttributeValue> SetObject(Customer obj)
{
//DynamoDb - Using Low Level API
var attributes = new Dictionary<string,
AttributeValue> {
//Id
{
nameof(obj.Id),
new AttributeValue {
S = obj.Id
}
},
//Name
{
nameof(obj.Name),
new AttributeValue {
S = obj.Name.Trim()
}
},
};
return attributes;
}
This private method looks cumbersome & error prone to me. I don't want to look up through each property one by one in this way.
I am thinking if there is some way to loop through all the properties & assign name
& value
to Dictionary
collection
var propsList = typeof(EventTO).GetProperties().Select(x => x.Name).ToList<string>();
This gives me all the property names but how do I fetch the value & key from the passed object of type customer.
Thanks!