Conservative Identification Marking on Interfaces

In a situation where interface implementations may or may not need to be removed, is it advisable to require that all implementations be removed by marking the interface itself asIDisposable ?

This forces users of this interface to always delete objects, even if it may be unnecessary for certain implementations. The goal is to ensure that implementations are always easily replaceable . An alternative is to give the user the right to decide what may seem more correct, but more difficult to communicate. On the other hand, given a user who has only a character declared with an interface type, it might seem more appropriate to require that it be deleted unconditionally. Is there a compromise or fallacy in this reasoning?

If the question is too dependent on the situation, I would like to create it in the context of textbook repositories (repository design template), where most of the known implementations will need to be disposed of.

+4
source share
4 answers

If the question is too dependent on the situation, I would like to create it in the context of textbook repositories (repository design template), where most of the known implementations will need to be disposed of.

If you write library code that creates objects and is responsible for their lifetime, then the ideal scenario would be to process IDisposable, but not to require it, if in the vast majority of cases most processes do not require cleaning; for example, using isor asat the moment when your code is executed with an unknown object. This is rarely as simple as this, but for example:

IFoo obj = (IFoo)Activator.CreateInstance(unknownType);
using(obj as IDisposable)
{
    // TODO: code
}
+2
source

, IFoo IDisposable, , , IFoo, , IFoo , , , , factory, IFoo, factory.

IEnumerator. , , (, , ), factory IEnumerable.GetEnumerator(), IEnumerator. , IEnumerable.GetEnumerator(), , ; , , IEnumerable, , , , IDisposable , , .

Microsoft IEnumerator<T>, , , IDisposable, , , do-nothing Dispose. , Dispose , IDisposable, do-nothing Dispose , , , , , , IDisposable, .

1% IEnumerator<T> IDisposable, , , , . IEnumerable<T> inherit IDisposable, , - , .

+2

,

ISomeInterface

, Dispose, IDisposable.

ISomeInterface : IDisposable

, IDisposable, , ISomeInterface Dispose themsevles, using.

Dispose , . , .


,

, ,

, a Stream " ".

+1

, / , , , .

, , - /:

  • , , , IDisposable , . , , , , : , , . , Dispose , . , ( , - , , , ).

  • , , , , , IDisposable , IDisposable. , . - -.

tl; dr: , , . .

, , ; - , , , . . .

After you have rated each, immediately take all the considerations, collect your opinion and make your decision.

0
source

All Articles