Skip to content

Is it possible to map private fields to table columns? #1149

Open
@Jyuart

Description

@Jyuart

In clean architecture (DDD specifically), it's common to encapsulate your domain layer.

  1. It should not have external dependencies (thus, no attributes, which are ugly either way, though)
  2. The behavior itself is encapsulated, so instead of properties, we use private fields which can be changed only via publicly exposed methods that resemble domain logic

Before adding this library to the project, I thought the Field class was made specifically for that. But now it seems that it's not. It either doesn't work with fields or just doesn't pick up private ones.

Tell me, please, do I miss something obvious, or it's not possible to use the full-ORM side of RepoDb if all of my classes contain only private fields?

Basic example:

public class User
{
    public Guid UserId { get; }

    private readonly FullName _name;
    private readonly Email _email;

    public User(FullName name, Email email)
    {
        UserId = Guid.NewGuid();
        _name = name;
        _email = email;
    }
}

public class FullName
{
    private readonly string _firstName;
    private readonly string _lastName;

    public FullName(string firstName, string lastName)
    {
        _firstName = Guard.Against.NullOrWhiteSpace(firstName, nameof(_firstName));
        _lastName = Guard.Against.NullOrWhiteSpace(lastName, nameof(_lastName));
    }
}

I tried mapping like this (which is not perfect, but it'd be ok if it worked), but it doesn't work:

        FluentMapper
            .Entity<User>()
            .Table("users")
            .Column(x => x.UserId, "user_id");

        FluentMapper.Entity<FullName>()
            .Column(new Field("_firstName"), "first_name")
            .Column(new Field("_lastName"), "last_name");

        FluentMapper.Entity<Email>()
            .Column(new Field("_value"), "email");

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions