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

Create a new instance of a entity using constructor #14

Open
maikmb opened this issue Jul 6, 2020 · 4 comments
Open

Create a new instance of a entity using constructor #14

maikmb opened this issue Jul 6, 2020 · 4 comments
Labels
blocked Something is blocked enhancement New feature or request hacktoberfest help wanted Extra attention is needed severity-minor Item is not urgent

Comments

@maikmb
Copy link
Contributor

maikmb commented Jul 6, 2020

Hello everyone!

Gotu has the FromJson method to create a new instance of an entity, but the constructor is more efficient to initialize a new instance from any input in json like this:

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

Can this be classified as an improvement?

@maikmb maikmb changed the title Creating a new instance of a entity using constructor Create a new instance of a entity using constructor Jul 6, 2020
@dalssoft
Copy link
Member

dalssoft commented Jul 7, 2020

by definition constructors are the 'Initialize function' of a class. so it is a very special one (actually it is the only one)

by going the way you suggested we will use the this special function to initialize a entity in a very specific way

my only question is: is it the only way to initialize a entity? if so, I don't see a problem in monopolies this function for this purpose. the problem is that I think this is not the only way to do it

another point is: who (1) is different from (2) in terms of readability?

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

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

(btw, the issue #1 suggests a different name for fromJSON. maybe newFrom?)

so for now I would prefer to leave the constructor free of responsibilities until we have a better understanding of the possibilities

but the constructor is more efficient to initialize a new instance

how so?

@maikmb
Copy link
Contributor Author

maikmb commented Jul 7, 2020

Json objects are the most used today in most implementations. The documentation shows only two methods of creating the .fromJson instance with Object and String.

We can use JSON as a standard in the constructor to simplify the code and use custom methods for other types, such as ** fromXml, fromArray **.

@jhomarolo jhomarolo reopened this May 25, 2021
@jhomarolo jhomarolo added blocked Something is blocked enhancement New feature or request help wanted Extra attention is needed severity-minor Item is not urgent labels Dec 24, 2021
@italojs
Copy link
Member

italojs commented Apr 14, 2022

I think use it into constructor is more intuitive because most languages use this UX to create new instances.

I agree with @maikmb that make sense to have "methods for other types, such as fromXml, fromArray ."

@italojs
Copy link
Member

italojs commented Apr 14, 2022

I think it's related with issue #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Something is blocked enhancement New feature or request hacktoberfest help wanted Extra attention is needed severity-minor Item is not urgent
Projects
None yet
Development

No branches or pull requests

4 participants