Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Jyuart opened this issue Jun 2, 2023 · 3 comments
Open

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

Jyuart opened this issue Jun 2, 2023 · 3 comments
Labels
question Further information is requested

Comments

@Jyuart
Copy link

Jyuart commented Jun 2, 2023

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");
@Jyuart Jyuart added the question Further information is requested label Jun 2, 2023
@mikependon
Copy link
Owner

No, this is not possible. The hydration process onky succeed with columns projection to those writeable class properties or constructor parameters (with the same name as underlying table field).

@thabans
Copy link

thabans commented Jun 20, 2023

would it be possible to have system call out to a method on set call with the sql value object so we can set this manually?

public class User {
  public void   OnSet(SqlExecuteReuslt result) {
        _name = result"first_name"};
 }
}

Configurations.Setup().CallMethodOnSet("OnSet");

@mikependon
Copy link
Owner

On your class FullName with ctor argument of firstName and lastName

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));
    }
}

As there are projections defined on the ctor arguments during hydration, therefore, I would expect that this would work if the underlying table has the FirstName and LastName columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants