Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# ContextProperties
A Framework that enables you to evaluate properties context dependent.
The ContextProperty Framework provides a way to define properties that can return differenc values depending on a given context. The actual use of the such a property may be very specific but there are some examples:

* Localistaion of strin properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Text;
using phirSOFT.TopologicalComparison;

namespace phirSOFT.ContextProperties
{
public partial class ContextProperty<TValue>
{
private partial class ContextProviderTree
{
internal class ContextProviderComparer : ITopologicalComparer<IContextProvider<IContextProperty<TValue>, TValue>>
{
public int Compare(IContextProvider<IContextProperty<TValue>, TValue> x, IContextProvider<IContextProperty<TValue>, TValue> y)
{
{
if (x is ITopologicalComparable<IContextProvider<IContextProperty<TValue>, TValue>> xcomp)
return xcomp.CompareTo(y);

if (y is ITopologicalComparable<IContextProvider<IContextProperty<TValue>, TValue>> ycomp)
return ycomp.CompareTo(x);
}

{
if (x is ITopologicalComparable xcomp)
return xcomp.CompareTo(y);

if (y is ITopologicalComparable ycomp)
return ycomp.CompareTo(x);
}

{
if (x is IComparable<IContextProvider<IContextProperty<TValue>, TValue>> xcomp)
return xcomp.CompareTo(y);

if (y is IComparable<IContextProvider<IContextProperty<TValue>, TValue>> ycomp)
return ycomp.CompareTo(x);
}

{
if (x is IComparable xcomp)
return xcomp.CompareTo(y);

if (y is IComparable ycomp)
return ycomp.CompareTo(x);
}

throw new InvalidOperationException("Could not compare types");
}

public bool CanCompare(IContextProvider<IContextProperty<TValue>, TValue> x, IContextProvider<IContextProperty<TValue>, TValue> y)
{
{
if (x is ITopologicalComparable<IContextProvider<IContextProperty<TValue>, TValue>> xcomp)
return xcomp.CanCompareTo(y);

if (y is ITopologicalComparable<IContextProvider<IContextProperty<TValue>, TValue>> ycomp)
return ycomp.CanCompareTo(x);
}

{
if (x is ITopologicalComparable xcomp)
return xcomp.CanCompareTo(y);

if (y is ITopologicalComparable ycomp)
return ycomp.CanCompareTo(x);
}

return x is IComparable<IContextProvider<IContextProperty<TValue>, TValue>> ||
y is IComparable<IContextProvider<IContextProperty<TValue>, TValue>> ||
x is IComparable ||
y is IComparable;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public IEnumerable<IContextProvider<IContextProperty<TValue>, TValue>> this[ICon
return _nodes[key];

IContextProvider<IContextProperty<TValue>, TValue> lastFound = null;
var comparer = TopologicalComparer<IContextProvider<IContextProperty<TValue>, TValue>>.Default;
var comparer = new ContextProviderComparer();

foreach (var nodesKey in _nodes.Keys)
{
Expand Down
27 changes: 27 additions & 0 deletions phirSOFT.ContextProperties/TargetObjectContextProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace phirSOFT.ContextProperties
{
/// <summary>
/// Implements a <see cref="IContextProvider{TProperty,TValue}"/> that redirects the calls to the target object if possible.
/// </summary>
/// <typeparam name="TProperty">THe type of the property.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
public class TargetObjectContextProvider<TProperty, TValue> : IContextProvider<TProperty, TValue> where TProperty : IContextProperty<TValue>
{
/// <inheritdoc cref="IContextProvider{TProperty,TValue}.GetValue"/>
public TValue GetValue(object targetObject, TProperty targetProperty)
{
return ((IContextProvider<TProperty, TValue>) targetObject).GetValue(targetObject, targetProperty);
}

/// <inheritdoc cref="IContextProvider{TProperty,TValue}.OverridesValue"/>
public bool OverridesValue(object targetObject, TProperty targetProperty)
{
return targetObject is IContextProvider<TProperty, TValue> provider &&
provider.OverridesValue(targetObject, targetProperty);
}
}
}