andywilsonuk / StringTokenFormatter

Provides string extension methods for replacing tokens within strings (using the format '{name}') with their specified lookup value.
Apache License 2.0
37 stars 1 forks source link

Async methods #18

Closed TonyValenti closed 3 years ago

TonyValenti commented 4 years ago

Hi @andywilsonuk - Any objection if I add some interfaces for working with Async objects? Essentially:

MyString.FormatTokenAsync
andywilsonuk commented 4 years ago

Hi @TonyValenti been a while.

Sounds like a good idea although it would only make sense if the token value lookups were async. Is that where you were going with this?

TonyValenti commented 4 years ago

Yep. I’ll introduce a new ITokenValueContainerAsync and create the appropriate methods and implementations.

andywilsonuk commented 4 years ago

Cool, go for it.

dviry commented 3 years ago

are we there yet? ;)

TonyValenti commented 3 years ago

@dviry I never ended up starting on this. Would love to though. Is your company willing to fund the dev work?

andywilsonuk commented 3 years ago

@dviry I'll take a look at implementing this.

TonyValenti commented 3 years ago

@andywilsonuk - As an FYI, when I scoped this out, I felt like a totally new interface was necessary to properly do this. And with a new async interface, I kind of wanted to refectory the sync interfaces to have a similar structure.

andywilsonuk commented 3 years ago

I've been too distracted to look at this again since the aborted first attempt but I still have some doubts about the use cases for using string formatting with async.

Any async implementation is by definition IO-bound and round tripping to map tokens to values seems incorrect (even with caching of previously mapped values. What I would probably do is write a custom ITokenValueContainer and fetch all the mapping values upfront, if there were too many for this to be practical I would instead parse the string into a SegmentedString, get the list of tokens used from that and fetch them in bulk.

Am I missing a trick? Is there a legitimate use case for requiring async which is strong enough to warrant the significant internal refactoring?

TonyValenti commented 3 years ago

The use case I had was specifically around asynchronous sub-objects. I have a custom container that will evaluate sub objects. For example: {Matter.Client.First_Name}

What I wanted to be able to do is make the sub-evaluation async. Essentially, all "basic" properties would be loaded and cached and child child objects would be asynchronously loaded and cached as requested.

andywilsonuk commented 3 years ago

Oh I see, I'll chew that over.

andywilsonuk commented 3 years ago

OK, I'll admit it, I'm not going to do this. Maybe one day...