Skip to content

Entity type requires .from #235

@mprytula

Description

@mprytula

Is your feature request related to a problem? Please describe.
There is a common pattern of instatiating domain objects from infrastructure, for example Entity could be instantiated from its database representation or event. Currently we have to extract props from those and use .create() or build additional factory methods.
Both approaches can lead to code duplications and spreading the same logic over the codebase.

Describe the solution you'd like
Entity has .toObject<this, T>(adapter: Adapter<this, T>) => T method.
My proposal is to add .fromObject<this, T>(adapter: Adapter<this, T>) => this.

Describe alternatives you've considered

  1. extract props from those and use .create()
  2. implement additional factory methods inside of the entity or wherever in the code base.
  3. implement Adapter interface(right one imo) and manually use its method .adaptOne

Why bad?

  1. Extracting props leads to code duplication, as sometimes props will be extracted from the same type of object, but in different places of code, leading to duplication. This could be solved by creating a method, which will be handling this, what brings us to 2nd alternative.
  2. This method could bring us to chaotic spreading of such adapter methods. As there is no clear defenition of where should be such functions extracted and how would they look.
  3. Is good, but in my opinion code style would be more consistent if we had .toObject and .fromObject.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions