October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Managed Extensions: Versioning Collection Classes

  • September 1, 2004
  • By Tom Archer
  • Send Email »
  • More Articles »

Welcome to this week's installment of .NET Tips & Techniques! Each week, award-winning Architect and Lead Programmer Tom Archer demonstrates how to perform a practical .NET programming task using either C# or Managed C++ Extensions.

In the previous two articles, I've illustrated how to make your C++ classes enumerable and how to add sorting capabilities to those classes. This article discusses the issue of versioning. Put simply, versioning your collections becomes important if the data held by the collection can be modified while a client is enumerating that data. Here's an example of the problem using the same Article class example I've been using throughout this series on programming collection classes with Managed Extensions:

  1. Client code instantiates an ArticleCollection object.
  2. Client code then requests an enumerator for that collection.
  3. The ArticleCollection instantiates an ArticleEnumerator object, passing to it the current data held in an internal member. This is done so that multiple clients can retrieve enumerators from the same collection object.
  4. Client code changes the collection's data—by adding or removing an item. As the enumerator object's data is for enumeration purposes only, it is now out of synch with the collection data. Therefore, the client code is now using an obsolete enumerator.
The following steps demonstrate a technique that a member of the Microsoft .NET Common Language Runtime (CLR) team showed me—a technique that he told me they use internally.
  1. Define an internal member of the collection class (the IEnumerable implementing class) that tracks the current version number. The following code snippet assumes, as a starting point, the article.h file used in the Managed Extensions: Sorting Your Collection Classes article:
    __gc class ArticleCollection : public IEnumerable
    {
    ...
      static int version = 0;
    
  2. Increment the version number member from any collection method that would render the collection invalid. For example, a method that allows client code to add data to the collection would obviously invalidate the collection for any client that is currently enumerating that object:
    __gc class ArticleCollection : public IEnumerable
    {
    ...
    public:
      void Add()
      {
        // If data is changed, update version so that any
        // outstanding enumerators are now out of data!
        version++;
        // perform add
      }
    
  3. Update the collection class's GetEnumerator method to pass the current version to the enumerator class's constructor. This is done so that you can track the version number at the enumerator object level:
    __gc class ArticleCollection : public IEnumerable
    {
    ...
    public:
      IEnumerator* GetEnumerator()
      {
        return dynamic_cast<IEnumerator*>(new ArticleEnumerator(this, version));
      }
    
  4. Update the enumerator object's constructor to also receive the version number and store it at the object instance level:
    __gc class ArticleEnumerator : public IEnumerator
    {
    ...
    public:
      ArticleEnumerator(ArticleCollection* collection, int version)
      {
        this->collection = collection;
        this->version = version;
        position = -1;
      }
    protected:
      int version;
    
  5. Add a method to the enumerator class to throw an exception if the client attempts to access an obsolete enumerator's data:
    __gc class ArticleEnumerator : public IEnumerator
    {
    protected:
      void VerifyVersion()
      {
        if (version != collection->version)
          throw new InvalidOperationException(S"Data out of sync. "
                                              S"Need to reacquire enumerator");
    }
    
  6. Finally, update the enumerator class's get_Current, MoveNext, and Reset methods to call the VerifyVersion method before attempting to perform their work:
    __gc class ArticleEnumerator : public IEnumerator
    {
    public:
      bool MoveNext()
      {
        VerifyVersion();
        ...
      }
    
      __property Object* get_Current()
      {
        VerifyVersion();
        ...
      }
    
      void Reset()
      {
        VerifyVersion();
        ...
      }
    };
    




Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel