dimanche 23 juillet 2017

SRP in methods that represent SQL queries

I am working on an app that access a db and now I was wondering what is the best practice here.

Maybe is a stupid question, but then I want to make sure it is stupid:P

I have methods representing queries like this:

Public Function GetCellVposOfTransmitter(transmitter As VPos) As VPos
    Return (From position In Dc.VPos
            Join article In Dc.Art
            On position.ArtID Equals article.ArtID
            Where position.VID = transmitter.VID And position.Pos = transmitter.Pos And article.WGrp = 52 
            Select position).FirstOrDefault()
End Function

Public Function GetPressureTransmittersFromProcessId(processId As Integer) As IQueryable(Of VPos)
    Return From position In Dc.VPos
           Join article In Dc.Art
           On position.ArtID Equals article.ArtID
           Where position.SPos = 0 And position.VID = processId And article.WGrp = 33 
           Select position
End Function

In this case I call GetPressureTransmittersFromProcessId first to get a collection of devices associated to a given process and then for every returned item I call GetCellVposOfTransmitter to get the measuring cell built in each device.

Now every method has only one responsability and is so simple as possible, BUT I query twice the database, and in this case more, since I loop trough the collection and query each time.

I could write a method with a query that returns e.g. a Dictionary(Of Vpos,Vpos) that contains every device as key and the associated cell as value. So I will query only once, but I am breaking (or I am not?) the SRP...

What is the best-practice here? I mean, what has priority here, code or server performance? My app will not be overload any server (lol), but is just to know.

Aucun commentaire:

Enregistrer un commentaire