question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom getters with Global Database object removed

See original GitHub issue

I’ve been trying Ktorm 2.7-RC that you uploaded after my request (thank you!).

In practice, I’ve found the new way of using Ktorm quite uncomfortable as now I’ve to pass around a database object. While this is doable, one place where I’ve been struggling with the new APIs is the custom setters. A while back you gave us an example of using a custom getter inside an entity interface:

interface Department : Entity<Department> {
    val id: Int
    val name: String
    val employees get() = Employees.findList { it.departmentId eq id }
}

Do you have an idea how to achieve this with the new API?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vincentlauvlwjcommented, Jan 6, 2020

Yes, but then getList depends on the global db connection which means I wouldn’t have any control over running it on a different connection. Severely restricts the power of connection switching and, on a initial thought, in a way defeats the purpose of removing the global db object.

Yes, that’s exactly why I decided to remove Database.global. Here I just provide a workaround for you because you might want to use the global database like before.

1reaction
vincentlauvlwjcommented, Jan 6, 2020

I actually thought about doing that but I’d have to make it a lateinit var making it mutable from anywhere. Maybe I leave it like that or do something so that it can only be initialized once.

You can change the visibility of the setter, so that it can only be modified inside the kt file:

lateinit var globalDatabase: Database 
    private set
Read more comments on GitHub >

github_iconTop Results From Across the Web

Lombok how to customise getter for Boolean object field?
I have declared @Data at class level and have too many attributes in the class, so excluding getter or making the default getter...
Read more >
Property getters and setters - The Modern JavaScript Tutorial
Accessor properties are represented by “getter” and “setter” methods. In an object literal they are denoted by get and set :.
Read more >
Best Practices for Java Getter and Setter - DZone
In this post, we take a closer look at getter and setter methods in Java, common mistakes, and best practices for combating these...
Read more >
getter - JavaScript - MDN Web Docs - Mozilla
On getting the property, the property is removed from the object and re-added, but implicitly as a data property this time.
Read more >
Avoid getters and setters whenever possible
Noooo!!! Don't click that generate getters and setters option!!! I like the rule: "Don't use accessors and mutators." Like any good rule, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found