I am working on a .NET project that needs to interact with some user-defined classes, designated as "jobs." All job classes must implement a specific IJob interface so the library can consume them. Sometimes a job class may contain an unmanaged resource that must be explicitly deleted.
How should I guarantee that all tasks will be properly disposed of after use, if I do not know in advance if the work needs explicit disposal? I have some ideas, but I would like to hear your comments / suggestions:
Make IJob : IDisposable , forcing all jobs to implement the Dispose() method. This will allow me to work with jobs in using blocks, but since most jobs do not require explicit deletion, this can add unnecessary confusion for client developers.
Do all job work in the try-finally blocks and use finally to ensure that Dispose() is called if the job implements IDisposable . This makes it easier for customers to implement a new class of tasks - not having to implement an empty Dispose() method, but also hides the fact that the library knows and cares about one-time tasks.
After writing this, I am inclined toward solution No. 1, but I still think that it would be nice to see alternative solutions and additional pros and cons of the two, which I already have in mind.
Jรธrn schou-rode
source share