Design pattern for an ordered call

I am developing a small directory synchronization mechanism that can use different sources and handle incremental synchronization. I have defined an interface for DirectorySource, which currently looks like this:

public interface IDirectorySource
{
    IEnumerable<IDirectoryEntry> GetChanges(IList<string> attributes, IChangeToken token);
}

I want to provide an enumerator instead of a list to avoid unnecessary memory allocation, but my problem is that I also want to return a new IChangeToken that contains the new status information needed for the next GetChanges call (for incremental update). The changetoken value must be calculated after the listing is complete, as someone else can update the directory between different calls.

I was thinking about having a second parameter IChangeToken, which receives a new token, but it is not very pleasant (and will not be sequential, since the token will be returned immediately, but cannot be filled until the transfer is completed) .. And I thought about returning something like the "IChangeSet" interface, which contains the GetEnumerator method and the GetToken method, but the problem is that subsequent calls to the enumerator method return different data (and therefore have different changetokens).

How can I create an interface that makes it "impossible" for a user of my interface to use it incorrectly with respect to my GetChanges counter and getting the associated ChangeToken?

I hope my question makes sense ...;) .. It was difficult for me to understand which title to use for my question ...;)

+3
2

, Tuple. , Tuple - - , , , . .NET Framework (3.5) , (4.0). Tuple - . , # , F #, , .

, , :

Tuple<IEnumerable<IDirectoryEntry>, IChangeToken> GetChanges(IList<string> attributes, IChangeToken token);
+1

, "IChangeSet", . IDirectorySource :

public interface IDirectorySource
{
    IChangeSet GetChanges(IList<string> attributes, IChangeToken token);
}

IChangeSet, :

public interface IChangeSet : IEnumerable<IDirectoryEntry>
{
    IChangeToken GetToken();
}

, :

IChangeSet result = source.GetChanges(attributes, token);

foreach (IDirectoryEntry entry in result) {
    // Do something with the data...
}

IChangeToken resultToken = result.GetToken();

, GetToken() ( ), , .

100%, , ...

0

Source: https://habr.com/ru/post/1733768/


All Articles