No video

IDisposable Exposed

  Рет қаралды 3,287

Shiv Kumar

Shiv Kumar

Күн бұрын

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

Пікірлер: 27
@Karen-mj9kk
@Karen-mj9kk Жыл бұрын
Best explanation I've found about IDisposable. Thank you!
@Hect0rx
@Hect0rx 2 жыл бұрын
Great video and topic as always, one of the few channels here on KZbin that deliver professional in-depth content
@Matlus
@Matlus 2 жыл бұрын
Thank you Arghs! Your comment is much appreciated.
@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.
@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 :)
@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?
@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.
@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!
@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.
@matthewparker7789
@matthewparker7789 Жыл бұрын
Great video: only 13k subs ? :-(
@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!
@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.
Let's Talk - Null Conditional Operator. No Thank you!
20:28
Shiv Kumar
Рет қаралды 2,2 М.
To LINQ Or Not To LINQ - That is the Question
32:35
Shiv Kumar
Рет қаралды 3,3 М.
Inside Out 2: Who is the strongest? Joy vs Envy vs Anger #shorts #animation
00:22
Son ❤️ #shorts by Leisi Show
00:41
Leisi Show
Рет қаралды 10 МЛН
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 2,5 МЛН
Prefer Composition Over Inheritance - A Real-World Example
29:58
Shiv Kumar
Рет қаралды 4,9 М.
Unit Testing Strategy
48:06
Shiv Kumar
Рет қаралды 3,1 М.
High Performance Logging and Custom Objects
29:07
Shiv Kumar
Рет қаралды 1,5 М.
Abstraction in Software Design
48:01
Shiv Kumar
Рет қаралды 6 М.
Logging And Application Insights in Non-ASP.NET Applications
33:52
Let's Talk - Always use the "as" operator - No Thank you
35:20
Shiv Kumar
Рет қаралды 1,4 М.
Custom Loggers. Importance of Logging Off Application Servers
37:11
Introduction To gRPC
50:27
Shiv Kumar
Рет қаралды 6 М.
gRPC - Getting Started .NET 5/ .NET Core 3.x
1:01:00
Shiv Kumar
Рет қаралды 3,8 М.
C# 9 Record Types
40:23
Shiv Kumar
Рет қаралды 3,1 М.
Inside Out 2: Who is the strongest? Joy vs Envy vs Anger #shorts #animation
00:22