Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database access in GUI thread , bad isn't it?

I'm working through some MSDN examples, and some books on ADO.Net. What they all have in common is using the point/click/drag-drop design time feature in Visual Studio to develop database applications, binding data sets to controls etc.

And the resulting code does all DB access in the GUI thread, as far as I can tell. This sounds like bad practice, and annoying behavior of the application of the database access once queries starts to take time.

Am I missing something ? Is this how we're supposed to develop database applications ? I usually woudn't think twice about placing network IO in a seperate thread, always.

like image 520
nos Avatar asked Jan 23 '23 11:01

nos


2 Answers

Yes, it's a bad idea unless you really don't care about having the GUI hang. In some cases, particularly for "quick and dirty" tools, that's actually the right choice. If it means you can get something to a user who's just going to use it for a couple of days anyway and cares about getting the job done soon rather than always having a responsive UI, then there's little point wasting time hopping between threads.

But no, it's not how you're meant to develop database applications which are meant to stay responsive.

However, I can understand why books and tutorials do it - at least to some extent. If they're trying to teach you database access rather than threading, it means that a greater proportion of the code is going to be relevant to the subject matter than if everything was absolutely "production code". I know from experience that it's tough to keep "teaching code" as clean as one would like.

However, I think it would be a good idea for such tutorials and books to explain this right up front, so they don't lead to bad habits.

like image 146
Jon Skeet Avatar answered Jan 26 '23 02:01

Jon Skeet


Note that the main problem with this practice has nothing to do with threading, but with separation of concerns. By binding database access so tightly to the UI, they lose the possibilities available by separation, and will require DB changes when the UI changes and vice versa.

Whether it matters to be in the same thread or not will depend on the circumstances, platform, etc.

like image 30
John Saunders Avatar answered Jan 26 '23 01:01

John Saunders