由于循环引用,确定如何对 F# 类型进行排序时出现问题 [英] Problem determining how to order F# types due to circular references

查看:15
本文介绍了由于循环引用,确定如何对 F# 类型进行排序时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些扩展通用类型的类型,这些是我的模型.

I have some types that extend a common type, and these are my models.

然后我为 CRUD 操作的每个模型类型设置了 DAO 类型.

I then have DAO types for each model type for CRUD operations.

我现在需要一个函数,它可以让我找到给定任何模型类型的 id,所以我为一些杂项函数创建了一个新类型.

I now have a need for a function that will allow me to find an id given any model type, so I created a new type for some miscellaneous functions.

问题是我不知道如何订购这些类型.目前我在 dao 之前有模型,但我在 CityDAO 之前需要 DAOMiscDAOMisc 之前需要 CityDAO,这是'可能.

The problem is that I don't know how to order these types. Currently I have models before dao, but I somehow need DAOMisc before CityDAO and CityDAO before DAOMisc, which isn't possible.

简单的方法是把这个函数放在每个 DAO 中,只引用它之前的类型,所以,StateCity 之前作为 >StateCity 有外键关系,所以杂项功能会很短.但是,这让我觉得是错误的,所以我不确定如何最好地解决这个问题.

The simple approach would be to put this function in each DAO, referring to just the types that can come before it, so, State comes before City as State has a foreign key relationship with City, so the miscellaneous function would be very short. But, this just strikes me as wrong, so I am not certain how to best approach this.

这是我的杂项类型,其中 BaseType 是我所有模型的通用类型.

Here is my miscellaneous type, where BaseType is a common type for all my models.

type DAOMisc =
    member internal self.FindIdByType item = 
        match(item:BaseType) with
        | :? StateType as i -> 
            let a = (StateDAO()).Retrieve i
            a.Head.Id
        | :? CityType as i -> 
            let a = (CityDAO()).Retrieve i
            a.Head.Id
        | _ -> -1

这是一种 dao 类型.CommonDAO 实际上有 CRUD 操作的代码,但这在这里并不重要.

Here is one dao type. CommonDAO actually has the code for the CRUD operations, but that is not important here.

type CityDAO() =
    inherit CommonDAO<CityType>("city", ["name"; "state_id"], 
        (fun(reader) ->
            [
                while reader.Read() do
                    let s = new CityType()
                    s.Id <- reader.GetInt32 0
                    s.Name <- reader.GetString 1
                    s.StateName <- reader.GetString 3
            ]), list.Empty
    )

这是我的模型类型:

type CityType() =
    inherit BaseType()
    let mutable name = ""
    let mutable stateName = ""
    member this.Name with get() = name and set restnameval=name <- restnameval
    member this.StateName with get() = stateName and set stateidval=stateName <- stateidval
    override this.ToSqlValuesList = [this.Name;]
    override this.ToFKValuesList = [StateType(Name=this.StateName);]

这个 FindIdByType 函数的目的是我想找到一个外键关系的 id,这样我就可以在我的模型中设置值,然后让 CRUD 函数执行所有操作正确的信息.所以,City 需要州名的 id,所以我会得到州名,把它放到 state 类型中,然后调用这个函数来获取那个州的 idstate,所以我的城市插入也将包含外键的 id.

The purpose for this FindIdByType function is that I want to find the id for a foreign key relationship, so I can set the value in my model and then have the CRUD functions do the operations with all the correct information. So, City needs the id for the state name, so I would get the state name, put it into the state type, then call this function to get the id for that state, so my city insert will also include the id for the foreign key.

这似乎是最好的方法,以一种非常通用的方式来处理插入,这是我目前正在努力解决的问题.

This seems to be the best approach, in a very generic way to handle inserts, which is the current problem I am trying to solve.

更新:

我需要研究一下,看看我是否可以在定义了所有其他 DAO 之后以某种方式将 FindIdByType 方法注入 CommonDAO,就好像它是一个闭包一样.如果这是 Java,我会使用 AOP 来获得我正在寻找的功能,不确定如何在 F# 中执行此操作.

I need to research and see if I can somehow inject the FindIdByType method into the CommonDAO after all the other DAOs have been defined, almost as though it is a closure. If this was Java I would use AOP to get the functionality I am looking for, not certain how to do this in F#.

最终更新:

在考虑我的方法后,我意识到它有致命的缺陷,所以我想出了一个不同的方法.

After thinking about my approach I realized it was fatally flawed, so I came up with a different approach.

这就是我将如何进行插入,我决定将这个想法放入每个实体类中,这可能是一个更好的想法.

This is how I will do an insert, and I decided to put this idea into each entity class, which is probably a better idea.

member self.Insert(user:CityType) =
    let fk1 = [(StateDAO().Retrieve ((user.ToFKValuesList.Head :?> StateType), list.Empty)).Head.Id]
    self.Insert (user, fk1)

我还没有开始使用 fklist,但它是 int list 并且我知道每个列的名称,所以我只需要做inner join 用于选择,例如.

I haven't started to use the fklist yet, but it is int list and I know which column name goes with each one, so I just need to do inner join for selects, for example.

这是泛化的基本类型插入:

This is the base type insert that is generalized:

member self.Insert(user:'a, fklist) =
    self.ExecNonQuery (self.BuildUserInsertQuery user)

如果 F# 可以做协方差/反方差就好了,所以我必须解决这个限制.

It would be nice if F# could do co/contra-variance, so I had to work around that limitation.

推荐答案

这个例子与我在函数式编程中所习惯的相去甚远.但是对于相互递归类型的排序问题,有一个标准的解决方案:使用类型参数,制作两级类型.我将在 OCaml(一种相关语言)中给出一个简单的示例.我不知道如何将简单的例子翻译成你正在使用的可怕类型的函数.

This example is very far from what I'm accustomed to in functional programming. But for the problem of ordering mutually recursive types, there is a standard solution: use type parameters and make two-level types. I'll give a simple example in OCaml, a related language. I don't know how to translate the simple example into the scary type functions you are using.

以下是无效的:

type misc = State of string
          | City  of city

type city = { zipcode : int; location : misc }

以下是使用两级类型修复它的方法:

Here's how you fix it with two-level types:

type 'm city' = { zipcode : int; location : 'm }

type misc = State of string
          | City of misc city'
type city = misc city'

这个例子是 OCaml,但也许你可以推广到 F#.希望这可以帮助.

This example is OCaml, but maybe you can generalized to F#. Hope this helps.

这篇关于由于循环引用,确定如何对 F# 类型进行排序时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆