-
Notifications
You must be signed in to change notification settings - Fork 697
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
Add open initializer function to Entity #157
Conversation
Signed-off-by: Julius Lehmann <[email protected]>
Signed-off-by: Julius Lehmann <[email protected]>
Signed-off-by: Julius Lehmann <[email protected]>
Added more flexibility, it is now possible to have separate functions to be called on initialization, one before the user provided |
Signed-off-by: Julius Lehmann <[email protected]>
@lehmanju , could you provided a real world use-cases when this might be useful? |
I have added extra functionality to my Entity. On creation some actions should be performed via a REST Api but only once, of course. The point of having an initializer inside the Entity is that I don't want to write common code in every invocation of new(). So I need a way to detect whether this Entity is created from scratch or just loaded from the database. Maybe there are better ways to do this ... |
With opening class TestEntity(id: EntityID<Int>) : Entity<Int>(id) {
companion object : IntEntityClass<TestEntity>(IntIdTable) {
override fun new(init: TestEntity.() -> Unit) =
super.new{ before(); init() }.apply {
after()
}
}
} I just think that having |
@Tapac I'd be happy to have new as an open function, your proposal is actually better ;) Thank you very much, in the future I would probably use data classes as well. |
EntityClass.new {} functions made open to support overriding
@lehmanju , I've pushed changes with |
This would allow any subclass of Entity to detect whether it is being initialized (by new{}) or just created from previously existing database values. Custom entities could overwrite this method to do certain initializing stuff because the default init block is called every time (also on loading from the database).