Repository navigation
Conversation
8ef0fd7 to
5f1cb92
Compare
|
Looks like a big hack. What if user wants to have something initialized in the constructor? |
|
Im going to close it, thanks for your efforts. |
|
@pleerock I agree. It is big hack, but correct behavior in my opinion. I was inspired by Doctrine ORM. When entity is deserialized from db, constructor is omitted by reflection. So you have free constructor for initial checks and you don't need write special hack for undefined values. To answer your question: If someone want to initialize something during deserialization from db, there is event (http://typeorm.io/#/listeners-and-subscribers/afterload) |
|
I agree that calling constructor during deserialization is a wrong behavior and suggest to merge this.
|
|
Reopening this issue and according PR. |
|
I think I have change my mind since I see people have inconvenience with strict mode. @Kononnable @vlapo @havenchyk what you think about it? |
|
@pleerock Wow, glad to hear it! Tell me if you need some help |
|
There are reasons why some libraries requires such classes(e.g. entity classes) to have a constructor without parameters. I'm good with both solutions:
|
| // if target is set to a function (e.g. class) that can be created then create it | ||
| if (this.target instanceof Function) | ||
| return new (<any> this.target)(); | ||
| return ObjectInstantiator.createInstanceWithoutConstructor(<any> this.target); |
There was a problem hiding this comment.
You know you could do something like:
const object = {};
Reflect.setPrototypeOf(object, this.target.prototype);
return object;There was a problem hiding this comment.
What about Object.create(this.target.prototype); ? Looks more clear for me.
There was a problem hiding this comment.
@vlapo, yes Object.create will do the job, also Reflect.construct.
@XanderEmu not sure about Reflect.setPrototypeOf, see examples below
class Post {
constructor(title) {
console.log("Inside constructor for " + title);
this._title = title;
}
}
let post1 = new Post("post 1"); // > 'Inside constructor for post 1'
console.log(post1 instanceof Post); // > true (OK !)
console.log(post1._title); // > 'post 1' (OK !)
let post2 = Reflect.construct(function() {}, [], Post);
console.log(post2 instanceof Post); // > true (OK !)
console.log(post2._title); // > undefined (OK !)
const object = {};
let post3 = Reflect.setPrototypeOf(object, Post.prototype);
console.log(post3 instanceof Post); // > false (NOT OK !)
let post4 = Object.create(Post.prototype);
console.log(post4 instanceof Post); // > true (OK !)
console.log(post4._title); // > undefined (OK !)There was a problem hiding this comment.
@XanderEmu Sorry, my bad, I messed it up, my example with Reflect.setPrototypeOf should have been coded properly:
const object = {};
Reflect.setPrototypeOf(object, Post.prototype);
post3 = object;
console.log(post3 instanceof Post); // > trueAll good then !
|
Will this PR make strict mode usable? |
|
Probably. |
|
I can merge this branch if it fix all issues and will be created against |
|
So I tried to do just that. I basically copied the code (submitted by @pepakriz into a new branch created against The tests are failing for I'm not sure how should I proceed. Basically, I don't understand why this is the case. Perhaps, with the change proposed by @pepakriz, it is not a problem anymore? |
|
@XanderEmu I'll need to check tests on |
|
I'm afraid they weren't failing on next. I submitted a PR anyway: |
|
I am good with both solution too. But I have to say that most of my entity classes have a lot of properties so mostly I use only one parameter with data to initialize and always optional. Right now we have some issues and questions about parameters in constructor, after merge we will have question/issues about "non called constructors". So if this change will merged we have to update documentation and inform developers about deserialization process. |
|
@vlapo If you are using strict mode, Typescript complains if you have a non optional property on a class that is not explicitly instantiated in the constructor. This seems like a pretty integral part of type correctness to me. If you are using an optional argument to instantiate your entities, are you doing a null check every time you access a property on an entity? If not, it seems like you are missing out on some of the correctness checking that Typescript could provide. |
|
@pepakriz nice work. I was about to submit a PR tackling the same issue, and I stumbled upon your work. The example you have implemented in your test cases underline the right behavior the ORM should have in my opinion. With this solution, and with a Domain-Driven-Design standpoint: the business process of creating domain object and the technical process of re-creation/hydratation of these pre-existing domain objects are clearly separated. NHibernate for example requires to have a default constructor on the domain classes (that you would usually hide by marking it as Here the solution provided is even more elegant, as it does not require polluting domain classes with useless empty constructor (useless as they add no business meaning to your domain). PS: just for information, I was using |
@vlapo Yes documentation should clearly state that TypeOrm will not call any constructor while re-constructing entities. But again this design sounds more logical, so I am not sure it will raise more questions than before. In my case the question was more to understand "why is the ORM calling the constructor when re-constructing my entities". I add to put if/else within my constructors to avoid execution of some business code by the ORM, raising more questions from other members of the team asking why these if/else were here for ... |
fixes #1772