Skip to content

Add IStringFormatterProvider concept to allow for custom string output #46

@carusology

Description

@carusology

Background

As of right now, there is only one way to render every single property's value on an ImmutableBase<TImmutable> inheriting class: the IStringFormatter initialized with the DefaultStringFormatter implementation. However, further down in our closed source code, there are ImmutableBase<TImmutable> implementations that store values which are sensitive - like reset tokens for emails. Those value objects that should not output their sensitive values if they are to be printed into loggers.

Therefore, I need to add the ability for an inheriting class to control which IStringFormatter is used for a given property. This can be done in a similar manner to #45.

Task

  • Create two new interfaces, as follows:

    public interface IStringFormatterFactory {
        IStringFormatter Create(PropertyInfo property);
    }
    
    public interface IStringFormatterProvider {
        bool IsSupported(PropertyInfo property);
    }
  • Create one implementation, DefaultStringFormatterProvider, that supports all types and does nothing but return the current DefaultStringFormatter implementation.

  • Create an AggregateStringFormatterProvider that takes in a list of IStringFormatterProvider implementations and processes them in order. The first match is used to create an IStringFormatter.

  • Use an IStringFormatterFactory in the ImmutableBase<TImmutable> static constructor to hydrate IStringFormatter implementations for each property instead of hard-coding all property values to use DefaultStringFormatter as it does today. It should have a public getter and setter such that others can change how properties are cached. Additionally, when it changes its value, that should cause all of the cached IStringFormatter implementations to be rebuilt using the new IStringFormatterFactory.

public static IStringFormatterFactory StringFormatterFactory { 
    get { return formatterFactory; }
    set { formatterFactory = value; ClearFormattersCache(); }
}

static ImmutableBase() {
    formatterFactory = new AggregateStringFormatterProvider(
        new DefaultStringFormatterProvider()
    );

    // ... other initialization code.
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions