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

Can we use a pointer for Repository assignment? #61

Open
frederikhors opened this issue Jul 8, 2022 · 3 comments
Open

Can we use a pointer for Repository assignment? #61

frederikhors opened this issue Jul 8, 2022 · 3 comments

Comments

@frederikhors
Copy link

Given the repository in repository.go:

type Repository interface {
  AddTraining(ctx context.Context, tr *Training) error
  //...
}

we are creating new structs which implement that interface using code such as in trainings_firestore_repository.go:

type TrainingsFirestoreRepository struct {
  firestoreClient *firestore.Client
}

func NewTrainingsFirestoreRepository(
  firestoreClient *firestore.Client,
) TrainingsFirestoreRepository {
  return TrainingsFirestoreRepository{
    firestoreClient: firestoreClient,
  }
}

and we are create commands and queries with those structs, such as in service.go:

func newApplication(ctx context.Context, trainerGrpc command.TrainerService, usersGrpc command.UserService) app.Application {
  client, err := firestore.NewClient(ctx, os.Getenv("GCP_PROJECT"))
  //...
  trainingsRepository := adapters.NewTrainingsFirestoreRepository(client)
  //...
  return app.Application{
    Commands: app.Commands{
      ApproveTrainingReschedule: command.NewApproveTrainingRescheduleHandler(trainingsRepository, usersGrpc, trainerGrpc, logger, metricsClient),
      //...
    },
    Queries: app.Queries{
      AllTrainings:     query.NewAllTrainingsHandler(trainingsRepository, logger, metricsClient),
      //...
    },
  }
}

satisfying func signature like:

func NewApproveTrainingRescheduleHandler(
  repo training.Repository,
  //...
) decorator.CommandHandler[ApproveTrainingReschedule] {}

Question

In each func like command.NewApproveTrainingRescheduleHandler(trainingsRepository, /*...*/) we are passing trainingsRepository each time different (passed-by-value).

Can we use a pointer there avoiding maybe useless memory consumption?

@m110
Copy link
Member

m110 commented Jul 9, 2022

Hey @frederikhors.

Sure, there's no issue with using pointers to repositories or other adapters. In most cases, you would find the effect on memory negligible, though. In the example above, the only field of the struct is a pointer, so copying it won't be noticeable in any way.

@frederikhors
Copy link
Author

Ok. But I'm not able to use pointers. The compiler reject it. Can you try?

@m110
Copy link
Member

m110 commented Jul 30, 2022

What error did you get? You will probably need to change some occurrences in the code to match pointers, but it should be straightforward.

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

No branches or pull requests

2 participants