DEV Community

Hamza Darouzi
Hamza Darouzi

Posted on

Simplify Your Data Model with Complex Types in .NET 8

What Are Complex Types?

Complex types allow us to organize related properties within an entity. Think of them as neat little bundles of data that can be reused across different parts of your application. 🎁

Why Choose Complex Types?

  1. Cleaner Code : By using complex types, we avoid cluttering our entities with repetitive properties. Imagine an Order entity with both ShippingAddress and BillingAddress . Instead of duplicating those properties, we can create a StreetAddress complex type and reuse it wherever needed.

  2. Natural Aggregates: Unlike owned entities, which are tightly bound to their owners, complex types can be saved independently. They're like friendly companions that don't mind hanging out solo or with their owner.

  3. Implicit Keys: Complex types don't need their own keys because they're always part of a one-to-one relationship with their owner. EF Core handles this gracefully behind the scenes.

Example Time! 🚀

Take a look at picture which i provided down below ..

public class Order
{
    public int Id {get;set;}
    public StreetAddress ShippingAddress {get;set;}
    // Other Order Properties
}
[ComplexType]
public class StreetAddress
{
    public string Street {get;set;}
    public string City {get;set;}
}
Enter fullscreen mode Exit fullscreen mode

Let's say we have an Order class , And our StreetAddress complex type:

Now, whenever we create an order, we can easily set its shipping address using the StreetAddress complex type. No fuss, no redundancy!

Mapping Complex Types by Configuration

To map complex types using configuration over convention in EF Core, we use the IEntityTypeConfiguration interface. This allows us to define the mapping in a separate configuration class, providing more control and flexibility.

To map the StreetAddress complex type using configuration, we create a configuration class:

public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
        builder.OwnsOne(o => o.ShippingAddress, sa =>
        {
            sa.Property(p => p.Street).HasColumnName("ShippingStreet");
            sa.Property(p => p.City).HasColumnName("ShippingCity");
        });
    }
}
Enter fullscreen mode Exit fullscreen mode

How It Maps to the Database

When EF Core maps the StreetAddress complex type to the database, it treats the properties of the complex type as columns in the table of the owning entity. In this example, the Order table will have columns named ShippingStreet and ShippingCity.

This approach allows you to customize the column names and other aspects of the mapping, providing greater control over how your data is stored in the database.

Wrapping Up

So, my fellow devs, next time you're modeling your data in .NET 8, consider reaching for complex types. They'll keep your codebase tidy, your relationships clear, and your coffee warm.

Have you used complex types?
Share your thoughts below! Let's keep the conversation going. 🤓

Top comments (0)