IEnumerable : cé a dhiúscraíonn cad agus cathain - An bhfuair mé é i gceart?

Seo cás hipitéiseach.

Tá líon an-mhór ainmneacha úsáideora agam (10,000,000,000,000,000,000,000 abair. Sea, táimid san aois idir-slaodach :)). Tá a bhunachar sonraí féin ag gach úsáideoir. Ní mór dom dul tríd liosta na n-úsáideoirí agus roinnt SQL a fhorghníomhú i gcoinne gach ceann de na bunachair sonraí agus na torthaí a phriontáil.

Mar gheall ar fhoghlaim mé maitheas an chláir fheidhmigh agus toisc go ndéileálaim le líon chomh mór úsáideoirí, socraíonn mé a chur i bhfeidhm seo ag úsáid seichimh F # agus íon, (aka IEnumerable). Agus seo liom.

// gets the list of user names
let users() : seq<string> = ...

// maps user name to the SqlConnection
let mapUsersToConnections (users: seq<string>) : seq<SqlConnection> = ...

// executes some sql against the given connection and returns some result
let mapConnectionToResult (conn) : seq<string> = ...

// print the result
let print (result) : unit = ...

// and here is the main program
users()
|> mapUsersToConnections
|> Seq.map mapConnectionToResult
|> Seq.iter print

Álainn? Galánta? Cinnte.

Ach! Cé agus cén pointe a dhiúscraíonn na SqlConnections?

Agus ní dóigh liom gur cheart go mbeadh an freagra mapConnectionToResult ceart , mar tá a fhios aige rud ar bith faoi shaolré an cheangail a thugtar dó. Agus b’fhéidir go n-oibreoidh rudaí nó nach n-oibreoidh siad ag brath ar an gcaoi a gcuirtear mapUsersToConnections i bhfeidhm agus ar fhachtóirí éagsúla eile.

Mar mapUsersToConnections is é an t-aon áit eile a bhfuil rochtain aige ar cheangal caithfidh sé a bheith freagrach chun nasc SQL a dhiúscairt.

In F #, is féidir é seo a dhéanamh mar seo:

// implementation where we return the same connection for each user
let mapUsersToConnections (users) : seq<SqlConnection> = seq {
    use conn = new SqlConnection()
    for u in users do
        yield conn
}


// implementation where we return new connection for each user
let mapUsersToConnections (users) : seq<SqlConnection> = seq {
    for u in users do
        use conn = new SqlConnection()
        yield conn
}

Is é a bheadh ​​sa chomhionannas C #:

// C# -- same connection for all users
IEnumerable<SqlConnection> mapUsersToConnections(IEnumerable<string> users)
{
    using (var conn = new SqlConnection())
    foreach (var u in users)
    {
        yield return conn;
    }
}

// C# -- new connection for each users
IEnumerable<SqlConnection> mapUsersToConnections(IEnumerable<string> user)
{
    foreach (var u in users)
    using (var conn = new SqlConnection())
    {
        yield return conn;
    }
}

Tugann na tástálacha a rinne mé le tuiscint go ndéantar na rudaí a dhiúscairt i gceart ag pointí cearta, fiú má dhéantar iad a fhorghníomhú stuif i gcomhthreo: uair amháin ag deireadh an atrátha iomláin don nasc roinnte; agus tar éis gach timthrialla atráchta don nasc neamh-roinnte.

Mar sin, AN CEIST: An bhfuair mé é seo i gceart?

EDIT :

  1. Chuir roinnt freagraí in iúl go cineálta roinnt earráidí sa chód , agus rinne mé roinnt ceartúcháin. Tá an sampla iomlán oibre a thiomsaíonn thíos.

  2. Is chun críocha amháin atá úsáid SqlConnection, mar shampla, tá sé in-aitheanta i ndáiríre.


Sampla a thiomsaíonn

open System

// Stand-in for SqlConnection
type SimpeDisposable() =
    member this.getResults() = "Hello"
    interface IDisposable with
        member this.Dispose() = printfn "Disposing"

// Alias SqlConnection to our dummy
type SqlConnection = SimpeDisposable

// gets the list of user names
let users() : seq<string> = seq {
    for i = 0 to 100 do yield i.ToString()
}

// maps user names to the SqlConnections
// this one uses one shared connection for each user
let mapUsersToConnections (users: seq<string>) : seq<SqlConnection> = seq {
    use c = new SimpeDisposable()
    for u in users do
        yield c
}

// maps user names to the SqlConnections
// this one uses new connection per each user
let mapUsersToConnections2 (users: seq<string>) : seq<SqlConnection> = seq {
    for u in users do
        use c = new SimpeDisposable()
        yield c
}

// executes some "sql" against the given connection and returns some result
let mapConnectionToResult (conn:SqlConnection) : string = conn.getResults()

// print the result
let print (result) : unit = printfn "%A" result

// and here is the main program - using shared connection
printfn "Using shared connection"
users()
|> mapUsersToConnections
|> Seq.map mapConnectionToResult
|> Seq.iter print


// and here is the main program - using individual connections
printfn "Using individual connection"
users()
|> mapUsersToConnections2
|> Seq.map mapConnectionToResult
|> Seq.iter print

Is iad na torthaí:

Ceangal roinnte: "Dia dhuit" "Dia dhuit" ... "Diúscairt"

Naisc aonair: "Dia dhuit" "Diúscairt" "Dia dhuit" "Diúscairt"

8
задан Philip P. 21 July 2011 в 17:18
поделиться