SlideShare a Scribd company logo
1 of 23
Download to read offline
1
wireというDIツールをプロダクトで使ってみた
2
● メイン所属:信州大学工学部4年

● サブ所属:メルペイ(インターン)

● ポジション:バックエンド

● 趣味:カメラ、筋トレ

田村 弘(@bieshan)

3
wireとは何か
今日は何を話すか
wireの仕組み
wireをプロダクトに入れてみた
wireの難しいところ
02
03
04
01
4
wireとは何か
● DI(Dependency injection(依存性の注入))
○ 簡単に必要な依存関係を注入してくれる
● DIツールは何か
○ 依存関係をごにょごにょして楽にしてくれるツール
● wireの場合
○ 依存関係のコードを生成してくれる
5
wireとは何か
● DI(Dependency injection(依存性の注入))
○ 簡単に必要な依存関係を注入してくれる
● DIツールは何か
○ 依存関係をごにょごにょして楽にしてくれるツール
● あると何が嬉しいのか
○ 依存関係用のコードを減らすことができる
○ 依存関係が明白になる
6
wireとは何か
● GoのDIツールには二種類ある
○ runtimeに依存を解決するもの
○ コンパイル時に依存を解決するもの
● wireはコンパイル時で、要はコードを生成してくれる
7
wireの仕組み(雰囲気)
NewXX(*DB, *Logger) *Service
NewDB() *DB
NewLogger() *Logger
Serviceを返すメ
ソッドが欲しい
Serviceを返すメソッド
依存されるメソッド達
了解
8
wireの仕組み(雰囲気)
9
wireの仕組み(雰囲気)
10
wireの仕組み(雰囲気)
11
wireをプロダクトに導入してみた
● プロダクトというと
○ 基本的にloggerとかDBとか複数の依存先を持つ
○ 特にテストだと場合分けが多く、同じ生成コードが繰り返される
func Test(t *testing) {
requiredService1 := FakeService1()
requiredService2 := FakeService2()
.
.
.
TestService := NewTestService(
requiredService1,
requiredService2,
.
.
.
)
// 略
}
12
wireをプロダクトに導入してみた
● プロダクトというと
○ 基本的にloggerとかDBとか複数の依存先を持つ
○ 特にテストだと場合分けが多く、同じ生成コードが繰り返される
func TestServiceSuccess(t *testing) {
requiredService1 := FakeService1()
requiredService2 := FakeService2()
requiredService3 := FakeService3()
TestService := NewTestService(
requiredService1,
requiredService2,
requiredService3,
)
assert.True(t, TestService.run())
}
requiredService1 := FakeService1()
requiredService2 := FakeService2()
ここの部分コードをwireで生成する
13
wireをプロダクトに導入してみた
func InitTestService() *Service {
wire.Build(
FakeService1,
FakeService2,
FakeService3,
FakeService4,
NewTestService,
)
return nil
}
Wireを使って
コードを生成
func InitTestService() *Service {
FakeService1 := FakeService1()
FakeService2 := FakeService2()
FakeService3 := FakeService3()
FakeService4 := FakeService4()
service := NewTestService(
requiredService1,
requiredService2,
requiredService3,
requiredService4,
)
return service
}
14
wireをプロダクトに導入してみた
func Test(t *testing) {
requiredService1 := FakeService1()
requiredService2 := FakeService2()
.
.
.
TestService := NewTestService(
requiredService1,
requiredService2,
.
.
.
)
// 略
}
func Test(t *testing) {
testService := InitTest()
// 略
}こうなる
15
wireの難しいところ
NewXX(*DB, *DB) *Service
NewFooDB() *DB
NewBarDB() *DB
どっち?
Serviceを返すメソッド
依存されるメソッド達
16
wireの難しいところ
新しい型を作ればいい
type MySQLConnectionString string
17
wireの難しいところ
func NewMySQLConn() string {}
func NewMySQLConn() MySQLConnectionString {}
書き換えないといけない
18
wireの難しいところ
全部が全部それができるわけではない...
19
wireの難しいところ
例えば、外部ライブラリーのメソッドを使う場合
fooDb := mySQL.NewConn()
barDb := mySQL.NewConn()
hogeService := NewService(fooDb, barDb)
型を区別するため新しい型を作りたいが....
20
wireの難しいところ
func NewConn() *Conn {}
ライブラリー内部
ライブラリー内部まで書き換えられない...
21
wireの難しいところ
なので、
type HogeDBConn struct {
Conn *mySQL.Conn
}
構造体でラップをして使う
22
まとめ
● いいところ
○ 依存性の注入が楽になる
○ 依存関係が明白
○ コードが綺麗になる
● 難しいところ
○ 書くコードに制限がかかる
23
ご清聴ありがとうございました。

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

wireをプロダクトに導入してみたという話