RPS Value#

IRPSValue interface#

The main interface of RPS Value

public interface IRPSValue
{
    IReadOnlyDictionary<string, string> Dependencies { get; }

    RPSInstance Instance { get; }

    string Value { get; set; }

    ValueError Error { get; set; }

    void AddDependency(string name, string value);

    void RemoveDependency(string name);
}

public interface IRPSValue<out T> : IRPSValue
{
    T Original { get; }

    T Transformed { get; }
}

RPSInstance#

Represent mapping for RPS Value (pair of Class and Property names) with the ability to compare RPSInstance between themself.

public class RPSInstance
{
    public static readonly RPSInstance Empty = new RPSInstance();

    public RPSInstance()
    {
    }

    public RPSInstance(string className, string propertyName)
    {
        ClassName = className;
        PropertyName = propertyName;
    }

    public string ClassName { get; set; }

    public string PropertyName { get; set; }

    public override bool Equals(object obj)
        => obj is RPSInstance mapping && this == mapping;

    public override int GetHashCode()
    {
        unchecked
        {
            return (ClassName.GetHashCode() * 397) ^ PropertyName.GetHashCode();
        }
    }

    public static bool operator ==(RPSInstance x, RPSInstance y)
        => x?.ClassName == y?.ClassName && x?.PropertyName == y?.PropertyName;

    public static bool operator !=(RPSInstance x, RPSInstance y)
        => !(x == y);
}

RPSValueBase class#

Abstract class of RPSValue with basic functionality implementation

public abstract class RPSValueBase : IRPSValue
{
    private readonly Dictionary<string, string> _dependencies = new Dictionary<string, string>();

    protected RPSValueBase(RPSInstance instance = null,
        IDictionary<string, string> dependencies = null)
    {
        Instance = instance;

        if (dependencies != null)
            AddDependencyRange(dependencies);
    }

    public IReadOnlyDictionary<string, string> Dependencies => _dependencies;

    public RPSInstance Instance { get; }

    public virtual string Value { get; set; }

    public ValueError Error { get; set; }

    public void AddDependency(string name, string value)
        => _dependencies[name] = value;

    public void AddDependencyRange(IDictionary<string, string> dependencies)
    {
        foreach (KeyValuePair<string, string> dependency in dependencies)
            AddDependency(dependency.Key, dependency.Value);
    }

    public void RemoveDependency(string name)
        => _dependencies.Remove(name);
}

RPSValue class#

public class RPSValue : RPSValueBase, IRPSValue<string>
{
    public RPSValue(RPSInstance instance = null,
        string originalValue = default,
        IDictionary<string, string> dependencies = null)
        : base(instance: instance, dependencies: dependencies)
    {
        Original = originalValue;
    }

    public RPSValue(string className,
        string propertyName,
        string originalValue = default,
        IDictionary<string, string> dependencies = null)
        : this(instance: new RPSInstance(className, propertyName),
            originalValue:
            originalValue, dependencies: dependencies)
    {
    }

    public override string Value
    {
        get => Original;
        set => Transformed = value;
    }

    public virtual string Original { get; }

    public virtual string Transformed { get; protected set; }
}

public class RPSValue<TTarget> : RPSValue
{
    public RPSValue(TTarget target,
        RPSInstance instance = null,
        string originalValue = default,
        IDictionary<string, string> dependencies = null)
        : base(instance: instance,
            originalValue: originalValue,
            dependencies: dependencies)
    {
        Target = target;
    }

    public RPSValue(TTarget target,
        string className,
        string propertyName,
        string originalValue = default,
        IDictionary<string, string> dependencies = null)
        : this(target,
            instance: new RPSInstance(className, propertyName),
            originalValue: originalValue,
            dependencies: dependencies)
    {
    }

    public TTarget Target { get; set; }
}