Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 719793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:41:05+00:00 2026-05-14T05:41:05+00:00

I have a Location class which represents a location somewhere in a stream. (The

  • 0

I have a Location class which represents a location somewhere in a stream. (The class isn’t coupled to any specific stream.) The location information will be used to match tokens to location in the input in my parser, to allow for nicer error reporting to the user.

I want to add location tracking to a TextReader instance. This way, while reading tokens, I can grab the location (which is updated by the TextReader as data is read) and give it to the token during the tokenization process.

I am looking for a good approach on accomplishing this goal. I have come up with several designs.

Manual location tracking

Every time I need to read from the TextReader, I call AdvanceString on the Location object of the tokenizer with the data read.

Advantages

  • Very simple.
  • No class bloat.
  • No need to rewrite the TextReader methods.

Disadvantages

  • Couples location tracking logic to tokenization process.
  • Easy to forget to track something (though unit testing helps with this).
  • Bloats existing code.

Plain TextReader wrapper

Create a LocatedTextReaderWrapper class which surrounds each method call, tracking a Location property. Example:

public class LocatedTextReaderWrapper : TextReader {
    private TextReader source;

    public Location Location {
        get;
        set;
    }

    public LocatedTextReaderWrapper(TextReader source) :
        this(source, new Location()) {
    }

    public LocatedTextReaderWrapper(TextReader source, Location location) {
        this.Location = location;
        this.source = source;
    }

    public override int Read(char[] buffer, int index, int count) {
        int ret = this.source.Read(buffer, index, count);

        if(ret >= 0) {
            this.location.AdvanceString(string.Concat(buffer.Skip(index).Take(count)));
        }

        return ret;
    }

    // etc.
}

Advantages

  • Tokenization doesn’t know about Location tracking.

Disadvantages

  • User needs to create and dispose a LocatedTextReaderWrapper instance, in addition to their TextReader instance.
  • Doesn’t allow different types of tracking or different location trackers to be added without layers of wrappers.

Event-based TextReader wrapper

Like LocatedTextReaderWrapper, but decouples it from the Location object raising an event whenever data is read.

Advantages

  • Can be reused for other types of tracking.
  • Tokenization doesn’t know about Location tracking or other tracking.
  • Can have multiple, independent Location objects (or other methods of tracking) tracking at once.

Disadvantages

  • Requires boilerplate code to enable location tracking.
  • User needs to create and dispose the wrapper instance, in addition to their TextReader instance.

Aspect-orientated approach

Use AOP to perform like the event-based wrapper approach.

Advantages

  • Can be reused for other types of tracking.
  • Tokenization doesn’t know about Location tracking or other tracking.
  • No need to rewrite the TextReader methods.

Disadvantages

  • Requires external dependencies, which I want to avoid.

I am looking for the best approach in my situation. I would like to:

  • Not bloat the tokenizer methods with location tracking.
  • Not require heavy initialization in user code.
  • Not have any/much boilerplate/duplicated code.
  • (Perhaps) not couple the TextReader with the Location class.

Any insight into this problem and possible solutions or adjustments are welcome. Thanks!

(For those who want a specific question: What is the best way to wrap the functionality of a TextReader?)

I have implemented the “Plain TextReader wrapper” and “Event-based TextReader wrapper” approaches and am displeased with both, for reasons mentioned in their disadvantages.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:41:05+00:00Added an answer on May 14, 2026 at 5:41 am

    I agree that creating a wrapper that implements TextReader, delegates the implementation to the underlying TextReader and adds some additional support for tracking of location is a good approach. You can think of this as the Decorator pattern, because you’re decorating the TextReader class with some additional functionality.

    Better wrapping: You can write it in a simpler way to decouple your TextReader from the Location type – this way you can easily modify Location independently or even provide other features that are based on tracking the progress of reading.

    interface ITracker {
      void AdvanceString(string s);
    }
    
    class TrackingReader : TextReader {
      private TextReader source;
      private ITracker tracker;
      public TrackingReader(TextReader source, ITracker tracker) {
        this.source = source;
        this.tracker = tracker;
      }
      public override int Read(char[] buffer, int index, int count) {
        int res = base.Read(buffer, index, count);
        tracker.AdvanceString(buffer.Skip(index).Take(res);
        return res;
      }
    }
    

    I would also encapsulate creation of the tracker into some factory method (so that you have a single place in the application that deals with construction). Note that you can use this simple desing to create a TextReader that reports the progress to multiple Location objects too:

    static TextReader CreateReader(TextReader source, params ITracker[] trackers) {
       return trackers.Aggregate(source, (reader, tracker) =>
           new TrackingReader(reader, tracker));
    }
    

    This creates a chain of TrackingReader objects and each of the object is reporting the progress of reading to one of the trackers passed as arguments.

    Regarding Event-based: I think that using standard .NET/C# events for this kind of thing isn’t done as frequently in the .NET libraries, but I think this approach may be quite interesting too – especially in C# 3 where you can use features like lambda expressions or even Reactive Framework.

    Simple use doesn’t add that much boiler plate:

    using(ReaderWithEvents reader = new ReaderWithEvents(source)) {
      reader.Advance += str => loc.AdvanceString(str);
      // ..
    }
    

    However, you could also use Reactive Extensions to process the events. To count the number of new lines in the source text, you could write something like this:

    var res =
      (from e in Observable.FromEvent(reader, "Advance")
       select e.Count(ch => ch == '\n')).Scan(0, (sum, current) => sum + current);
    

    This would give you an event res that fires each time you read some string from the TextReader and the value carried by the event would be the current number of line (in the text).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't need to worry at what the customer enters… May 14, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer There's a snippet for that. Usage is: attachment = ExtFileField(ext_whitelist=(".pdf",… May 14, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer In your table view, include a cell with the text… May 14, 2026 at 4:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.