DEV Community

Cover image for Custom Converters in DynamoDB: Building Robust Data Models with Ease
manojlingala
manojlingala

Posted on

Custom Converters in DynamoDB: Building Robust Data Models with Ease

DynamoDB's custom converters feature allows you to define custom mappings between your application's data model and DynamoDB's attribute-value data model. This is particularly useful when working with complex or custom data types, such as enums or value objects, that may not be natively supported by DynamoDB.

By using custom converters, you can simplify your application's code and reduce the amount of boilerplate code required for mapping between your application and DynamoDB. Custom converters can also improve performance by reducing the amount of data that needs to be transferred between your application and DynamoDB, and by allowing you to optimize the way data is stored and retrieved.

The purpose of this article is to demonstrate how to use two specific types of custom converters in DynamoDB: Enum Converter and Value Object Converter. These converters are particularly useful for mapping enums and value objects to and from DynamoDB attribute values, respectively. We will provide code examples and discuss some advanced topics related to custom converters in DynamoDB.

DynamoDB's Enum Converter allows you to map between dotnet or any other supported language specific enums and DynamoDB attribute values. This is useful because DynamoDB natively supports only a limited set of data types, such as strings, numbers, and binary data. With Enum Converter, you can map your application's enums to these native data types, and store them in DynamoDB as attributes.

Here's an example of how to create and use an Enum Converter in DynamoDB:

using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;

public enum PaymentMethod
{
    CreditCard,
    PayPal,
    Bitcoin
}

public class PaymentMethodConverter : IPropertyConverter
{
    public DynamoDBEntry ToEntry(object value)
    {
        return new Primitive((value as PaymentMethod).ToString());
    }

    public object FromEntry(DynamoDBEntry entry)
    {
        return Enum.Parse(typeof(PaymentMethod), entry.AsString());
    }
}

Enter fullscreen mode Exit fullscreen mode

We can then use this converter in our data model as follows:

[DynamoDBTable("Orders")]
public class Order
{
    [DynamoDBProperty("paymentMethod"), DynamoDBTypeConverter(typeof(PaymentMethodConverter))]
    public PaymentMethod PaymentMethod { get; set; }

    // ... other properties and methods
}

Enter fullscreen mode Exit fullscreen mode

Value objects are a concept in software development that represent an immutable value or set of values with a specific behavior. They can be used to encapsulate complex or custom data types that don't fit into the simple data types supported by databases like DynamoDB. Value objects are typically implemented as immutable classes that expose their data through getters and don't have any setters or other mutators.

The benefits of using value objects over primitives or simple types include increased type safety, better encapsulation, and more expressive code. By using value objects, you can reduce the likelihood of errors due to incorrect types or values, and you can make your code more readable and maintainable.

DynamoDB's Value Object Converter allows you to map between value objects and DynamoDB attribute values.

Here's an example of how to create and use a Value Object Converter in DynamoDB:

using Amazon.DynamoDBv2.DataModel;
using Newtonsoft.Json;

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

public class AddressConverter : IPropertyConverter
{
    public DynamoDBEntry ToEntry(object value)
    {
        return new Primitive(JsonConvert.SerializeObject(value));
    }

    public object FromEntry(DynamoDBEntry entry)
    {
        return JsonConvert.DeserializeObject(entry.AsString(), typeof(Address));
    }
}

Enter fullscreen mode Exit fullscreen mode

We use the Newtonsoft.Json / System.Text to convert the value object to/from JSON string. We can then use this converter in our data model as follows:

[DynamoDBTable("Customers")]
public class Customer
{
    [DynamoDBProperty("address"), DynamoDBTypeConverter(typeof(AddressConverter))]
    public Address Address { get; set; }

    // ... other properties and methods
}

Enter fullscreen mode Exit fullscreen mode

Handling nested objects
Sometimes, your data model may contain nested objects, where one object contains another object as a property. In these cases, you can use a combination of custom converters to handle the nested objects.

For example, suppose we have the following data model:

using Amazon.DynamoDBv2.DataModel;
using Newtonsoft.Json;
using System.Collections.Generic;

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

public class Customer
{
    public string CustomerId { get; set; }
    public Address Address { get; set; }
}

public class Order
{
    public string OrderId { get; set; }
    public Customer Customer { get; set; }
}

public class AddressConverter : IPropertyConverter
{
    public DynamoDBEntry ToEntry(object value)
    {
        return new Primitive(JsonConvert.SerializeObject(value));
    }

    public object FromEntry(DynamoDBEntry entry)
    {
        return JsonConvert.DeserializeObject(entry.AsString(), typeof(Address));
    }
}

public class CustomerConverter : IPropertyConverter
{
    public DynamoDBEntry ToEntry(object value)
    {
        var customer = (Customer)value;
        var address = new AttributeValue(new AddressConverter().ToEntry(customer.Address));

        var map = new Dictionary<string, AttributeValue>
        {
            {"CustomerId", new AttributeValue(customer.CustomerId)},
            {"Address", address}
        };

        return new AttributeValue(map);
    }

    public object FromEntry(DynamoDBEntry entry)
    {
        var map = entry.M;

        return new Customer
        {
            CustomerId = map["CustomerId"].S,
            Address = (Address)new AddressConverter().FromEntry(map["Address"].S)
        };
    }
}

public class OrderConverter : IPropertyConverter
{
    public DynamoDBEntry ToEntry(object value)
    {
        var order = (Order)value;
        var customer = new AttributeValue(new CustomerConverter().ToEntry(order.Customer));

        var map = new Dictionary<string, AttributeValue>
        {
            {"OrderId", new AttributeValue(order.OrderId)},
            {"Customer", customer}
        };

        return new AttributeValue(map);
    }

    public object FromEntry(DynamoDBEntry entry)
    {
        var map = entry.M;

        return new Order
        {
            OrderId = map["OrderId"].S,
            Customer = (Customer)new CustomerConverter().FromEntry(map["Customer"].M)
        };
    }
}

Enter fullscreen mode Exit fullscreen mode

In summary, we have explored how to use custom converters in DynamoDB to map between your application's data model and DynamoDB attribute values. Specifically, we have looked at Enum Converter and Value Object Converter as ways to map C# enums and value objects, respectively, to DynamoDB attribute values. We have also covered some advanced topics, such as handling nested objects and using custom serialization/deserialization libraries.

Thank you for reading ❤️.

Top comments (0)