Tuesday, June 28, 2016

The most simple and reliable implementation of the Dispose design pattern

It would seem that this pattern is not just easy, and very simple, not analyzed in detail in a well-known book.

However, so far, even within the same project, it can often be implemented in different ways, creating a zoo bicycles, crutches and leaks.

I would like to share their way of implementation, which is based on the minimization of the invention, a bike, a maximum reduction of the amount of code and increasing its expression and transparency.

Preconditions

No mixing managed and unmanaged resources




I had not realized he and I do not advise colleagues to use ownership of managed and unmanaged resources in the same class.

Thus, one class can:
Not to own resources in general
Owning one unmanaged resource that is easy to convert it to a managed
Owning one or multiple managed resources

Implementation inheritance is not desirable

I do not use inheritance from classes unless absolutely necessary, the proposed implementation involves as an object-holder a sealed resource instance.
This does not mean that it can not be modified to support inheritance.

Wrappers for unmanaged resources realized by Janitor.Fody

No comments:

Post a Comment