Attributes¶
DynamoMapper configuration is done via attributes on the mapper class.
DynamoMapperAttribute¶
Marks a static partial class as a mapper and sets defaults.
[DynamoMapper(
Convention = DynamoNamingConvention.CamelCase,
DefaultRequiredness = Requiredness.InferFromNullability,
IncludeBaseClassProperties = false,
OmitNullValues = true,
OmitEmptyStrings = false,
DateTimeFormat = "O",
EnumFormat = "G")]
public static partial class OrderMapper
{
public static partial Dictionary<string, AttributeValue> ToItem(Order source);
public static partial Order FromItem(Dictionary<string, AttributeValue> item);
}
Properties:
Convention- key naming conventionDefaultRequiredness- default requirednessIncludeBaseClassProperties- include properties declared on base classes (opt-in)OmitNullValues- omit null values, including nested object and nested collection propertiesOmitNullStrings- deprecated legacy option kept for compatibility with helper-backed null omissionOmitEmptyStrings- omit empty string attributesDateTimeFormat-DateTime/DateTimeOffsetformatTimeSpanFormat-TimeSpanformatEnumFormat- enum formatGuidFormat-Guidformat
Notes:
- When
IncludeBaseClassProperties = true, inherited properties are included for root models and nested inline objects. - If a derived type declares a property with the same name as an inherited property, the derived property wins.
- Prefer
OmitNullValuesfor mapper-level null omission.OmitNullStringsremains available only as a legacy compatibility option.
DynamoFieldAttribute¶
Configures mapping for a specific member. Apply multiple times to the mapper class.
[DynamoMapper]
[DynamoField(nameof(Order.Notes), OmitIfNull = true, OmitIfEmptyString = true)]
[DynamoField(nameof(Order.OrderId), AttributeName = "orderId")]
public static partial class OrderMapper
{
public static partial Dictionary<string, AttributeValue> ToItem(Order source);
public static partial Order FromItem(Dictionary<string, AttributeValue> item);
}
Properties:
MemberName(ctor) - target member nameAttributeName- DynamoDB attribute name overrideRequired- requiredness overrideKind- DynamoDBDynamoKindoverrideOmitIfNull- omit when null, including nested object and nested collection propertiesOmitIfEmptyString- omit when empty stringToMethod/FromMethod- static conversion methods on the mapper class
DynamoIgnoreAttribute¶
Skips a member in one or both directions.
[DynamoMapper]
[DynamoIgnore(nameof(Order.InternalNotes), Ignore = IgnoreMapping.All)]
public static partial class OrderMapper
{
public static partial Dictionary<string, AttributeValue> ToItem(Order source);
public static partial Order FromItem(Dictionary<string, AttributeValue> item);
}
Properties:
MemberName(ctor) - target member nameIgnore-IgnoreMapping.All,IgnoreMapping.FromModel, orIgnoreMapping.ToModel
DynamoMapperConstructorAttribute¶
Marks which constructor DynamoMapper should use when generating FromItem for a model type.
This attribute is applied to the model's constructor, not the mapper class.
using LayeredCraft.DynamoMapper.Runtime;
public class User
{
public User()
{
Id = string.Empty;
Name = string.Empty;
}
[DynamoMapperConstructor]
public User(string id, string name)
{
Id = id;
Name = name;
}
public string Id { get; set; }
public string Name { get; set; }
}
Rules:
- Only one constructor can be marked with
[DynamoMapperConstructor]. - If multiple are marked, DynamoMapper emits diagnostic
DM0103.
See Basic Mapping for the full constructor selection rules and gotchas.