简体   繁体   English

我应该如何公开在f#中声明的全局词典,该词典将添加来自不同HttpModules的项?

[英]How should I expose a global Dictionary declared in f# that will have items added from different HttpModules?

I have a dictionary (formatters) declared in the following code that will have items added to it inside of multiple HttpModules. 我有一个在以下代码中声明的字典(格式化程序),该字典将在多个HttpModule中添加项。 Once those are loaded it will not be written to again. 一旦加载了这些,就不会再被写入。 What would be the best way to expose this so it can be accessed from any .NET language? 公开此内容以便可以从任何.NET语言访问的最佳方法是什么? I know this seems lame and looks like I should just have them implement ToString() however part of the application requires strings to be in a certain format and I don't want clients having to implement ToString() in a way that is specific to my application. 我知道这似乎很la脚,看起来我应该让他们实现ToString(),但是应用程序的一部分要求字符串采用某种格式,并且我不希望客户端必须以特定于以下方式的方式实现ToString():我的应用程序。

module MappingFormatters
open System
open System.Collections.Generic

let formatters = new Dictionary<Type, obj -> string>();

let format item =

    let toDateTime (d:DateTime) =
        let mutable date = d;
        if (date.Kind) <> System.DateTimeKind.Utc then
            date <- date.ToUniversalTime()
        date.ToString("yyyy-MM-ddTHH:mm:00Z")

    let stripControlCharacters (str:string) =
        let isControl c = not (Char.IsControl(c))
        System.String( isControl |> Array.filter <| str.ToCharArray())

    let defaultFormat (item:obj) =
        match item with
        | :? string as str-> stripControlCharacters(str)
        | :? DateTime as dte -> toDateTime(dte)
        | _ -> item.ToString()

    let key = item.GetType();
    if formatters.ContainsKey(key) then
        formatters.Item(key) item
    else
        defaultFormat item

If the question is just one about language interoperability, then I think you should just change the type from 如果问题只是关于语言互操作性的问题,那么我认为您应该将类​​型从

Dictionary<Type, obj -> string>

to

Dictionary<Type, Func<obj, string> >

and then you should be in good shape. 然后您应该处于良好状态。

After researching. 经过研究。 I have decided to create a type called MappingFormatters to hold the method for adding the formatter. 我决定创建一个名为MappingFormatters的类型,以容纳添加格式化程序的方法。 The client does not need to call it, but my f# code will. 客户端不需要调用它,但是我的f#代码将调用它。 I believe this will let me use the common f# conventions while exposing a way for other .net languages to inter-operate with the least confusion. 我相信这将使我能够使用常见的f#约定,同时为其他.net语言提供一种互操作性最小的方法。

module File1
open System


let mutable formatters = Map.empty<string, obj -> string>

let format (item:obj) = 

    let dateToString (d:DateTime) =
        let mutable date = d;
        if (date.Kind) <> System.DateTimeKind.Utc then
            date <- date.ToUniversalTime()
        date.ToString("yyyy-MM-ddTHH:mm:00Z")

    let stripCtrlChars (str:string) =
        let isControl c = not (Char.IsControl(c))
        System.String( isControl |> Array.filter <| str.ToCharArray())


    let key = item.GetType().AssemblyQualifiedName
    if Map.containsKey key formatters then
        Map.find key formatters item
    else
        match item with
        | :? DateTime as d -> dateToString d
        | _ -> stripCtrlChars (item.ToString())

let add (typ:Type) (formatter:obj -> string) =
    let contains = Map.containsKey
    let key = typ.AssemblyQualifiedName

    if not (formatters |> contains key) then
        formatters <- Map.add key formatter formatters

type MappingFormatters() = class
    let addLock = new obj()
    member a.Add (``type``:Type, formatter:Func<obj,string>) =
        lock addLock (fun () ->
            add ``type`` (fun x -> formatter.Invoke(x))
        )
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何从服务层方法中公开总记录数和IEnumable分页记录集合? - How should I expose the total record count and IEnumable collection of paged records from my service layer method? 如何从启动自定义HttpModules中排除Webresource.axd - How to exclude Webresource.axd from firing my custom HttpModules 如何安装F#并从ASPX页面使用它 - How to install F# and use it from aspx page 的HttpModules - HttpModules 我们可以在一个 ASP.NET 应用程序中拥有多少个 httphandler 和 httpmodules? - How many httphandlers and httpmodules we can have in one asp.net application? 如何确定HttpModules的执行顺序? - How is the order of execution for HttpModules determined? 在F#中,如何判断对象是否为Async &lt;_&gt;,如何将其转换为Async &lt;_&gt;? - In F#, how do I tell if an object is an Async<_>, and how can I cast it to an Async<_>? 我如何git将基于.fsproj的f#项目部署到azure? - How do I git deploy an .fsproj based f# project to azure? c#f#:WebApi:如何使用HttpRouteCollection执行与RouteCollection.RouteExistingFiles等效的操作? - c# f#: WebApi: How do I do the equivalent of RouteCollection.RouteExistingFiles with HttpRouteCollection? 如何从f#访问.net asp.core库中的静态类 - How to access static classes in a .net asp.core library from f#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM