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

function that raises an event when a token value is not found #9

Closed percramer closed 5 years ago

percramer commented 5 years ago

Hi,

i like your project. But would it be hard to ad an event that gets fired when a value for a token can't be found? This would allow us to then search for the value (in custom code) and pass it back.

Regards,

Per

andywilsonuk commented 5 years ago

Hi Per,

When you say value do you mean when a token isn't present in a dictionary (for example)?

This could be achieved by writing a custom ITokenValueContainer (as that is where the matching to tokens happens). Potentially also, a custom IValueFormatter which would be passed the unmatched token as both the segment original and value, so that when they are equal call out to your custom code.

Can you tell me a little more about what types of values you are passing in (string, lazy, etc)? as a dictionary or from object properties?

Andy

percramer commented 5 years ago

Hi Andy,

yes! I my application sometimes tokens need to get value that needs to be determined during runtime. So it the value could be the outcome of an sql query (which returns a single string), or something simple like the current time.

Regards,

Per

andywilsonuk commented 5 years ago

If you know the expected tokens and it's just the values you want to lookup at runtime then you can pass in either a lazy or a function (see usage 4 and 5 in the readme). If it's that you want to match any valid token and then pass that to, for example, SQL Server so that it can be looked up in a table, say, then that can be done with a custom implementation of ITokenValueContainer.

Andy