Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introducing Wire: Dependency Injection by Code ...

Introducing Wire: Dependency Injection by Code Generator

Yosuke Ishikawa

December 18, 2018
Tweet

More Decks by Yosuke Ishikawa

Other Decks in Technology

Transcript

  1. %*

  2. 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() }
  3. 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
  4. 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(
  5. // +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 }
  6. //+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 }
  7. //+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 } ैདྷखॻ͖͍ͯͨ͠ ίʔυ͕ࣗಈੜ੒͞Εͨ