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.
In order to determine the reaction order, the power-law form of the rate equation is generally used. The expression of this form of the rate law is given by r = k[A]x[B]y.
Problem identification requires the use of an appropriate measure or assessment tool to determine whether a problem (i.e., discrepancy) exists. For example, to determine whether a reading problem exists, an oral reading measure may be used to calculate a student's reading rate and accuracy.
Jumping to solutions (before understanding the problem or its root cause) Jumping to the root cause (without thorough enough analysis and observation)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With