herbsjs / gotu

Entities - Create your Entity and enjoy an elegant validation and serialization
Other
11 stars 19 forks source link

fromJSON should be from fromObject #1

Open m7vicente opened 4 years ago

m7vicente commented 4 years ago

The serialization method 'fromJSON' is capable to parse in entity not only json strings, but entire objects, so, it should be called 'fromObject'.

dalssoft commented 4 years ago

Agree. I think a better name would be build or buildFrom What do you think?

m7vicente commented 4 years ago

The most intuitive is buildFrom , but i think the best way to create a entity from a object is from constructor parameters , like new User(obj).

dalssoft commented 4 years ago

what about `newFrom'?

const user = new User.newFrom({ ...input });

or just from

const user = new User.from({ ...input });

still open to suggestions

endersoncosta commented 4 years ago

I believe that .build or .create should be better than .from ... Because it already is used in other libs like own javascript...

dalssoft commented 4 years ago

just to add to this discussion...

There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

there are two approaches towards naming:

  1. capture the intention of the method:

const user = new User.create({ ...input });

  1. prioritize code readability:

const user = new User.from({ ...input });

in (1) create captures the intetion of the method, but doesn't look that nice when reading the code

in (2) it is a attempt to capture the intention "create a new User from this data" in code and make it more readable

m7vicente commented 4 years ago

In most cases we use of this method to create an entity instance-based from any object, it can be from a database or a simple post request, in some ways, the object has been constructed previously, so User.from(obj), make more sense, because it's not a literal new object.

endersoncosta commented 4 years ago

Ohh, got it! This really make sense. Now new User.createFrom( param ) seems to be an option...

dalssoft commented 4 years ago

createFrom seems to mix both ideas. I'm not sure it's the way to go...

m7vicente commented 4 years ago

lets use justUser.from(param), this way, in the future we can create other constructors likeUser.FromXml(xmlParam)`, and we let the users create customized constructors methods for an entity using the same pattern.

italojs commented 2 years ago

I think it's related with issue https://github.com/herbsjs/gotu/issues/14