I have 2 classes that depend on each other. For the sake of example, let's say that the structure is:
class AuthMiddleware {
public authenticate() {
// uses PostgraphileRouter here to extract some data before the authentication
// authenticates ..
// keeps the userId if authentication succeeded
this.setSessionData(userId);
}
private setSessionData() {..}
public getSessionData() {..}
}
class PostgraphileRouter {
// makes a graphql query that uses AuthMiddleware.getSessionData()
}
I don't want to work with co-dependent classes, so I decided to put
setSessionData()
and getSessionData()
in a SessionManager
class and inject it to both classes.
The problem is that now I have to make setSessionData()
public although I want to keep it available only to AuthMiddleware
class.
Is extracting common code to another class is the best practice when dealing with co-dependency?
If it is, can I make some functions (such as
setSessionData()
) available only to specific classes?
Thanks!