geoffreywiseman/Moo

Update should be driven by translation property in target

Opened this issue · 2 comments

One major usage of MOO I believe is to strip down the entity to compose of a DTO that consists of attributes that is "enough" for the business function. Therefore it is not uncommon that there is no translation in DTO for corresponding attribute in the entity.

Assume I have a entity class Foo, and a FooDto:

class Foo {
    private String bar1;
    private String bar2;
}

class FooDto {
    private String bar1;
}

When I am doing Update.from(aFooDto).to(aFoo); it is complaining for could not access bar2 in FooDto.

It seems to be that update is driven by properties available in the update target (which is the source model in Translation) instead of the target of translation in which contains the "translation settings".

I believe it is more reasonable to see Update as, using the above example, because FooDto.bar1 is translated from Foo.bar1, therefore when I am doing update, I want to contribute FooDto.bar1 back to Foo.bar1. As Foo.bar2 has nothing to do with my DTO, that's reasonable to ignore it when I am updating.

There's a few options you already have here, you could mark bar2 ignored:

class Foo {
  private String bar1;

  @Ignore
  private String bar2;
}

Or you could tell Moo that source properties are not required:

Configuration config = new Configuration();
config.setSourcePropertiesRequired( false );
new Moo( config );

This latter one is a bit of a pain because it's not easy to configure Moo in a way that works with the simplest invocation options (Translate, Update classes) yet. It's also not specific to a particular scenario.

I'm a little wary of assuming that Updates are always DTO -> Domain and Translate is always Domain -> DTO and making assumptions on that basis, although I agree that's a common scenario.

As mentioned in another issue, approach 1 is not favorable (at least for me) as I think it is preferrable to put all config of one "profile" of translation/update in same place (for which is the translated target now).

approach 2 seems reasonable. However if combining with non-trivial update expression (as mentioned in another issue), that will not work.

I am not saying Update are always from DTO -> Domain, because I believe in Moo's design, DTO is not necessary to be the only usage. However I think it is reasonable to assume, Translation is defined as mapping from Translation Source Objects to a Translated Target, and Update is defined as the Translated Target contribute back to the Translation Source Objects. (That's the definition in the DTO Building tools I created before)

Is my understanding different from Moo's design? I think current Moo's design define Translate and Update as: Translate is to create the Translated Target from scratch, Update is to update an existing instance of Translated Target. Is my understanding correct?

just my 2cents :)