æè¿ãgolang ã®ã¬ã¤ã¤æ§é ã«ããã¦ãä»ã®ã³ã¼ãã«å½±é¿ãªãã¤ã³ãã©ã¬ã¤ã¤ã®ãã¼ã¿ã½ã¼ã¹å®è£ ãå·®ãæ¿ãããã¨ã¯å¯è½ã? ã¨ãã質åãåããã
åçæéãéããã¦ããä¸ã§è³ªåãåããã®ã§ã
ãç¾å®çã«ã¯é£ãããã¨ããéãªåçããã§ããªãã£ãã
ãããã«éããããªã¨æã£ãã®ã§ã
èªåãªãã®åçãã¡ããã¨æ®ããã¨æãã
- å½±é¿ãåãã対象ã¨ãªãã³ã¼ãã¯?
- MySQL -> PostgresSQL ã¸ã®å·®ãæ¿ã
- MySQL -> WebAPI ã¸ã®å·®ãæ¿ã
- DDDæ¬ & IDDDæ¬ã®ãµã³ãã«ã¯ã©ããªã£ã¦ããã?
- å·®ãæ¿ããå¿ è¦ã¯ããã®ã?
- ã¾ã¨ã
å½±é¿ãåãã対象ã¨ãªãã³ã¼ãã¯?
golang ã®ã¬ã¤ã¤æ§é ã«ããã¦ãä»ã®ã³ã¼ãã«å½±é¿ãªãã¤ã³ãã©ã¬ã¤ã¤ã®ãã¼ã¿ã½ã¼ã¹å®è£ ãå·®ãæ¿ãããã¨ã¯å¯è½ã?ã¨ããã¨ããã® ä»ã®ã³ã¼ã ã¨ã¯å ·ä½çã«ã©ãã«ãªãã®ã?
ããã¯ã¬ã¤ã¤ã¢ã¼ããã¯ãã£ã®ä¾åé¢ä¿ä¸ã
UIã¬ã¤ã¤ã¨ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã該å½ããã
ãã¡ã¤ã³ã¬ã¤ã¤ãå½±é¿ãåããå¯è½æ§ã¯ãããã
ãã¡ã¤ã³ã¬ã¤ã¤ã¨ã¤ã³ãã©ã¬ã¤ã¤ã« DIP ãé©ç¨ãããã¨ã§åé¿ãããã¨ãã§ããã
ã¡ãªã¿ã«ã
DIP ã¯ããã¾ã§ ãã¡ã¤ã³ã¬ã¤ã¤ãå½±é¿ãåããªãããã«ããã ã ãªã®ã§ã
DIP ãé©ç¨ãã = ã¤ã³ãã©ã¬ã¤ã¤ã®å·®ãæ¿ããå¯è½
ã¨ããããã§ã¯ãªãã
ã¤ã³ãã©ã¬ã¤ã¤ã«ä¾åããUIã¬ã¤ã¤ã¨ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã¯å½±é¿ãåããã
ä»åã¯ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã«æ³¨ç®ãã¦èãã¦ã¿ãã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã¸ã®èãæ¹ã¯ããã®ã¾ã¾UIã¬ã¤ã¤ã«é©ç¨ã§ããã¯ãã
以ä¸ã¯ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã®å®è£
ã§ã
AddUser() ã§ã¯ User ãä¿åããã®ã« Repository ãå©ç¨ãã¦ããã
pakcage application func AddUser(user User) error { dbConnection := NewDBConnection() dbConnection.Begin() userRepository := NewUserRepository(dbConnection) if err := userRepository.Add(user); err != nil { dbConnection.Rollback() return error } dbConnection.Commit() return nil }
UserRepository ã«é¢ãã¦ã¯ã
ãã¡ã¤ã³ã¬ã¤ã¤ã« interface ãç½®ãã
package domain type UserRepository interface { AddUser(user User) error }
ã¤ã³ãã©ã¬ã¤ã¤ã«å®è£
ãç½®ãã
package infra type UserRepository struct { conn DBConnection } func NewUserRepository(conn DBConnection) domain.UserRepository { return &UserRepository { conn } } func (u *UserRepository) AddUser(user domain.User) error { return u.conn.Insert(user) }
ä»åã¯ãã®å®è£
ããã¼ã¹ã«ã¤ã³ãã©ã¬ã¤ã¤ã®ãã¼ã¿ã½ã¼ã¹å·®ãæ¿ãã«ã¤ãã¦èãã¦ããã
MySQL -> PostgresSQL ã¸ã®å·®ãæ¿ã
å ç¨ç´¹ä»ããå®è£ ã MySQL ã§ããã¨ãã¦ãä»ã®ã³ã¼ãã«å½±é¿ãªã PostgresSQL ã«å·®ãæ¿ãããã¨ã¯ã§ããã ããã?
çµè«ããè¨ãã¨ãå¯è½ã ã¨æãã
ãªããã¨ããã¨ã
golang ã® sql ããã±ã¼ã¸ã¯ãå
·ä½ç㪠RDB ã«ä¾åããªãããã«ä½ããã¦ããããã
å
ç¨ç´¹ä»ããã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤å®è£
ã¯æ¬ä¼¼ã³ã¼ãã«ãªã£ã¦ãããã
sql ããã±ã¼ã¸ã¯å©ç¨ããSQLãã©ã¤ããå·®ãæ¿ããã ã㧠MySQL -> PostgresSQL ã¸ã®å·®ãæ¿ããã§ããã
ORM ã sql ããã±ã¼ã¸ã«æºæ ãã¦ããã¯ããªã®ã§ã
åé¡ãªãå·®ãæ¿ããããã¯ãã
ãã¡ãããSQLãã©ã¤ãã® import æãªã©ç´°ããä¿®æ£ã¯ããã ãããã
åºæ¬ã¤ã³ãã©ã¬ã¤ã¤ã«éããä¿®æ£ã«ãªãã®ã§ã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã«å½±é¿ãä¸ãããã¨ã¯ãªãã¯ãã
ã¨ãããã¨ã§ã
ä»ã® RDB ã¸ã®å·®ãæ¿ãã¯ã¬ã¤ã¤æ§é ã«é¢ä¿ãªãå¯è½ã ã£ããããã
MySQL -> WebAPI ã¸ã®å·®ãæ¿ã
ãããããæ¬çªãMySQL ã WebAPI ã«å·®ãæ¿ãããã¨ã¯ã§ããã ããã?
çµè«ããè¨ãã¨ãå®è£ æ¹æ³ã«ãã£ã¦ã¯å¯è½ ã¨ãããã¨ã«ãªãã
MySQL -> PostgresSQL ã¸ã®å·®ãæ¿ã㯠sql ããã±ã¼ã¸ãäºææ§ãæ
ä¿ãã¦ããã¦ãããã
MySQL -> WebAPI ã¯æ
ä¿ãã¦ãããªãã®ã§ã
èªåã§é å¼µãå¿
è¦ãããã
ä»åã®å®è£
ä¾ã ã¨ã
NewDBConnection() ã®ããã« RDB ã«ä¾åãã¦ããã³ã¼ããåå¨ããã
RDB ã«ä¾åãã¦ããã³ã¼ããåå¨ããå®è£
ã ã¨ã WebAPI ã¸ã®å·®ãæ¿ãã¯ç¡çã
pakcage application func AddUser(user User) error { //RDBã«ä¾åãã¦ãã dbConnection := NewDBConnection() dbConnection.Begin() userRepository := NewUserRepository(dbConnection) if err := userRepository.Add(user); err != nil { dbConnection.Rollback() return error } dbConnection.Commit() return nil }
ã©ãããã°å·®ãæ¿ããããã ããã?
ã¤ã³ãã©ã¬ã¤ã¤ã«DBä¾åã®ã³ã¼ããã¾ãã£ã¨å®è£ ãã¦ãã¾ã
ããã¨æãã¤ãã®ã¯
ã¤ã³ãã©ã¬ã¤ã¤ã«DBä¾åã®ã³ã¼ããã¾ãã£ã¨å®è£
ãã¦ãã¾ãã¢ããã¼ãã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ãã
DBæ¥ç¶ããã©ã³ã¶ã¯ã·ã§ã³ã®ã³ã¼ããæ¶ãã¦ãã¾ãã
pakcage application func AddUser(user User) error { userRepository := NewUserRepository() if err := userRepository.Add(user); err != nil { return error } return nil }
ã¤ã³ãã©ã¬ã¤ã¤ ã«ãããã®å¦çãå®è£
ãã¦ãã¾ãã
package infra type UserRepository struct { conn DBConnection } func NewUserRepository() domain.UserRepository { } func (u *UserRepository) AddUser(user domain.User) error { u.conn := NewDBConnection() u.conn.Begin() if err := DBConnection.Insert(user); err != nil { u.conn.Rollback() return err } u.conn.Commit() }
ããããã°ã
MySQL -> WebAPI ã¨ããå·®ãæ¿ãã§ãã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã«ä¿®æ£ã¯ä¸è¦ã
ã¤ã³ãã©ã¬ã¤ã¤å
ã®ä¿®æ£ã ãæ¸ãã
ããã¯çµæ§ä¸è½ã§ã
WebAPI ã ãã§ã¯ãªãã
redis ãç©çãã¡ã¤ã«ãªã©ããããã¯ãã
PaaS ãå©ç¨ããå ´åã¯ã
PaaS ç¬èªã®ãªãã¸ã§ã¯ããå¼ãåãå¿
è¦ããããããããªãã
ãã ããã®å®è£ ã ã¨ãã©ã³ã¶ã¯ã·ã§ã³ãåé¡ã«ãªãã
以ä¸ã®ããã« User 㨠Task ãåæã«ä¿åããå ´åã
User 㨠Task ã«ãã©ã³ã¶ã¯ã·ã§ã³ãããããã¨ãã§ããªãã
pakcage application func AddUser(user User, task Task) error { userRepository := NewUserRepository() if err := userRepository.Add(user); err != nil { return error } taskRepository := NewTaskRepository() if err := taskRepository.Add(task); err != nil { return error } return nil }
User 㨠Task ã¯çµææ´åæ§ã«ãªãã®ã§ã
Task ã®ä¿åã失æããå ´åã®ãªã«ããªå®è£
ãå¿
è¦ã«ãªãã
ã¾ããã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã§ãã©ã³ã¶ã¯ã·ã§ã³ç®¡çãã¦ããªãã®ã§ã
ãªãã¸ã§ã¯ãã®æ´åæ§ãã©ã®ã¹ã³ã¼ãã§ä¿ããã¦ããã®ã?
ãæ示ã§ãã¦ãªãã®ãæ°ã«ãªãã¨ããã
User 㨠Task ã«ãã©ã³ã¶ã¯ã·ã§ã³ããããã¨ãã観ç¹ã§ã¯ã
以ä¸ã®ããã« User, Task ãä¸ç·ã«ä¿åãããã㪠Repository ãä½ãã°è§£æ±ºãããã
pakcage application func AddUser(user User, task Task) error { userRepository := NewUserRepository() if err := userRepository.Add(user, task); err != nil { return error } return nil }
ãããã以ä¸ã®2ç¹ã«ãããå人çã«ã¯ç©æ¥µçã«ä½¿ããã¨ã¯æããªãã
1. Repository å®è£
ï¼ã¤ã³ãã©ã¬ã¤ã¤ï¼ã« User, Task ãå©ç¨ãããã¸ãã¯ãæµåºããå¯è½æ§ããã
æµåºããªãããã«æ¸ãã°ããã®ã ãã
ãã¼ã éçºã¨ãã ã¨æµåºããå¯è½æ§ãé«ããªãæ¸å¿µãããã®ã§ã
ç¾å®çã§ã¯ãªãããªã¨æã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤å
ã§æ±ããªãã¸ã§ã¯ããå¢ããã°å¢ããã»ã©ã
å¼æ°ãå¢ãã¦ããã
ããã«ä¼´ã£ã¦ãã¸ãã¯ãæµåºããå¯è½ãé«ããªãã¨ããã®ãæ¸å¿µã¨ãã¦ããã
2. User, Task ãå¼æ°ã«åãã¤ã³ã¿ã¼ãã§ã¼ã¹ãä¸èªç¶
ããã¯å®å
¨ã«å人ã®å¥½ã¿ã ãã
Repository ã¯åä¸ã®éç´ã CRUD ãããã®ãªã®ã§ãéåæãããã
DDDã®å ´å
ããã§ãDDDã®å ´åã«ã¤ãã¦ãè¨åãã¦ãããã¨æããDDDã«ã¯
åä¸ã®ãã©ã³ã¶ã¯ã·ã§ã³ã§ã¯åä¸ã®éç´ã®ã¿å¤æ´å¯è½
ã¨ããã«ã¼ã«ãããã®ã§ã
å
ç¨ã® User 㨠Task ããã©ã³ã¶ã¯ã·ã§ã³ã§æ±ãå ´åã¯ã
以ä¸ã®ããã« User ã Task ãæã¤ãããªã¢ããªã³ã°ã«ãªã£ããã
type User struct { Name string Tasks []*Task }
User, Task ãå«ãå¥ã® struct ãå®ç¾©ãã¦ã
ããããä¿åãã Repository ãå®è£
ãããã¨ã«ãªãã¨æãã
ï¼ãã¡ãããã¡ããã¨æå³ã®ãã struct ãè¦ã¤ãã£ãå ´å
type SomeThing struct { User User Tasks []*Task }
ããããããããã®éç´ã®ã¢ããªã³ã°ãé£ãã ã¨ããå¥ã®åé¡ããããã
DDDã«ããã¦ã¯ã
ã¤ã³ãã©ã¬ã¤ã¤ã«DBä¾åã®ã³ã¼ããã¾ãã£ã¨å®è£
ãã¦ãã¾ãã¢ããã¼ãã¯æªããªãæ°ããã¦ããã
ã¨æã£ããã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã§ãã©ã³ã¶ã¯ã·ã§ã³ç®¡çã§ãã¦ããªããã
ãã¡ã¤ã³ã¤ãã³ãã®ã¤ãã³ãã¹ãã¢ãæ±ããã¨ããã¨ã
ãã®ã¢ããã¼ãããã£ããããªãã
ã¤ãã³ãã¹ãã¢ã¯éç´æ´æ°ã¨åããã¼ã«ã«ãã©ã³ã¶ã¯ã·ã§ã³ã§ç®¡çãã
éç´æ´æ°ã¨ã¤ãã³ãçºè¡ã«æ´åæ§ãæãããå¿
è¦ãããã®ã§ã
Repository ã®ä¸ã§ãã©ã³ã¶ã¯ã·ã§ã³ãå®çµããã¦ãã¦ãã¾ãã¨ã
Repository ã®ä¸ã§ãã¡ã¤ã³ã¤ãã³ããçºè¡ããå®è£
ã«ããå¿
è¦ãããã
ï¼ããã¯å®è£
æ¹æ³ã«ãã£ã¦å¤ããã®ã§ããå¿
è¦ããããã¨ã¯éããªããã©ã»ã»ã»ã
ãããã
ãã¡ã¤ã³ã¤ãã³ãã¯éç´ã®æ¯ãèãï¼ã¡ã½ããï¼ãéãã¦çºè¡ããããã®ãªã®ã§ã
Repository ã®ä¸ã§ãã¡ã¤ã³ã¤ãã³ããçºè¡ããå®è£
ãåºæ¬çã«NGã
Repository ã¯ãã¡ã¤ã³ã¤ãã³ããçºè¡ãã責åãæããªãã
ï¼ãã ããéç´ã®æ¯ãèããéãã¦çºè¡ãããªããã¡ã¤ã³ã¤ãã³ãã¯ããã¡ã¤ã³ã¤ãã³ãèªä½ãéç´ã¨ãã¦æ±ãã®ã§ãRepository å
ã§çºè¡ãã¦ãOKã
ç¬èªã®æ¥ç¶ãªãã¸ã§ã¯ããä½ã
ç¬èªã®æ¥ç¶ãªãã¸ã§ã¯ããä½ãã¨ããã®ããé¸æè¢ã¨ãã¦ã¯ããæ°ãããã以ä¸ã® DBConnection ãã
pakcage application func AddUser(user User) error { dbConnection := NewDBConnection() dbConnection.Begin() userRepository := NewUserRepository(dbConnection) if err := userRepository.Add(user); err != nil { dbConnection.Rollback() return error } dbConnection.Commit() return nil }
以ä¸ã®ããã«ç¬èªã§ä½ã£ã AppConnection ã«å¤æ´ããã
pakcage application func AddUser(user User) error { appConnection := NewAppConnection() appConnection.Begin() userRepository := NewUserRepository(appConnection) if err := userRepository.Add(user); err != nil { appConnection.Rollback() return error } appConnection.Commit() return nil }
AppConnection ã®å®è£
ã¯ä»¥ä¸ã
type AppConnection struct { conn DBConnection } func NewAppConnection() *AppConnection { conn := NewDBConnection() return &AppConnection {conn} } func (a *AppConnection) Begin() { //çç¥ } func (a *AppConnection) Commit() { //çç¥ } func (a *AppConnection) Rollback() { //çç¥ }
ãã®ããã«DBæ¥ç¶ã AppConnection ã§é è½ãããã¨ã«ãã£ã¦ã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã¯DBæ¥ç¶ã«ä¾åãããã¨ããªããªãã
MySQL -> WebAPI ã«å¤æ´ãããã¨ãã¯ã以ä¸ã®ããã« AppConnection ãä¿®æ£ããã
type AppConnection struct { webAPI WebAPI } func NewAppConnection() *AppConnection { webAPI := NewWebAPII() return &AppConnection {webAPI} } func (a *AppConnection) Begin() { //WebAPI ã«ã¯ãã©ã³ã¶ã¯ã·ã§ã³ããªãã®ã§ã空å®è£ ã«ããã return } func (a *AppConnection) Commit() { //WebAPI ã«ã¯ãã©ã³ã¶ã¯ã·ã§ã³ããªãã®ã§ã空å®è£ ã«ããã return } func (a *AppConnection) Rollback() { //WebAPI ã«ã¯ãã©ã³ã¶ã¯ã·ã§ã³ããªãã®ã§ã空å®è£ ã«ããã return }
èããã¾ã§ããªãããã®å®è£
ã«ã¯åé¡ãããã
ã¾ããWebAPI ã«ã¯ãã©ã³ã¶ã¯ã·ã§ã³ããªãå¯è½æ§ãé«ãã
WebAPI 以å¤ã®ãã¼ã¿ã½ã¼ã¹ã§ããã©ã³ã¶ã¯ã·ã§ã³ãåå¨ããªããã®ã¯å¤ãã
ãã©ã³ã¶ã¯ã·ã§ã³ãåå¨ããªãã®ã«ã
AppConnection ã¯ãã©ã³ã¶ã¯ã·ã§ã³ãå©ç¨ããããã® Begin(), Commit(), Rollback() ãæã£ã¦ããã®ã§ã
ãããã¯ç©ºå®è£
ã«å¤æ´ããå¿
è¦ãããã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã®ã³ã¼ããèªãã¨ã
ãããã©ã³ã¶ã¯ã·ã§ã³ãå©ç¨ãã¦ããããã«è¦ãããã
å®éã¯å©ç¨ãã¦ããªãã¨ãããã¨ã«ãªãã
ç½ ã§ãããªãã
ããã«ãä¸é¨ã® Repository 㯠WebAPI ã§ã
ä»ã® Repository 㯠RDB ã®å ´åã
AppConnection ã¯å©ç¨ã§ããªãã
ããããã
以ä¸ã®ããã« NewRDBAppConnection() 㨠NewRDBAppConnection() ã®ããã«ã
æ¥ç¶ãçæããé¢æ°ãè¤æ°ä½ã£ã¦ã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã§ä½¿ãåãããããªä»çµã¿ãå¿
è¦ã«ãªãã
type AppConnection struct { webAPI WebAPI db DBConnection } func NewAppConnection() *AppConnection { webAPI := NewWebAPII() return &AppConnection { WebAPI: webAPI, } } func NewAppConnection() *AppConnection { db := NewDBConnection() return &AppConnection { db: db, } }
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã§ä½¿ãåããã¨ãããã¨ã¯ã
å®è£
å·®ãæ¿ãã«ãã£ã¦å½±é¿ãåºã¦ãã¾ãã¨ãããã¨ãªã®ã§ã
ãã®å®è£
ãå¾®å¦ã
ã¡ãªã¿ã«ã
ãããã£ãDBã®ãã©ã³ã¶ã¯ã·ã§ã³ãã©ããããç¬èªãªãã¸ã§ã¯ããä½æããå®è£
æ¹æ³ã¯ãã¾ã好ãã§ã¯ãªãã
好ãã§ã¯ãªãã¨ãããã
ãããªãã®è¦ä»¶ããªãéãã
æ¥ç¶ãªãã¸ã§ã¯ãã®ãã¡ã¯ããªãä½ãã°äºè¶³ããã¨æãã®ã§ã
å®è£
ãã¦å¬ãããªãã¤ã¡ã¼ã¸ããªãã
èªåãæã¯DBã®ãã©ã³ã¶ã¯ã·ã§ã³ãã©ããããç¬èªãªãã¸ã§ã¯ããéç£ããã¿ã¤ãã®äººéã ã£ããã©ã
ãããã£ãç¬èªãªãã¸ã§ã¯ã㯠DBManager, TransactionController ã®ãããª
ããã¨è¦ã§ã¤ããããªå½åãããã¦ããå¾åã«ããæ°ãããã
DDDæ¬ & IDDDæ¬ã®ãµã³ãã«ã¯ã©ããªã£ã¦ããã?
ä»ã«ããã¼ã¿ã½ã¼ã¹ãå·®ãæ¿ãããå®è£ ã¯ãããããããªããããããã DDDæ¬ & IDDDæ¬ ã¯ãã¼ã¿ã½ã¼ã¹ã®å·®ãæ¿ããæ ä¿ãã¦ãã®ã?
ãµã³ãã«ãä»åã®å®è£ ä¾ã¨åãããã«ãã¼ã¿ã½ã¼ã¹ã¨ã㦠RDB ãå©ç¨ãã¦ããã
ãã ãã
java + spring ã¨ãããã¨ãããã
ã¢ããã¼ã·ã§ã³ã§ RDB ã®ãã©ã³ã¶ã¯ã·ã§ã³ã管çã§ããããã«ãªã£ã¦ããã
æ示çãªã³ãããããã¼ã«ããã¯ã®ã³ã¼ãã¯ä¸è¦ã ãã
ãã¡ã¤ã³ã¤ãã³ãã®çºè¡ãåä¸ã®ãã©ã³ã¶ã¯ã·ã§ã³ã¹ã³ã¼ãã§ç®¡çã§ãã¦ããã
RDB ã WebAPI ã«ããã¨ã©ããªãã ããã?
RDBã®ãã©ã³ã¶ã¯ã·ã§ã³ã¯å¹ããªããªããã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã«æ示çãªã³ãããããã¼ã«ããã¯ã®ã³ã¼ããåå¨ããªãã®ã§ã
ãã©ã³ã¶ã¯ã·ã§ã³ã®ã¢ããã¼ã·ã§ã³ãåé¤ããã°ä¿®æ£å®äºãªæ°ãããã
ãµã³ãã«ã§ã¢ããªã±ã¼ã·ã§ã³ã®ä¿®æ£ãå¿
è¦ã¨æããããã¿ã¼ã³ã¨ãã¦ã¯ã
ã¢ããã¼ã·ã§ã³ã§ãã©ã³ã¶ã¯ã·ã§ã³ãå¶å¾¡ã§ããªããã¼ã¿ã½ã¼ã¹ã«å·®ãæ¿ãã
ã¨ããã®ã該å½ããã®ããªã
ã©ããªãã¼ã¿ã½ã¼ã¹ã«ãå·®ãæ¿ãå¯è½ã¨ããããã§ã¯ãªãã¯ãã
èªå㯠java ããåãããªãã®ã§ã
ã»ã¼åã§èå¯ããã ãã ãã
java 㯠AOP ã«ãããµãã¼ããããã®ã§ã
golang ã»ã©è¦å´ããªãå°è±¡ãåããã
å·®ãæ¿ããå¿ è¦ã¯ããã®ã?
ã¨ãããã¨ã§ãèªåãèããçµæã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã«å½±é¿ãªãå·®ãæ¿ããã®ã¯é£ããããªã¨æã£ãã
èªåã¯ã©ããã¦ããã®ãã¨ããã¨ã
ã¤ã³ãã©ã¬ã¤ã¤ã®å·®ãæ¿ãã¯èæ
®ãã¦å®è£
ãã¦ããªãã
å·®ãæ¿ããçºçããå ´åãã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã®ä¿®æ£ãçºçããã®ã¯ä»æ¹ãªãã¨æã£ã¦ããã
å·®ãæ¿ã対象ã«ãã£ã¦èæ
®ããè¦ç´ ãæ¿ãã£ã¦ãããã
å·®ãæ¿ããçºçãããã©ãããåãããªãã®ã§ã
å¿
è¦ã«ãªã£ãéã«ãããªãã®å·¥æ°ããããã®ãã·ã³ãã«ãªæ°ãããã
ãã¡ãããç´è¿ã§æ³å®ãããã®ã§ããã°ããããªãã®ä»çµã¿ãå®è£
ããã®ããããããããªããã»ã»ã»ã
ãã ããã¡ã¤ã³ã¬ã¤ã¤ã«å½±é¿ãä¸ãããã¨ã¯é¿ãããã®ã§ã
DIPãå©ç¨ãã
ãã¡ã¤ã³ã¬ã¤ã¤ã«ç½®ããã¦ãã interface ã¯ç¹å®ã®æè¡ã«ä¾åããªãããã«æ°ãã¤ãã¦ããã
å
·ä½çã«ã¯ã
以ä¸ã®ããã« interface ã®å¼æ°ã RDB ãªã©ã®ç¹å®æè¡ã«ä¾åãã¦ããã¨ã
éæ¥çã«ã¤ã³ãã©ã¬ã¤ã¤ã®ä¿®æ£ã«å¼ãã¥ããããã¨ã«ãªãã®ã§ã
package domain type UserRepository interface { // DBConnection 㯠RDB ã«ä¾åãã¦ãã GetByID(conn DBConnection, id int) (User, error) }
以ä¸ã®ããã«ä¾åããªãããã«ãã¦ããã
package domain type UserRepository interface { GetByID(id int) (User, error) }
ãã®å ´åãDBConnection 㯠interface ãå®è£
ãã struct ãæã¤ãã¨ã«ãªãã
package infra type UserRepository struct { conn DBConnection } func (u *UserRepository) GetByID(id int) (User, error) { }
ã¾ã¨ã
ä»åã¯ã¤ã³ãã©ã¬ã¤ã¤ã®å·®ãæ¿ãã®ä¸ã§ä¸çªé¢åã§ãããããã¼ã¿ã½ã¼ã¹å®è£ ã®å·®ãæ¿ãã«ã¤ãã¦èããã
ãã©ã³ã¶ã¯ã·ã§ã³ãèæ ®ããå¿ è¦ãããã®ã§ãé¢åããªã¼ã¨ã
ãã¼ã¿ã½ã¼ã¹å®è£
ã®å·®ãæ¿ã以å¤ã®å·®ãæ¿ãã¯ã
ã¢ããªã±ã¼ã·ã§ã³ã¬ã¤ã¤ã«å½±é¿ããªãå®è£
ã«ãããã¨ãå¯è½ãªã±ã¼ã¹ãå¤ãã¨æãã
ex. æå·åãã¡ã¼ã«éä¿¡
é·ã
ã¨æ¸ãããã
ãã¼ã¿ã½ã¼ã¹å®è£
ã®å·®ãæ¿ãã¯è²ã
é¢åãªã®ã§ã
DIP ã§ãã¡ã¤ã³ã¬ã¤ã¤ã ãå®ã£ã¦ããã°ãã¼ããªã¨æã£ã¦ããã