我正在使用 Suave 构建经过身份验证的 Web API,我经常偶然发现在不同函数中聚合信息的问题
pathScan Navigation.playersAvailables GetGame >>= getInSession >>= (fun (gameId,playerInSession) -> //access to both gameId and player in session)
签名:
getGame : HttpContext -> Async<HttpContext option>
getInSession : HttpContext -> Async<HttpContext option>
- getGame 从 httpContext.request.querystring getInSession 获取 id
- 从 httpContext.cookie 获取 sessionId
为了做到这一点,我发现的唯一一件事就是将信息存储在 userDataDictionnary 中:
Writers.setUserData "player" { playerId= playersId; socialId=socialId; username = username}
并在其他函数中检索它,但对我来说看起来很讨厌:
let player = x.userState.["player"] :?> PlayerSession
//do some other stuff now that we have the current player
还有另一种方法吗?我想拥有像 getGameId 和 get Session 等纯函数,并且能够按照我希望处理我的不同路线来组合它们:
pathScan Navigation.playersAvailables GetGame >>= getInSession >>= (fun (gameId,playerInSession) -> //access to both gameId and player in session)
pathScan Navigation.otherRoute GetGame >>= (fun (gameId) -> //process gameId)
pathScan Navigation.otherRoute2 getInSession >>= (fun (sessionId) -> //process sessionId to do some other stuff)
恐怕我需要的是与一些真正的函数式程序员进行一天的谈话..