diff --git a/README.md b/README.md index 3fa05ec..1a67361 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/phirSOFT.ContextProperties/ContextProperty.ContextProviderTree.ContextProviderComparer.cs b/phirSOFT.ContextProperties/ContextProperty.ContextProviderTree.ContextProviderComparer.cs new file mode 100644 index 0000000..1cab1c2 --- /dev/null +++ b/phirSOFT.ContextProperties/ContextProperty.ContextProviderTree.ContextProviderComparer.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Text; +using phirSOFT.TopologicalComparison; + +namespace phirSOFT.ContextProperties +{ + public partial class ContextProperty + { + private partial class ContextProviderTree + { + internal class ContextProviderComparer : ITopologicalComparer, TValue>> + { + public int Compare(IContextProvider, TValue> x, IContextProvider, TValue> y) + { + { + if (x is ITopologicalComparable, TValue>> xcomp) + return xcomp.CompareTo(y); + + if (y is ITopologicalComparable, 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, TValue>> xcomp) + return xcomp.CompareTo(y); + + if (y is IComparable, 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, TValue> x, IContextProvider, TValue> y) + { + { + if (x is ITopologicalComparable, TValue>> xcomp) + return xcomp.CanCompareTo(y); + + if (y is ITopologicalComparable, 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, TValue>> || + y is IComparable, TValue>> || + x is IComparable || + y is IComparable; + } + } + } + } +} diff --git a/phirSOFT.ContextProperties/ContextProperty.ContextProviderTree.cs b/phirSOFT.ContextProperties/ContextProperty.ContextProviderTree.cs index 486fb4d..0fb06f7 100644 --- a/phirSOFT.ContextProperties/ContextProperty.ContextProviderTree.cs +++ b/phirSOFT.ContextProperties/ContextProperty.ContextProviderTree.cs @@ -88,7 +88,7 @@ public IEnumerable, TValue>> this[ICon return _nodes[key]; IContextProvider, TValue> lastFound = null; - var comparer = TopologicalComparer, TValue>>.Default; + var comparer = new ContextProviderComparer(); foreach (var nodesKey in _nodes.Keys) { diff --git a/phirSOFT.ContextProperties/TargetObjectContextProvider.cs b/phirSOFT.ContextProperties/TargetObjectContextProvider.cs new file mode 100644 index 0000000..5d61e96 --- /dev/null +++ b/phirSOFT.ContextProperties/TargetObjectContextProvider.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace phirSOFT.ContextProperties +{ + /// + /// Implements a that redirects the calls to the target object if possible. + /// + /// THe type of the property. + /// The type of the value. + public class TargetObjectContextProvider : IContextProvider where TProperty : IContextProperty + { + /// + public TValue GetValue(object targetObject, TProperty targetProperty) + { + return ((IContextProvider) targetObject).GetValue(targetObject, targetProperty); + } + + /// + public bool OverridesValue(object targetObject, TProperty targetProperty) + { + return targetObject is IContextProvider provider && + provider.OverridesValue(targetObject, targetProperty); + } + } +}