Skip to content

Instantly share code, notes, and snippets.

@tjaskula
Last active August 29, 2015 13:56
Show Gist options
  • Save tjaskula/9019486 to your computer and use it in GitHub Desktop.
Save tjaskula/9019486 to your computer and use it in GitHub Desktop.
Is this considered a bad practice, using a hard dependency inside a function. Something like ServiceLocator anti pattern in OOP ?
// And here is how both of them are composed in a higher level module so I can pass in another function without hard dependency for testing
let keys = [| "SOclient_id"; "SOscope"; "SOredirect_uri"|]
let queryParameters = readConfigApi keys readConfigurationValue
// tried to isolate a hard dependency in different function
let readConfigurationValue keyValue =
(keyValue, RocketCvRuntimeContext.Instance.ConfigurationManager.GetConfigurationValue(keyValue))
// here is the high level function using indirectly the first one
let readConfigApi keys configValueReader =
keys |> Seq.map (fun e ->
e |> configValueReader
)
|> Map.ofSeq
@tjaskula
Copy link
Author

And if you have some tips how a functional programer would write this it would be great

@7sharp9
Copy link

7sharp9 commented Feb 15, 2014

You don't need the lambda for a start:

let readConfigApi keys configValueReader =
        keys |> Seq.map configValueReader |> Map.ofSeq

@tjaskula
Copy link
Author

Thanks for the tips.

Just wanted to say that this dependency is coming from a C# part :
RocketCvRuntimeContext.Instance.ConfigurationManager.GetConfigurationValue(keyValue)

Is there any recommendation on how to deal with such a code ? When you have to use some pieces of the code made for different purposes ?

@7sharp9
Copy link

7sharp9 commented Feb 17, 2014

You could add a function that had the same signature to the readConfigurationValue function and pass in the RocketCvRuntimeContext.Instance.ConfigurationManager.GetConfigurationValue function in. Then you could abstract to another configuration provider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment