如何使用 F# 中的单例模式更新具有记录成员的类

huangapple go评论51阅读模式
英文:

How to update a class with a record member using the Singleton Pattern in F#

问题

这是翻译好的部分:

假设我有以下的记录定义:

type WindowServices =
    {
        ReportMenu: Window option 
        ProgressNotes: Window option
        PatientRegistration: Window option
    }

此外,我有这个类,在其中我试图实现单例模式:

type WindowServiceManager private(wss) =  
    let mutable ws:WindowServices = wss  
    member this.WS 
        with get () = ws
        and set (value) = ws <- value
    
    static member val Instance = 
            let wss = {ReportMenu = None; ProgressNotes = None; PatientRegistration = None}    
            WindowServiceManager(wss)

在类外部,单例 "Instance" 似乎可以通过以下方式正确获得:

let i = WindowServiceManager.Instance

然而,无论我尝试了什么,都不能改变默认记录值。也就是说,以下操作失败。在 WS 中从未进入新值:

let windowServices : WindowServices = 
              { ReportMenu = Some reportMenu; 
                ProgressNotes = Some progressNoteWindow; 
                PatientRegistration = Some patientRegistrationWindow }
           
i.WS <- windowServices

如何完成这个操作?

TIA

附录:以下显示了失败。"failwith" 总是被触发:

let getReportMenuService : Window = 
    let i = WindowServiceManager.Instance
    let p = match i.WS.ReportMenu with
        | None -> failwith "[ReportMenuService] 出了点问题!"
        | Some s -> s
    p
英文:

Lets say I have this record definition:

type WindowServices =
    {
        ReportMenu: Window option 
        ProgressNotes: Window option
        PatientRegistration: Window option
    }

Furthermore, I have this class in which I am attempting to implement the Singleton pattern:

type WindowServiceManager private(wss) =  
    let mutable ws:WindowServices = wss  
    member this.WS 
        with get () = ws
        and set (value) = ws &lt;- value
    
    static member val Instance = 
            let wss = {ReportMenu = None; ProgressNotes = None; PatientRegistration = None}    
            WindowServiceManager(wss)

Outside of the class, the singleton "Instance" seems to correctly be obtained with:

let i = WindowServiceManager.Instance

However, absolutely nothing I have tried can change the default record values. That is, this fails. The new values in "windowServices" are never entered into WS:

let windowServices : WindowServices = 
              { ReportMenu = Some  reportMenu; 
                ProgressNotes = Some progressNoteWindow; 
                PatientRegistration = Some patientRegistrationWindow }
           
i.WS &lt;- windowServices

How is this done?

TIA

Addendum: The following shows failure. "failwith" is ALWAYS hit:

let getReportMenuService : Window = 
    let i = WindowServiceManager.Instance
    let p = match i.WS.ReportMenu with
        | None -&gt; failwith &quot;[ReportMenuService] Something went wrong!&quot;
        | Some s -&gt; s
    p

答案1

得分: 1

以下是您的代码的翻译部分:

我认为在您的代码的其他地方可能发生了一些事情,您还没有与我们分享。您提供的代码按预期工作(使用Window作为string的别名以简化):

type Window = string

type WindowServices =
    {
        ReportMenu: Window option 
        ProgressNotes: Window option
        PatientRegistration: Window option
    }

type WindowServiceManager private(wss) =  
    let mutable ws:WindowServices = wss  
    member this.WS 
        with get () = ws
        and set (value) = ws &lt;- value
    
    static member val Instance = 
            let wss = {ReportMenu = None; ProgressNotes = None; PatientRegistration = None}    
            WindowServiceManager(wss)

let i = WindowServiceManager.Instance

let windowServices : WindowServices = 
              { ReportMenu = Some &quot;ReportMenu&quot;;
                ProgressNotes = Some &quot;ProgressNotes&quot; ;
                PatientRegistration = Some &quot;PatientRegistration&quot; }
           
i.WS &lt;- windowServices

let getReportMenuService : Window = 
    let i = WindowServiceManager.Instance
    let p =
        match i.WS.ReportMenu with
        | None -&gt; failwith &quot;[ReportMenuService] Something went wrong!&quot;
        | Some s -&gt; s
    p

getReportMenuService
    |&gt; printfn &quot;%A&quot;   // &quot;ReportMenu&quot;

希望这对您有帮助。如果您需要进一步的帮助,请随时提问。

英文:

I think there's something going on elsewhere in your code that you haven't yet shared with us. The code you've provided works as expected (using Window as an alias for string for simplicity):

type Window = string

type WindowServices =
    {
        ReportMenu: Window option 
        ProgressNotes: Window option
        PatientRegistration: Window option
    }

type WindowServiceManager private(wss) =  
    let mutable ws:WindowServices = wss  
    member this.WS 
        with get () = ws
        and set (value) = ws &lt;- value
    
    static member val Instance = 
            let wss = {ReportMenu = None; ProgressNotes = None; PatientRegistration = None}    
            WindowServiceManager(wss)

let i = WindowServiceManager.Instance

let windowServices : WindowServices = 
              { ReportMenu = Some &quot;ReportMenu&quot;;
                ProgressNotes = Some &quot;ProgressNotes&quot; ;
                PatientRegistration = Some &quot;PatientRegistration&quot; }
           
i.WS &lt;- windowServices

let getReportMenuService : Window = 
    let i = WindowServiceManager.Instance
    let p =
        match i.WS.ReportMenu with
        | None -&gt; failwith &quot;[ReportMenuService] Something went wrong!&quot;
        | Some s -&gt; s
    p

getReportMenuService
    |&gt; printfn &quot;%A&quot;   // &quot;ReportMenu&quot;

huangapple
  • 本文由 发表于 2023年5月18日 09:59:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定