--- /dev/null
+MIT License
+
+Copyright (c) 2021 Evgenii Akentev
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
--- /dev/null
+module Main where
+
+import qualified SuperWeatherProvider
+import qualified WeatherProvider
+import qualified WeatherReporter
+
+-- | This is an actual application where we use
+-- our concrete implementation of `WeatherProvider`.
+main :: IO ()
+main = do
+ let wph = SuperWeatherProvider.new
+ let wrh = WeatherReporter.new wph
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wrh
+ putStrLn weatherReportInLondon
--- /dev/null
+import Distribution.Simple
+main = defaultMain
--- /dev/null
+{-# LANGUAGE TypeApplications #-}
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE RankNTypes #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE GADTs #-}
+{-# LANGUAGE AllowAmbiguousTypes #-}
+
+module HandleRec where
+
+import Data.Vinyl
+
+type HandleRec rs = Rec ElField rs
+
+getMethod
+ :: forall l us v record . (HasField record l us us v v, RecElemFCtx record ElField)
+ => record ElField us -> v
+getMethod = getField . rgetf (Label @l)
--- /dev/null
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module WeatherProvider where
+
+import HandleRec
+
+type Temperature = Int
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+type Handle = HandleRec
+ '[ '("getWeatherData", (Location -> Day -> IO WeatherData))
+ ]
--- /dev/null
+{-# LANGUAGE TypeApplications #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE DataKinds #-}
+
+module WeatherReporter where
+
+import HandleRec
+import Data.Vinyl as V
+
+import qualified WeatherProvider
+
+type WeatherReport = String
+
+-- | We hide dependencies in the handle
+data Handle = Handle { weatherProvider :: WeatherProvider.Handle }
+
+-- | Constructor for Handle
+new :: WeatherProvider.Handle -> Handle
+new = Handle
+
+-- | Domain logic. Usually some pure code that might use mtl, free monads, etc.
+createWeatherReport :: WeatherProvider.WeatherData -> WeatherReport
+createWeatherReport (WeatherProvider.WeatherData temp) =
+ "The current temperature in London is " ++ (show temp)
+
+-- | Domain logic that uses external dependency to get data and process it.
+getCurrentWeatherReportInLondon :: Handle -> IO WeatherReport
+getCurrentWeatherReportInLondon (Handle wph) = do
+ weatherData <- (getMethod @"getWeatherData" wph) "London" "now"
+ return $ createWeatherReport weatherData
--- /dev/null
+module SuperWeatherProvider where
+
+import Data.Vinyl
+import WeatherProvider
+
+new :: Handle
+new = Field getSuperWeatherData
+ :& RNil
+
+-- | This is some concrete implementation `WeatherProvider` interface
+getSuperWeatherData :: Location -> Day -> IO WeatherData
+getSuperWeatherData _ _ = return $ WeatherData 30
--- /dev/null
+module TestWeatherProvider where
+
+import Data.Vinyl
+import WeatherProvider
+
+-- | This is a configuration that allows to setup the provider for tests.
+data Config = Config
+ { initTemperature :: Temperature
+ }
+
+new :: Config -> Handle
+new config = Field (getTestWeatherData $ initTemperature config)
+ :& RNil
+
+-- | This is an implementation `WeatherProvider` interface for tests
+getTestWeatherData :: Int -> Location -> Day -> IO WeatherData
+getTestWeatherData temp _ _ = return $ WeatherData temp
--- /dev/null
+import Test.Hspec
+
+import qualified TestWeatherProvider
+import qualified WeatherProvider
+import qualified WeatherReporter
+
+main :: IO ()
+main = hspec spec
+
+weatherWithTemp :: WeatherProvider.Temperature -> WeatherReporter.Handle
+weatherWithTemp = WeatherReporter.new
+ . TestWeatherProvider.new
+ . TestWeatherProvider.Config
+
+spec :: Spec
+spec = describe "WeatherReporter" $ do
+ it "weather in London is 0" $ do
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
+ weatherWithTemp 0
+ weatherReportInLondon `shouldBe` "The current temperature in London is 0"
+ it "weather in London is -5" $ do
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
+ weatherWithTemp (-5)
+ weatherReportInLondon `shouldBe` "The current temperature in London is -5"
--- /dev/null
+cabal-version: >=2
+name: vinyl-handle
+version: 0.1.0.0
+license-file: LICENSE
+author: Evgenii Akentev
+maintainer: i@ak3n.com
+build-type: Simple
+extra-source-files: CHANGELOG.md
+
+library domain
+ hs-source-dirs: domain
+ exposed-modules: WeatherProvider
+ , WeatherReporter
+ , HandleRec
+ default-language: Haskell2010
+ build-depends: base, vinyl
+
+library impl
+ hs-source-dirs: impl
+ exposed-modules: SuperWeatherProvider
+ default-language: Haskell2010
+ build-depends: base, domain, vinyl
+
+library test-impl
+ hs-source-dirs: test-impl
+ exposed-modules: TestWeatherProvider
+ default-language: Haskell2010
+ build-depends: base, domain, vinyl
+
+executable main
+ main-is: Main.hs
+ build-depends: base >=4.13 && <4.14
+ , domain
+ , impl
+ default-language: Haskell2010
+
+test-suite spec
+ type: exitcode-stdio-1.0
+ hs-source-dirs: test
+ main-is: Test.hs
+ default-language: Haskell2010
+ build-depends: base >= 4.7 && < 5
+ , QuickCheck
+ , hspec
+ , domain
+ , test-impl