-# backpack-handle-example
\ No newline at end of file
+# backpack-handle-example
+
+The examples of the Handle pattern in simple case, with records, and backpack.
--- /dev/null
+# Revision history for backpack-handle-pattern
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
--- /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
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wph
+ putStrLn weatherReportInLondon
--- /dev/null
+import Distribution.Simple
+main = defaultMain
--- /dev/null
+cabal-version: >=2
+name: backpack-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
+ signatures: WeatherProvider
+ exposed-modules: WeatherReporter
+ default-language: Haskell2010
+ build-depends: base
+
+library impl
+ hs-source-dirs: impl
+ exposed-modules: SuperWeatherProvider
+ reexported-modules: SuperWeatherProvider as WeatherProvider
+ default-language: Haskell2010
+ build-depends: base
+
+library test-impl
+ hs-source-dirs: test-impl
+ exposed-modules: TestWeatherProvider
+ reexported-modules: TestWeatherProvider as WeatherProvider
+ default-language: Haskell2010
+ build-depends: base
+
+executable backpack-handle-exe
+ main-is: Main.hs
+ build-depends: base >=4.13 && <4.14
+ , impl
+ , domain
+ 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
--- /dev/null
+signature WeatherProvider where
+
+data Temperature
+instance Show Temperature
+
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+data Handle
+
+-- | The interface of `WeatherProvider` with available methods.
+getWeatherData :: Handle -> Location -> Day -> IO WeatherData
--- /dev/null
+module WeatherReporter where
+
+import WeatherProvider
+
+type WeatherReport = String
+
+-- | This is domain logic. It uses `WeatherProvider` to get the actual data.
+getCurrentWeatherReportInLondon :: WeatherProvider.Handle -> IO WeatherReport
+getCurrentWeatherReportInLondon wph = do
+ weatherData <- WeatherProvider.getWeatherData wph "London" "now"
+ return $ "Current temperature in London is " ++ (show $ temperature weatherData)
\ No newline at end of file
--- /dev/null
+module SuperWeatherProvider where
+
+type Temperature = Int
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+data Handle = Handle
+
+new :: Handle
+new = Handle
+
+-- | This is some concrete implementation `WeatherProvider` interface
+getWeatherData :: Handle -> Location -> Day -> IO WeatherData
+getWeatherData _ _ _ = return $ WeatherData 30
--- /dev/null
+module TestWeatherProvider where
+
+type Temperature = Int
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+-- | This is a configuration that allows to setup the provider for tests.
+data Config = Config
+ { initTemperature :: Temperature
+ }
+
+data Handle = Handle
+ { config :: Config
+ }
+
+new :: Config -> Handle
+new = Handle
+
+-- | This is an implementation `WeatherProvider` interface for tests
+getWeatherData :: Handle -> Location -> Day -> IO WeatherData
+getWeatherData (Handle conf) _ _ = return $ WeatherData $ initTemperature conf
--- /dev/null
+import Test.Hspec
+
+import qualified TestWeatherProvider
+import qualified WeatherProvider
+import qualified WeatherReporter
+
+main :: IO ()
+main = hspec spec
+
+weatherWithTemp :: WeatherProvider.Temperature -> WeatherProvider.Handle
+weatherWithTemp = TestWeatherProvider.new . TestWeatherProvider.Config
+
+spec :: Spec
+spec = describe "WeatherReporter" $ do
+ it "weather in London is 0" $ do
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
+ weatherWithTemp 0
+ weatherReportInLondon `shouldBe` "Current temperature in London is 0"
+ it "weather in London is -5" $ do
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
+ weatherWithTemp (-5)
+ weatherReportInLondon `shouldBe` "Current temperature in London is -5"
--- /dev/null
+# Revision history for backpack-handle-pattern
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
--- /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
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wph
+ putStrLn weatherReportInLondon
--- /dev/null
+import Distribution.Simple
+main = defaultMain
--- /dev/null
+module WeatherProvider where
+
+type Temperature = Int
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+-- | The interface of `WeatherProvider` with available methods.
+data Handle = Handle
+ { getWeatherData :: Location -> Day -> IO WeatherData
+ }
--- /dev/null
+module WeatherReporter where
+
+import WeatherProvider
+
+type WeatherReport = String
+
+-- | This is domain logic. It uses `WeatherProvider` to get the actual data.
+getCurrentWeatherReportInLondon :: WeatherProvider.Handle -> IO WeatherReport
+getCurrentWeatherReportInLondon wph = do
+ weatherData <- WeatherProvider.getWeatherData wph "London" "now"
+ return $ "Current temperature in London is " ++ (show $ temperature weatherData)
\ No newline at end of file
--- /dev/null
+module SuperWeatherProvider where
+
+import WeatherProvider
+
+new :: Handle
+new = Handle
+ { getWeatherData = getSuperWeatherData
+ }
+
+-- | This is some concrete implementation `WeatherProvider` interface
+getSuperWeatherData :: Location -> Day -> IO WeatherData
+getSuperWeatherData _ _ = return $ WeatherData 30
\ No newline at end of file
--- /dev/null
+cabal-version: >=2
+name: records-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
+ default-language: Haskell2010
+ build-depends: base
+
+library impl
+ hs-source-dirs: impl
+ exposed-modules: SuperWeatherProvider
+ default-language: Haskell2010
+ build-depends: base
+ , domain
+
+library test-impl
+ hs-source-dirs: test-impl
+ exposed-modules: TestWeatherProvider
+ default-language: Haskell2010
+ build-depends: base
+ , domain
+
+executable records-handle-exe
+ 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
--- /dev/null
+module TestWeatherProvider where
+
+import WeatherProvider
+
+-- | This is a configuration that allows to setup the provider for tests.
+data Config = Config
+ { initTemperature :: Temperature
+ }
+
+new :: Config -> Handle
+new config = Handle
+ { getWeatherData = getTestWeatherData $ initTemperature config
+ }
+
+-- | 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 -> WeatherProvider.Handle
+weatherWithTemp = TestWeatherProvider.new . TestWeatherProvider.Config
+
+spec :: Spec
+spec = describe "WeatherReporter" $ do
+ it "weather in London is 0" $ do
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
+ weatherWithTemp 0
+ weatherReportInLondon `shouldBe` "Current temperature in London is 0"
+ it "weather in London is -5" $ do
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
+ weatherWithTemp (-5)
+ weatherReportInLondon `shouldBe` "Current temperature in London is -5"
--- /dev/null
+# Revision history for backpack-handle-pattern
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
--- /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 WeatherProvider
+import qualified WeatherReporter
+
+main :: IO ()
+main = do
+ let wph = WeatherProvider.new
+ weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wph
+ putStrLn weatherReportInLondon
--- /dev/null
+import Distribution.Simple
+main = defaultMain
--- /dev/null
+module WeatherProvider where
+
+type Temperature = Int
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+-- | Our Handle is empty, but usually other dependencies are stored here
+data Handle = Handle
+
+-- | Constructor for Handle
+new :: Handle
+new = Handle
+
+-- | This is some concrete implementation.
+-- In this example we return a constant value.
+getWeatherData :: Handle -> Location -> Day -> IO WeatherData
+getWeatherData _ _ _ = return $ WeatherData 30
--- /dev/null
+module WeatherReporter where
+
+import WeatherProvider
+
+type WeatherReport = String
+
+-- | Domain logic. Usually some pure code that might use mtl, free monads, etc.
+createWeatherReport :: WeatherData -> WeatherReport
+createWeatherReport (WeatherData temp) =
+ "The current temperature in London is " ++ (show temp)
+
+-- | Domain logic that uses external dependency to get data and process it.
+getCurrentWeatherReportInLondon :: WeatherProvider.Handle -> IO WeatherReport
+getCurrentWeatherReportInLondon wph = do
+ weatherData <- WeatherProvider.getWeatherData wph "London" "now"
+ return $ createWeatherReport weatherData
--- /dev/null
+cabal-version: >=2
+name: simple-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
+ default-language: Haskell2010
+ build-depends: base
+
+executable simple-handle-exe
+ main-is: Main.hs
+ build-depends: base >=4.13 && <4.14
+ , domain
+ default-language: Haskell2010