I'm designing a small directory synchronization engine that can take different kinds of sources and handle incremental synchronization. I have defined an interface for a DirectorySource that currently looks like this:
public interface IDirectorySource
{
IEnumerable<IDirectoryEntry> GetChanges(IList<string> attributes, IChangeToken token);
}
I do want to provide an enumerator instead of a list in order to be able to avoid unnecessary memory allocation, but my problem is that I also want to return a new IChangeToken that contains the new state information needed for the next call to GetChanges (to do incremental updates). The changetoken must be calculated after the enumeration is completed since someone else could be updating the directory between different calls.
I've thought about having a second IChangeToken-parameter that recieves the new token, but that doesn't feel quite nice (and would not be consistent since the token would be returned immediately but cannot be populated until the enumeration is completed).. And I've thought about returning something like a "IChangeSet"-interface that contains an GetEnumerator method and a GetToken-method, but the problem is still that subsequent calls to the enumerator method returns different data (and therefore have different changetokens).
How can I design an interface that makes it "impossible" for a user of my interface to use it wrong regarding my GetChanges enumerator and retrieving the associated ChangeToken?
I hope my question makes sense... ;).. I had a hard time figuring out what title to use for my question... ;)