-
-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing IDisposable on MimeEntity #732
Comments
I had a patch for this here: https://github.com/jstedfast/MimeKit/blob/master/disposable.patch It probably doesn't apply cleanly anymore, but it's a start. IIRC, I did not commit the patch because it caused the unit tests to fail and I never figured out how/why. |
Weird, if you've got a non-disposable class, obviously none of the tests invokes Do you mind if I have a go at it, or ...? |
I just updated disposable.patch. Feel free to play around with it. |
As I ported the patch, I spotted the issues. |
Does this imply the next release will have |
Depends... Keep in mind that things get more complicated when you add IDisposable support. Should |
Not really, although I see your point, this is a "C++ way of thinking". The important part is to have MIME is a tree structure, implying people typically open Then if some don't like this behaviour, they can simply avoid invoking I cannot imagine how this would break existing code, except in cases where others have inherited from |
It would seem intuitive to me that the following Those types of things are the types of things that broke my unit tests, btw. |
Hmm, I had an idea as I started thinking about our little problem. Maybe just keep the existing classes as they are, and create "YALOA" ...? (Yet Another Layer Of Abstraction) As in, create a class inheriting from That way there would be zero changes to the existing classes, yet still allow us to consume the library "as is" and easily clean up stuff ...? Not sure how that would work in regards to MailKit, and places that creates My current problem is that I've got to add "clean up code" every place I create
OK, so you disposed old entities at the above points ...?
...? Even if that's bad, the Maybe this becomes dirty'ish, but a lot of people would probably use middleware code, created by layers between MimeKit and client/application code, and the first thing they'd look for, is if |
Why wouldn't you just implement an extension method? public static void Dispose (this MimeEntity entity)
{
if (entity is Multipart multipart) {
for (int i = 0; i < multipart.Count; i++)
multipart[i].Dispose ();
} else if (entity is MessagePart rfc822) {
if (rfc822.Message != null)
rfc822.Message.Dispose ();
} else if (entity is MimePart part) {
if (part.Content != null)
part.Content.Stream.Dispose ();
}
}
public static void Dispose (this MimeMessage message)
{
message.Body.Dispose ();
} |
I'd need to duplicate code in all projects referencing the project, but don't worry, I'll figure it out ... |
Decided the best coarse would be to make Remove/RemoveAt/Clear/set_Item not dispose. Added a Clear(bool dispose) method for convenience for the Clear() case. |
Thank you man, this makes my life a 100 times easier :) Let me know when you've got a release out somehow with this. Maybe just commenting "done" on this guy or something. |
MimeKit v3.0.0 has been released with this feature. |
Najs, thx man :) |
Sorry to bother you, but for consistency reasons we should probably implement Thank you for the |
You mean MimeMessage? (MailMessage is System.Net.Mail) If so, I already made MimeMessage implement IDisposable. |
Thx mate :) |
Is your feature request related to a problem? Please describe.
When I create a
MimeEntity
that somehow loads content from files, this is typically done by opening stream and associating these with theContent
object. This implies I'll have to manually track all streams associated directly and indirectly with myMimeEntity
, which of course creates more complex and less "DRY" code for me.Describe the solution you'd like
Basically, implement
IDisposable
interface onMimeEntity
, for then to have its implementation recursively disposing allStream
objects directly or indirectly associated with theMimeEntity
. Below is example code.Describe alternatives you've considered
The alternative is to duplicate the above code every time I might in theory have an open
Stream
somehow associated with myMimeEntity
, which of course is sub-optimal.The text was updated successfully, but these errors were encountered: