Тёмный

IDisposable Exposed 

Shiv Kumar
Подписаться 14 тыс.
Просмотров 3,3 тыс.
50% 1

Why do we need to Dispose things, when we're in a Managed Runtime? When should be be disposing? How to correctly dispose of things? All these and many more question discussed and answered in this video. We look at a number of complicated scenarios in this video by way of code examples and go about fixing the issues to make all work as it is supposed to.
Source Code for the Completed VS Solution can be found here:
github.com/matlus/IDisposable...
Chapter Markers
00:00 Intro
01:45 What is Managed Code
03:16 Managed Services
04:41 Unmanaged Services
10:02 Introducing the Code for this video
15:06 Cascade Disposables
15:54 The Dispose Pattern
20:02 Why GC.SupressFinalize
21:51 Reason for Why we have IDisposable
27:31 ADO.NET - Connections, Transactions, Commands
30:30 DbConnection - Close vs Dispose
33:19 IDisposable & Close vs Dispose
34:33 Logger Provider also uses Disposable Things
43:26 Background Services and IDisposable/IAsyncDisposable

Опубликовано:

 

21 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 27   
@Hect0rx
@Hect0rx 2 года назад
Great video and topic as always, one of the few channels here on RU-vid that deliver professional in-depth content
@Matlus
@Matlus 2 года назад
Thank you Arghs! Your comment is much appreciated.
@Karen-mj9kk
@Karen-mj9kk Год назад
Best explanation I've found about IDisposable. Thank you!
@suhasinin9145
@suhasinin9145 2 года назад
Great video! Havent seen anybody who has digested the stuff and can explain it so clearly and calmly.
@Matlus
@Matlus 2 года назад
Thank you Suhasini! Glad you liked it and found it useful.
@hansrudolf5849
@hansrudolf5849 2 года назад
Looking forward to that video - let's see if I can learn one or two new things. Thanks for your time Shiv.
@Matlus
@Matlus 2 года назад
Hi Hans, let me know what you think!
@adriangas_
@adriangas_ 2 года назад
In a company that I used to work for, developers loved to use null-conditional operators everywhere 'just in case' and that code was a nightmare to debug, because of those null-conditionals masking the real problem and leaving you with totally unrelated null reference exceptions much later.
@Matlus
@Matlus 2 года назад
Yes, I've heard of situations where the null conditional operator has introduced bugs. Yesterday on one of my teams a dev had used it and I was explaining why she should remove it and that went on to an interesting conversation. I think I'll make video on this topic :)
@obiwanjacobi
@obiwanjacobi 2 года назад
It's good to mention that the AppDomain is only used in the (old/legacy) dotnet Framework. Dotnet Core and .NET5 and .NET6 do not use AppDomains. It's also good to know that the dispose pattern is only there for derived classes to be able to call all the dispose logic of the entire class hierarchy in combination with Finalization. If your class is sealed (as in this example) it cannot be derived from and you do not need to implement the dispose pattern.
@Matlus
@Matlus 2 года назад
Hi Marc, good point about sealed classes and the dispose pattern, I should have made mention of it, true. However, I implement the pattern in sealed classes nonetheless. As regards AppDomains, they're still there. You can't create (more) app domains like you could in earlier versions. At least that's how I understand it.
@schwamm1996kopf
@schwamm1996kopf 2 года назад
Thank you for the video, really well explained. I have a question: Does it make sense to use IDisposable on Singletons? I thought Singleton Instances live for the lifetime of the whole application and after the application shuts down, the os would free up the resources. So what's the reason to use IDisposable on Singletons?
@SBDavin
@SBDavin 2 года назад
I commend you for taking on this fascinating topic! I always thought finalizers should be added to our disposable classes, with the hope they will not be used. For example, if a programmer uses our class but mistakenly forgets to call Dispose(), then the instance remains on the freachable queue where eventually threadpool threads execute the queued finalizer(s) to properly dispose our object(s) and then removes the root reference to our instance so it can eventually be garbage collected. As you said, that's why we need SuppressFinalize(), so that finalization can be skipped and memory used by the object can be recompacted much sooner. IMHO, disassembling the Close() and Dispose() methods of the used data provider connection object is recommended to see what is really happening under the covers. Many times Dispose() calls Close() for us automatically. BTW - there are some real garbage collection nerds out, even GC publications and conferences. I'm still a novice but I'm fascinated with the science. Digging into memory pressure and generation counts could be a whole other video. Thank you, Shiv, for the great video!
@Matlus
@Matlus 2 года назад
Hi Davin, >I always thought finalizers should be added to our disposable classes, with the hope they will not be used. That could be a viable strategy, though I've never used it (or seen it used) myself. So do you call the private Dispose(bool disposing) method passing in false as the argument from your finalizer? Yes, Dispose calls Close. I guess the point was really trying to make was that they do different things (close releases the connection back to the pool). In my actual code on GitHub, I don't actually call close. I simply dispose.
@SBDavin
@SBDavin 2 года назад
@@Matlus Yes - the finalizer typically passes false to Dispose() to release the unmanaged resources (file handles, window handles, libraries, etc.) Thank you, sir.
@Matlus
@Matlus 2 года назад
Cool!
@akimbbo_upnext
@akimbbo_upnext 2 года назад
I like how you pay attention to this nullability detail at 18:30. In my project that i work on people are overusing this operator just to supress warnings:(
@Matlus
@Matlus 2 года назад
Thanks for writing in and confirming that this sort of thing happens a lot.
@MrJonnis13
@MrJonnis13 2 года назад
Welcome Back Shiv :) Can you please clarify the "AppDomain" difference between a console application and a Asp.Net web application ? How and why in the latter case, my code is running in multiple AppDomains ?
@Matlus
@Matlus 2 года назад
Thank you Johnny! ASP.NET applications run in another process (a worker process called W3p.exe) and generally, for a given "Web Application" you'd configure your web application to use multiple worker processes to handle the workload. Typically one per the number of CPU cores available on the server. Thus your application is running in multiple app domains (one per worker process). Hopefully that makes sense?
@MrJonnis13
@MrJonnis13 2 года назад
@@Matlus That's making it crystal clear! Thank you Shiv
@glaysonpatricio
@glaysonpatricio 2 года назад
Tks...
@Matlus
@Matlus 2 года назад
You're welcome Glayson.
@matthewparker7789
@matthewparker7789 11 месяцев назад
Great video: only 13k subs ? :-(
Далее
C# 9 Record Types
40:23
Просмотров 3,1 тыс.
У тебя проблемы?
00:20
Просмотров 595 тыс.
The Dispose Pattern
16:28
Просмотров 10 тыс.
Unit Testing Strategy
48:06
Просмотров 3 тыс.
Improve Your Communication Skills
1:01:52
Просмотров 6 тыс.
High Performance Logging and Custom Objects
29:07
Просмотров 1,5 тыс.
Introduction To gRPC
50:27
Просмотров 6 тыс.
Programming Destination for 2021
27:40
Просмотров 1,5 тыс.