使用monad堆栈进行依赖注入

时间:2017-11-30 17:24:54

标签: haskell dependency-injection separation-of-concerns

trying different approaches做有时被称为依赖注入的事情。为此,我详细阐述了天气应用程序的一个简单示例,我们想要获取天气数据(来自网络服务或硬件设备),存储天气数据(可以是数据库或只是文件),并报告(将其打印到屏幕或说出天气)。我们的想法是编写一个程序,该程序使用一些fetchstorereport函数,这些函数的实现可能会有所不同。

我设法使用functionsfree-monads将问题和摘要从检索,存储和报告的实现中分离出来,但是我使用monad堆栈达到的解决方案看起来很糟糕:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module WeatherReporterMTL where

import           Control.Monad.IO.Class
import           Control.Monad.Trans.Class

type WeatherData = String

class Monad m => WeatherService m where
    fetch :: m WeatherData

class Monad m => Storage m where
    store :: WeatherData -> m ()

class Monad m => Reporter m where
    report :: WeatherData -> m ()

-- | A dummy implementation of the @WeatherService@
newtype DummyService m a = DummyService { runDummyService :: m a }
    deriving (Functor, Applicative, Monad, MonadIO)

instance MonadIO m => WeatherService (DummyService m) where
    fetch = return "won't get any warmer in December."

-- | A dummy implementation of the @Storage@
newtype DummyStorage m a = DummyStorage { runDummyStorage :: m a }
    deriving (Functor, Applicative, Monad, MonadIO, WeatherService)

-- It seems wrong that the storage has to be an instance the weather service
-- (@WeatherService@) ...

instance MonadIO m => Storage (DummyStorage m) where
    store d = liftIO $ putStrLn $ "No room left for this report: " ++ d

-- | A dummy implementation of the @Reporter@
newtype DummyReporter m a = DummyReporter { runDummyReporter :: m a }
    deriving (Functor, Applicative, Monad, MonadIO, WeatherService, Storage)

-- Ok, now this seems even worse: we're putting information about
-- how we're gonna stack our monads :/

instance MonadIO m => Reporter (DummyReporter m) where
    report d = liftIO $ putStrLn $ "Here at the MTL side " ++ d

reportWeather :: (WeatherService m, Storage m, Reporter m) => m ()
reportWeather = do
    w <- fetch
    store w
    report w

dummyWeatherReport :: IO ()
dummyWeatherReport = runDummyService $ runDummyStorage $ runDummyReporter reportWeather

在上面的代码中,DummyStorageDummyReporter都必须包含WeatherService的简单实例,这看起来显然是错误的。而且,这些实例取决于最终堆叠monad的顺序。有没有办法避免不同堆栈之间的信息泄露?

1 个答案:

答案 0 :(得分:3)

不是将实现绑定到特定的新类型,也许你可以自由浮动&#34;实现函数需要访问IO和某些必要的簿记状态,如

data WeatherState = WeatherState -- dummy
fetch' :: (MonadState WeatherState m,MonadIO m) => m WeatherData
fetch' = undefined 
data StorageState = StorageState -- dummy
store' :: (MonadState StorageState m,MonadIO m) => WeatherData -> m ()
store' = undefined 
data ReporterState = ReporterState -- dummy
report' :: (MonadState ReporterState m,MonadIO m) => WeatherData -> m ()
report' = undefined

&#34;将&#34;意味着在StateT上创建一些带有所需状态的新类型,然后声明像

这样的实例
newtype Injected a = 
    Injected { getInjected :: StateT (WeatherState,StorageState,ReportState) a } 
    deriving (Functor,Applicative,Monad)

instance WeatherService Injected where
    fetch = Injected $ zoom _1 fetch'

instance Storage Injected where
    store x = Injected $ zoom _2 $ store' x

instance Reporter Injected where
    report x = Injected $ zoom _3 $ report' x

_1来自 microlens microlens-mtl 来自zoom。)

相关问题