, . , , - async (, ASP.NET MVC ActionFilters), . , tryGetServer string -> MongoServer option. , :
type Service<'a, 'b> = 'a -> 'b option
, :
type Filter<'a, 'b> = 'a -> Service<'a, 'b> -> 'b option
- , 'a a Service<'a, 'b>, , Service<'a, 'b>. - , 'a, , , . , .
let loggingFilter (connStr:string) (tryGetServer:string -> MongoServer option) : Filter<string, MongoServer option> =
let server = tryGetServer connStr
match tryGetServer connStr with
| Some _ ->
logger.Information "Successfully connected to the database server."
server
| None ->
logger.Information "Unable to connect to the database server."
server
, :
type Continuation<'a,'r> = ('a -> 'r) -> 'r
module Continuation =
let bind (m:Continuation<'a, 'r>) k c = m (fun a -> k a c)
module Filter =
/// Composes two filters into one which calls the first one, then the second one.
let andThen (f2:Filter<_,,_>) (f1:Filter<_,_>) : Filter<_,_> = fun input -> Continuation.bind (f1 input) f2
/// Applies a filter to a service returning a filtered service.
let apply (service:Service<_,_>) (filter:Filter<_,_>) : Service<_,_> = fun input -> filter input service
/// The identity filter which passes the input directly to the service and propagates the output.
let identity : Filter<_,_> = fun (input:'Input) (service:Service<_,_>) -> service input
, :
let tryGetServerLogable = Filter.apply tryGetServer loggingFilter
? , . , , , , Filter.andThen. , , - .
- . - , .