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

Support full constructors #581

Closed
jmkgreen opened this issue Feb 27, 2014 · 5 comments
Closed

Support full constructors #581

jmkgreen opened this issue Feb 27, 2014 · 5 comments
Labels
enhancement experimental implementation details subject to change or removal
Milestone

Comments

@jmkgreen
Copy link

It would be nice to see support for constructors that accepted each field as an argument.

This will let clients construct immutable entities without having to wire in custom object factories.

@evanchooly
Copy link
Member

Can you give an example of what you mean?

@jmkgreen
Copy link
Author

It was inspired partly from the DDD book (Eric Evans) and partly from
http://www.jayway.com/2012/02/28/configure-morphia-to-work-without-a-default-constructor/

If you get rid of no-arg constructors and provide a single constructor that
requires all fields to be present as arguments you can get rid of setter
methods. I've noticed a bit of fire directed at "the frameworks" because
they tend to require both no-arg constructors and setters/getters per field.

I noticed the other day Spring Data will use a constructor that accepts
each non-transient field as it's arguments. Felt cleaner.

So:

public class Person {

private final Date dob;
private final String firstName;
private final String lastName;

public Person(Date dob, String firstName, String lastName) {
this.dob = dob;
this.firstName = firstName;
this.lastName = lastName;
}

public Date getDateOfBirth() {
return new Date(dob.getTime());
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return firstName;
}

// Other methods for business use.

}

On 27 February 2014 11:45, Justin Lee notifications@github.com wrote:

Can you give an example of what you mean?

Reply to this email directly or view it on GitHubhttps://github.com//issues/581#issuecomment-36233938
.

@evanchooly
Copy link
Member

Well, the constructor part I got. You'd just lost me with the factory part. There was a similar request/conversation about @constructor arguments elsewhere, too. This is probably a good idea. Provides a bit of flexibility for users. The mapping code is likely to change along with the 3.x java driver integration so I'd like to defer this until that picture becomes clearer.

Morphia's perfectly happy without getters/setters for the record, though, so this feature is probably less pressing than many think. But I know a many devs hate adding no-arg constructors simply for the sake of persistence/serialization frameworks so this should probably happen either way.

@jmkgreen
Copy link
Author

Where the wiki has an entity being described, I think you have a comment
"Add getters/setters here" which I would interpret as required for
operation. If not, better to say that Morphia does not use getters/setters
for operation, otherwise someone might try to write a setter that does
something more and will get confused when it is not invoked.

Agree about deferring, just want it as a desirable on the radar.

On 27 February 2014 12:48, Justin Lee notifications@github.com wrote:

Well, the constructor part I got. You'd just lost me with the factory
part. There was a similar request/conversation about @Constructorhttps://github.com/Constructorarguments elsewhere, too. This is probably a good idea. Provides a bit of
flexibility for users. The mapping code is likely to change along with the
3.x java driver integration so I'd like to defer this until that picture
becomes clearer.

Morphia's perfectly happy without getters/setters for the record, though,
so this feature is probably less pressing than many think. But I know a
many devs hate adding no-arg constructors simply for the sake of
persistence/serialization frameworks so this should probably happen either
way.

Reply to this email directly or view it on GitHubhttps://github.com//issues/581#issuecomment-36238261
.

@evanchooly
Copy link
Member

Good point. i'll tweak that wiki entry.

On Thu, Feb 27, 2014 at 7:50 AM, James Green notifications@github.comwrote:

Where the wiki has an entity being described, I think you have a comment
"Add getters/setters here" which I would interpret as required for
operation. If not, better to say that Morphia does not use getters/setters
for operation, otherwise someone might try to write a setter that does
something more and will get confused when it is not invoked.

Agree about deferring, just want it as a desirable on the radar.

On 27 February 2014 12:48, Justin Lee notifications@github.com wrote:

Well, the constructor part I got. You'd just lost me with the factory
part. There was a similar request/conversation about @constructor<
https://github.com/Constructor>arguments elsewhere, too. This is probably
a good idea. Provides a bit of
flexibility for users. The mapping code is likely to change along with
the
3.x java driver integration so I'd like to defer this until that picture
becomes clearer.

Morphia's perfectly happy without getters/setters for the record, though,
so this feature is probably less pressing than many think. But I know a
many devs hate adding no-arg constructors simply for the sake of
persistence/serialization frameworks so this should probably happen
either
way.

Reply to this email directly or view it on GitHub<
https://github.com/mongodb/morphia/issues/581#issuecomment-36238261>

.

Reply to this email directly or view it on GitHubhttps://github.com//issues/581#issuecomment-36238452
.

@evanchooly evanchooly added this to the post-1.0 milestone Feb 28, 2014
@evanchooly evanchooly removed this from the post-1.0 milestone Jan 14, 2016
@evanchooly evanchooly added this to the 2.0 milestone Jun 13, 2016
McPringle added a commit to friedhof/komunumo that referenced this issue Jun 15, 2017
Morphia can’t deal with immutable entities. There is an issue for this: MorphiaOrg/morphia#581
@evanchooly evanchooly added the experimental implementation details subject to change or removal label Nov 14, 2019
evanchooly added a commit that referenced this issue Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement experimental implementation details subject to change or removal
Projects
None yet
Development

No branches or pull requests

2 participants