Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactored out IImplicitBindingTypeConverter
  • Loading branch information
Oliver Weichhold committed May 28, 2013
1 parent 7dd7cc3 commit 9aefd04
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 32 deletions.
Expand Up @@ -7,7 +7,7 @@
namespace ReactiveUI.Cocoa
{
public class DateTimeNSDateConverter :
IImplicitBindingTypeConverter
IBindingTypeConverter
{
public static Lazy<DateTimeNSDateConverter> Instance = new Lazy<DateTimeNSDateConverter>();

Expand Down
2 changes: 1 addition & 1 deletion ReactiveUI.Platforms/ComponentModelTypeConverter.cs
Expand Up @@ -3,7 +3,7 @@

namespace ReactiveUI
{
public class ComponentModelTypeConverter : IImplicitBindingTypeConverter
public class ComponentModelTypeConverter : IBindingTypeConverter
{
readonly MemoizingMRUCache<Tuple<Type, Type>, TypeConverter> typeConverterCache = new MemoizingMRUCache<Tuple<Type, Type>, TypeConverter>((types, _) =>
{
Expand Down
6 changes: 3 additions & 3 deletions ReactiveUI.Platforms/Registrations.cs
Expand Up @@ -39,21 +39,21 @@ public void Register(Action<Func<object>, Type> registerFunction)
registerFunction(() => new PlatformOperations(), typeof(IPlatformOperations));

#if !WINRT && !WP8
registerFunction(() => new ComponentModelTypeConverter(), typeof(IImplicitBindingTypeConverter));
registerFunction(() => new ComponentModelTypeConverter(), typeof(IBindingTypeConverter));
#endif

#if !MONO
registerFunction(() => new DependencyObjectObservableForProperty(), typeof(ICreatesObservableForProperty));
registerFunction(() => new XamlDefaultPropertyBinding(), typeof(IDefaultPropertyBindingProvider));
registerFunction(() => new CreatesCommandBindingViaCommandParameter(), typeof(ICreatesCommandBinding));
registerFunction(() => new CreatesCommandBindingViaEvent(), typeof(ICreatesCommandBinding));
registerFunction(() => new BooleanToVisibilityTypeConverter(), typeof(IImplicitBindingTypeConverter));
registerFunction(() => new BooleanToVisibilityTypeConverter(), typeof(IBindingTypeConverter));
registerFunction(() => new AutoDataTemplateBindingHook(), typeof(IPropertyBindingHook));
#endif

#if UIKIT
registerFunction(() => UIKitObservableForProperty.Instance.Value, typeof(ICreatesObservableForProperty));
registerFunction(()=> DateTimeNSDateConverter.Instance.Value, typeof(IImplicitBindingTypeConverter));
registerFunction(()=> DateTimeNSDateConverter.Instance.Value, typeof(IBindingTypeConverter));
#endif

#if COCOA
Expand Down
4 changes: 2 additions & 2 deletions ReactiveUI/BindingTypeConverters.cs
Expand Up @@ -8,7 +8,7 @@

namespace ReactiveUI
{
public class EqualityTypeConverter : IImplicitBindingTypeConverter
public class EqualityTypeConverter : IBindingTypeConverter
{
public int GetAffinityForObjects(Type lhs, Type rhs)
{
Expand Down Expand Up @@ -81,7 +81,7 @@ public static object DoReferenceCast<T>(object from)
}
}

public class StringConverter : IImplicitBindingTypeConverter
public class StringConverter : IBindingTypeConverter
{
public int GetAffinityForObjects(Type lhs, Type rhs)
{
Expand Down
8 changes: 4 additions & 4 deletions ReactiveUI/PropertyBinding.cs
Expand Up @@ -1012,18 +1012,18 @@ public class PropertyBinderImplementation : IPropertyBinderImplementation
return shouldBind;
}

MemoizingMRUCache<Tuple<Type, Type>, IImplicitBindingTypeConverter> typeConverterCache = new MemoizingMRUCache<Tuple<Type, Type>, IImplicitBindingTypeConverter>(
MemoizingMRUCache<Tuple<Type, Type>, IBindingTypeConverter> typeConverterCache = new MemoizingMRUCache<Tuple<Type, Type>, IBindingTypeConverter>(
(types, _) => {
return RxApp.DependencyResolver.GetServices<IImplicitBindingTypeConverter>()
.Aggregate(Tuple.Create(-1, default(IImplicitBindingTypeConverter)), (acc, x) =>
return RxApp.DependencyResolver.GetServices<IBindingTypeConverter>()
.Aggregate(Tuple.Create(-1, default(IBindingTypeConverter)), (acc, x) =>
{
var score = x.GetAffinityForObjects(types.Item1, types.Item2);
return score > acc.Item1 && score > 0 ?
Tuple.Create(score, x) : acc;
}).Item2;
}, 25);

internal IImplicitBindingTypeConverter getConverterForTypes(Type lhs, Type rhs)
internal IBindingTypeConverter getConverterForTypes(Type lhs, Type rhs)
{
lock (typeConverterCache) {
return typeConverterCache.Get(Tuple.Create(lhs, rhs));
Expand Down
30 changes: 11 additions & 19 deletions ReactiveUI/RegisterableInterfaces.cs
Expand Up @@ -148,24 +148,6 @@ public interface ICreatesObservableForProperty : IEnableLogger
/// </summary>
public interface IBindingTypeConverter : IEnableLogger
{
/// <summary>
/// Convert a given object to the specified type.
/// </summary>
/// <param name="from">The object to convert.</param>
/// <param name="toType">The type to coerce the object to.</param>
/// <param name="conversionHint">An implementation-defined value,
/// usually to specify things like locale awareness.</param>
/// <returns>An object that is of the type 'to'</returns>
bool TryConvert(object from, Type toType, object conversionHint, out object result);
}

/// <summary>
/// This interface is the extensible implementation of IValueConverters for
/// Bind and OneWayBind. Implement this to teach Bind and OneWayBind how to
/// convert between types without explicitely specifing converters for bindings
/// </summary>
public interface IImplicitBindingTypeConverter : IBindingTypeConverter
{
/// <summary>
/// Returns a positive integer when this class supports
/// TryConvert for this particular Type. If the method isn't supported at
Expand All @@ -178,7 +160,17 @@ public interface IImplicitBindingTypeConverter : IBindingTypeConverter
/// <returns>A positive integer if TryConvert is supported,
/// zero or a negative value otherwise</returns>
int GetAffinityForObjects(Type lhs, Type rhs);
}

/// <summary>
/// Convert a given object to the specified type.
/// </summary>
/// <param name="from">The object to convert.</param>
/// <param name="toType">The type to coerce the object to.</param>
/// <param name="conversionHint">An implementation-defined value,
/// usually to specify things like locale awareness.</param>
/// <returns>An object that is of the type 'to'</returns>
bool TryConvert(object from, Type toType, object conversionHint, out object result);
}

/// <summary>
/// Implement this to teach Bind and OneWayBind how to guess the most
Expand Down
4 changes: 2 additions & 2 deletions ReactiveUI/Registrations.cs
Expand Up @@ -14,8 +14,8 @@ public void Register(Action<Func<object>, Type> registerFunction)
registerFunction(() => new IRNPCObservableForProperty(), typeof(ICreatesObservableForProperty));
registerFunction(() => new POCOObservableForProperty(), typeof(ICreatesObservableForProperty));
registerFunction(() => new NullDefaultPropertyBindingProvider(), typeof(IDefaultPropertyBindingProvider));
registerFunction(() => new EqualityTypeConverter(), typeof(IImplicitBindingTypeConverter));
registerFunction(() => new StringConverter(), typeof(IImplicitBindingTypeConverter));
registerFunction(() => new EqualityTypeConverter(), typeof(IBindingTypeConverter));
registerFunction(() => new StringConverter(), typeof(IBindingTypeConverter));
registerFunction(() => new DefaultViewLocator(), typeof(IViewLocator));
registerFunction(() => new DefaultLogManager(), typeof(ILogManager));
}
Expand Down

0 comments on commit 9aefd04

Please sign in to comment.