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

Add open initializer function to Entity #157

Closed
wants to merge 4 commits into from

Conversation

lehmanju
Copy link

@lehmanju lehmanju commented Sep 9, 2017

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).

Signed-off-by: Julius Lehmann <[email protected]>
Signed-off-by: Julius Lehmann <[email protected]>
@lehmanju
Copy link
Author

lehmanju commented Sep 9, 2017

Added more flexibility, it is now possible to have separate functions to be called on initialization, one before the user provided init block in new is executed and one after that.

@Tapac
Copy link
Contributor

Tapac commented Sep 11, 2017

@lehmanju , could you provided a real world use-cases when this might be useful?
From my point of view it's not very handy to move this to Entity class, beside you it's always possible to create custom EntityClass.new(before, after, init){} function with same behavior.
Maybe it's just enough to make new open?

@lehmanju
Copy link
Author

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 ...

@Tapac
Copy link
Contributor

Tapac commented Sep 12, 2017

With opening fun new(id: ID?, init: T.() -> Unit): T it will be possible to write something like (see below) to receive same behavior:

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 before and after functions at Entity level is bad approach, because it create feeling that I have to invoke it somehow manually, IMO. Also we want to keep Entity as clean as possible while thinking about data class support (#24)

@lehmanju
Copy link
Author

@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.

Tapac added a commit that referenced this pull request Sep 13, 2017
EntityClass.new {} functions made open to support overriding
@Tapac
Copy link
Contributor

Tapac commented Sep 13, 2017

@lehmanju , I've pushed changes with open new on EntityClass, so now I will close this PR.

@Tapac Tapac closed this Sep 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants