Problem determining how to order F# types due to circular references
Posted
by James Black
on Stack Overflow
See other posts from Stack Overflow
or by James Black
Published on 2010-05-18T01:55:06Z
Indexed on
2010/05/18
2:01 UTC
Read the original article
Hit count: 286
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.
© Stack Overflow or respective owner