因為唔需要, IoC 係為咗解決OO 上嘅dependency 問題, 例如:
Class Dao {
private Pool pool;
Dao (Config cfg) {
this.pool = new Pool(cfg);
}
getEntityA() {
....
}
}
咁萬一底層嘅constructor 有變動:
Pool (Config cfg, int size, Other other) {
...
}
咁上層嘅object 都要跟住做修改, 分分鐘要改n層嘅野
所以相對上寫成下面咁會易啲maintenance:
Class Dao {
private Pool pool;
Dao (Pool pool) {
this.pool = pool;
}
}
而個pool instance就會由 IoC container 去管理
喺fp 之下, 會寫成:
getEntityA :: Pool -> IO (Either SomeException EntityA)
又或者:
getEntityA :: (HasPool m, MonadIO m, MonadThrow m) => m EntityA
咁喺FP 下, 又會點樣寫IoC?