由于循环引用而确定如何订购F#类型的问题 [英] Problem determining how to order F# types due to circular references

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

问题描述

我有一些扩展常见类型的类型,这些是我的模型。



然后,我为每个CRUD操作的模型类型都有DAO类型。 >

现在我需要一个函数来让我找到任何模型类型的id,所以我为一些其他函数创建了一个新类型。



问题是我不知道如何订购这些类型。目前我有dao之前的模型,但是我在某种程度上需要 DAOMisc CityDAO CityDAO > DAOMisc 之前,这是不可能的。

简单的方法是将这个函数放在每个DAO中,仅指可能出现的类型,所以 State 作为州与城市具有外键关系时,code>出现在城市 / code>,所以辅助功能会很短。但是,这只是我的错误,所以我不确定如何更好地处理这个问题。



这是我的杂项类型,其中 BaseType 是我所有型号的常见类型。

 类型DAOMisc = 
成员内部self.FindIdByType item =
match(item:BaseType)with
| :? StateType as i - >
let a =(StateDAO())。检索我
a.Head.Id
| :? CityType as i - >
let a =(CityDAO())。检索我
a.Head.Id
| _ - > -1

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

  type CityDAO()= 
继承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 =
成员this.Name with get()= name and set restnameval = name< - restnameval
成员this .StateName与get()= stateName和set stateidval = stateName< - stateidval
覆盖this.ToSqlValuesLis t = [this.Name;]
覆盖this.ToFKValuesList = [StateType(Name = this.StateName);]

这个 FindIdByType 函数的用途是我想找到一个外键关系的id,所以我可以在我的模型中设置这个值并且然后让CRUD功能使用所有正确的信息进行操作。所以, City 需要这个州名的id,所以我会得到州名,放到州类型,然后调用这个函数来获得该状态的ID,所以我的城市插入也将包括外键的ID。



这似乎是最好的方法,以一种非常通用的方式来处理插入,这是我正试图解决的当前问题。



更新:



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



最终更新:

在考虑了我的方法之后,我意识到这是致命的缺陷,所以我想出了一个不同的方法。



这是我将如何做插入,我决定把这个想法放到每个实体类中,这可能是一个更好的主意。

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

我还没有开始使用 fklist 但是它是 int list ,我知道哪个列名与每个列名一致,所以我只需要例如为选择做内部连接



这是一般化的基本类型插入: p>

 成员self.Insert(user:'a,fklis t)= 
self.ExecNonQuery(self.BuildUserInsertQuery user)

F#可以做co / contra-variance,所以我不得不解决这个限制。 解决方案

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



以下是不起作用的:

  type misc =字符串
|的状态City of city

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

以下是如何使用两种类型修复它的方法:

 输入'm city'= {zipcode:int;位置:'m} 

类型misc =字符串
|的状态城市misc城市'
类型城市= misc城市'

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

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

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

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.

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.

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.

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

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
    )

This is my model type:

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);]

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.

UPDATE:

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#.

Final Update:

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)

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)

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

解决方案

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.

Here's what doesn't work:

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'

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

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

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