Introducing Wire: Dependency Injection by Code Generator

Introducing Wire: Dependency Injection by Code Generator

8889da6a67db3667b0694d993c9a962c?s=128

Yosuke Ishikawa

December 18, 2018
Tweet

Transcript

  1. JTILBXB 8JSF ίʔυੜ੒ʹΑΔ%*

  2. None
  3. None
  4. %*

  5. %FQFOEFODZ*OKFDUJPO

  6. package json func NewDecoder(r io.Reader) *Decoder { return &Decoder{r: r}

    }
  7. io.Reader json.Decoder

  8. Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository

    API Server Job A Job B Datastore Client
  9. func main() { ctx := context.Background() client, err := datastore.NewClient(ctx,

    "my-project") if err != nil { log.Fatal(err) } defer client.Close() bookRepository := NewBookRepository(client) bookOwnershipRepository := NewBookOwnershipRepository(client) userRepository := NewUserRepository(client) notificationRepository := NewNotificationRepository(client) bookshelfService := NewBookshelfService(bookRepository, bookOwnershipRepository, inboxService := NewInboxService(userRepository, notificationRepository) server := NewAPIServer(bookshelfService, inboxService) server.Listen() }
  10. Shopping Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository

    Admin Server Job A Job B PubSub Client Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository API Server Job A Job B Datastore Client Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository API Server Job A Job B Datastore Client Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository API Server Job A Job B Datastore Client Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository API Server Job A Job B Datastore Client Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository API Server Job A Job B Datastore Client Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository API Server Job A Job B Datastore Client Recommend Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository Job C Job D Functions Client Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository API Server Job A Job B Datastore Client Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository API Server Job A Job B Datastore Client Bookshelf Service BookRepository BookOwnership Repository ReadHistory Repository History Service UserRepository API Server Job A Job B Datastore Client
  11. notificationRepository := NewNotificationRepository() bookshelfService := NewBookshelfService( bookRepository, bookOwnershipRepository, userRepository) inboxService

    := NewInboxService( userRepository, notificationRepository) bookRepository := NewBookRepository() bookOwnershipRepository := NewBookOwnershipRepository() userRepository := NewUserRepository() notificationRepository := NewNotificationRepository() bookshelfService := NewBookshelfService( bookRepository, bookOwnershipRepository, userRepository) inboxService := NewInboxService( userRepository, notificationRepository) bookRepository := NewBookRepository() bookOwnershipRepository := NewBookOwnershipRepository() userRepository := NewUserRepository() notificationRepository := NewNotificationRepository() bookshelfService := NewBookshelfService( bookRepository, bookOwnershipRepository, userRepository) inboxService := NewInboxService( userRepository, notificationRepository) bookRepository := NewBookRepository() bookOwnershipRepository := NewBookOwnershipRepository() userRepository := NewUserRepository() notificationRepository := NewNotificationRepository() bookshelfService := NewBookshelfService( bookRepository, bookOwnershipRepository, userRepository) inboxService := NewInboxService( userRepository, notificationRepository) bookRepository := NewBookRepository() bookOwnershipRepository := NewBookOwnershipRepository() userRepository := NewUserRepository() notificationRepository := NewNotificationRepository() bookshelfService := NewBookshelfService( bookRepository, bookOwnershipRepository, userRepository) inboxService := NewInboxService( userRepository, notificationRepository) bookRepository := NewBookRepository() bookOwnershipRepository := NewBookOwnershipRepository() userRepository := NewUserRepository() notificationRepository := NewNotificationRepository() bookshelfService := NewBookshelfService( bookRepository, bookOwnershipRepository, userRepository) inboxService := NewInboxService( userRepository, notificationRepository) bookRepository := NewBookRepository() bookOwnershipRepository := NewBookOwnershipRepository() userRepository := NewUserRepository() notificationRepository := NewNotificationRepository() bookshelfService := NewBookshelfService( bookRepository, bookOwnershipRepository, userRepository) inboxService := NewInboxService(
  12. None
  13. 8JSF

  14. None
  15. %*ʹඞཁͳίʔυ

  16. %*ʹඞཁͳίʔυ ຊ࣭ ࣗ໌

  17. %*ʹඞཁͳίʔυ ͜͜Λࣗಈੜ੒ ຊ࣭ ࣗ໌

  18. ຊ࣭ͱ͸

  19. w ͲͷΑ͏ͳґଘ͕͋Δͷ͔ w Ͳ͏΍ͬͯґଘΛऔಘ͢Δͷ͔

  20. w 1SPWJEFS w *OKFDUPS

  21. 1SPWJEFS

  22. func NewBookRepository(client *datastore.Client) BookRepository { ... }

  23. Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository

    API Server Job A Job B Datastore Client 1SPWJEFS
  24. Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository

    API Server Job A Job B Datastore Client 1SPWJEFS
  25. Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository

    API Server Job A Job B Datastore Client 1SPWJEFS
  26. Bookshelf Service BookRepository BookOwnership Repository Notification Repository Inbox Service UserRepository

    API Server Job A Job B Datastore Client 1SPWJEFS
  27. *OKFDUPS

  28. // +build wireinject ... func InitializeServer( ctx context.Context, projectID ProjectID)

    (server *Server, cleanup func(), err error) { wire.Build( ProvideDatastoreClient, NewBookRepository, NewBookOwnershipRepository, NewUserRepository, NewNotificationRepository, NewBookshelfService, NewInboxService, NewAPIServer) return }
  29. $ wire ./...

  30. //+build !wireinject ... func InitializeServer( ctx context.Context, projectID ProjectID) (*Server,

    func(), error) { client, cleanup, err := ProvideDatastoreClient(ctx, projectID) if err != nil { return nil, nil, err } bookRepository := NewBookRepository(client) bookOwnershipRepository := NewBookOwnershipRepository(client) userRepository := NewUserRepository(client) notificationRepository := NewNotificationRepository(client) bookshelfService := NewBookshelfService(bookRepository, bookOwnershipRepository, inboxService := NewInboxService(userRepository, notificationRepository) server := NewAPIServer(bookshelfService, inboxService) return server, func() { cleanup() }, nil }
  31. //+build !wireinject ... func InitializeServer( ctx context.Context, projectID ProjectID) (*Server,

    func(), error) { client, cleanup, err := ProvideDatastoreClient(ctx, projectID) if err != nil { return nil, nil, err } bookRepository := NewBookRepository(client) bookOwnershipRepository := NewBookOwnershipRepository(client) userRepository := NewUserRepository(client) notificationRepository := NewNotificationRepository(client) bookshelfService := NewBookshelfService(bookRepository, bookOwnershipRepository, inboxService := NewInboxService(userRepository, notificationRepository) server := NewAPIServer(bookshelfService, inboxService) return server, func() { cleanup() }, nil } ैདྷखॻ͖͍ͯͨ͠ ίʔυ͕ࣗಈੜ੒͞Εͨ
  32. None
  33. ࣮͸͜ΕΒ΋ࣗಈੜ੒Ͱ͖Δ w ύϥϝʔλʔͷड͚౉͠ w Τϥʔॲཧ w ΫϦʔϯΞοϓॲཧ

  34. ָͳ͚ͩͰ͸ͳ͍

  35. ίʔυੜ੒ʹΑΔґଘؔ܎ͷղܾͳͷͰ άϥϑͷܽଛΛίϯύΠϧ࣌ʹൃݟͰ͖Δ

  36. ·ͱΊ

  37. w 8JSF͸%*ͷࣗ໌ͳίʔυΛࣗಈੜ੒͢Δπʔϧ w ґଘؔ܎͸ίϯύΠϧ࣌ʹղܾ͞ΕΔͷͰ҆શ

  38. IUUQTHJUIVCDPNHPPHMFXJSF