Permalink
Browse files

moved everything with ObjectConverter to a new Conversion namespace, …

…added the TypeDescriptorConversionFamily for ObjectConverter
  • Loading branch information...
1 parent ea1ad8f commit be44d35ac4066129d7cb99f8c2812569ccd82bae @jeremydmiller jeremydmiller committed Nov 27, 2011
Showing with 628 additions and 475 deletions.
  1. +1 −0 src/FubuCore.Testing/Binding/BasicTypeConverterTester.cs
  2. +1 −0 src/FubuCore.Testing/Binding/SmartRequestTester.cs
  3. +1 −0 src/FubuCore.Testing/CommandLine/ArgumentTester.cs
  4. +1 −0 src/FubuCore.Testing/CommandLine/EnumerableArgumentTester.cs
  5. +1 −0 src/FubuCore.Testing/CommandLine/FlagTester.cs
  6. +1 −1 src/FubuCore.Testing/{ → Conversion}/GetStringRequestTester.cs
  7. +2 −1 src/FubuCore.Testing/{ → Conversion}/ObjectConverterTester.cs
  8. +2 −1 src/FubuCore.Testing/{ → Conversion}/StringConstructorConverterFamilyTester.cs
  9. +44 −0 src/FubuCore.Testing/Conversion/TypeDescripterConverterFamilyTester.cs
  10. +4 −3 src/FubuCore.Testing/FubuCore.Testing.csproj
  11. +1 −0 src/FubuCore.Testing/Reflection/Expressions/StringStartsWithPropertyOperationTester.cs
  12. +18 −0 src/FubuCore/Binding/AttributePropertyBinder.cs
  13. +11 −0 src/FubuCore/Binding/BindingAttribute.cs
  14. +1 −0 src/FubuCore/Binding/BindingContext.cs
  15. +0 −41 src/FubuCore/Binding/IPropertyBinder.cs
  16. +25 −0 src/FubuCore/Binding/IgnorePropertyBinder.cs
  17. +2 −1 src/FubuCore/Binding/SmartRequest.cs
  18. +62 −0 src/FubuCore/Binding/TypeDescriptorConverterFamily.cs
  19. +7 −63 src/FubuCore/Binding/ValueConverterRegistry.cs
  20. +2 −1 src/FubuCore/CommandLine/Argument.cs
  21. +1 −0 src/FubuCore/CommandLine/EnumerableArgument.cs
  22. +2 −1 src/FubuCore/CommandLine/Flag.cs
  23. +2 −1 src/FubuCore/CommandLine/InputParser.cs
  24. +44 −0 src/FubuCore/Conversion/ArrayConverterFamily.cs
  25. +18 −0 src/FubuCore/Conversion/EnumConverterFamily.cs
  26. +31 −0 src/FubuCore/Conversion/IObjectConverter.cs
  27. +20 −0 src/FubuCore/Conversion/IObjectConverterFamily.cs
  28. +26 −0 src/FubuCore/Conversion/NullableConverterFamily.cs
  29. +188 −0 src/FubuCore/Conversion/ObjectConverter.cs
  30. +44 −0 src/FubuCore/Conversion/StringConstructorConverterFamily.cs
  31. +31 −0 src/FubuCore/Conversion/TypeDescripterConverterFamily.cs
  32. +20 −0 src/FubuCore/Conversion/TypeDescriptorFamily.cs
  33. +13 −1 src/FubuCore/FubuCore.csproj
  34. +0 −360 src/FubuCore/ObjectConverter.cs
  35. +1 −0 src/FubuCore/ServiceEnabledObjectConverter.cs
@@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using FubuCore.Binding;
+using FubuCore.Testing.Conversion;
using FubuTestingSupport;
using NUnit.Framework;
using Rhino.Mocks;
@@ -1,5 +1,6 @@
using System;
using FubuCore.Binding;
+using FubuCore.Conversion;
using FubuTestingSupport;
using NUnit.Framework;
using Rhino.Mocks;
@@ -1,6 +1,7 @@
using System;
using System.Linq.Expressions;
using FubuCore.CommandLine;
+using FubuCore.Conversion;
using FubuCore.Reflection;
using FubuTestingSupport;
using NUnit.Framework;
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using FubuCore.CommandLine;
+using FubuCore.Conversion;
using FubuCore.Reflection;
using FubuTestingSupport;
using NUnit.Framework;
@@ -1,6 +1,7 @@
using System;
using System.Linq.Expressions;
using FubuCore.CommandLine;
+using FubuCore.Conversion;
using FubuTestingSupport;
using NUnit.Framework;
using FubuCore.Reflection;
@@ -5,7 +5,7 @@
using Rhino.Mocks;
using FubuTestingSupport;
-namespace FubuCore.Testing
+namespace FubuCore.Testing.Conversion
{
[TestFixture]
public class GetStringRequestTester
@@ -4,11 +4,12 @@
using System.Globalization;
using System.Linq;
using System.Threading;
+using FubuCore.Conversion;
using FubuCore.Testing.Binding;
using FubuTestingSupport;
using NUnit.Framework;
-namespace FubuCore.Testing
+namespace FubuCore.Testing.Conversion
{
public class Service
{
@@ -1,8 +1,9 @@
+using FubuCore.Conversion;
using FubuCore.Testing.Reflection.Expressions;
using NUnit.Framework;
using FubuTestingSupport;
-namespace FubuCore.Testing
+namespace FubuCore.Testing.Conversion
{
[TestFixture]
public class StringConstructorConverterFamilyTester
@@ -0,0 +1,44 @@
+using System;
+using FubuCore.Conversion;
+using NUnit.Framework;
+using FubuTestingSupport;
+
+namespace FubuCore.Testing.Conversion
+{
+ [TestFixture]
+ public class TypeDescripterConverterFamilyTester
+ {
+ [Test]
+ public void matches_positive()
+ {
+ var family = new TypeDescripterConverterFamily();
+ family.Matches(typeof(string), null).ShouldBeTrue();
+ family.Matches(typeof(int), null).ShouldBeTrue();
+ family.Matches(typeof(DateTime), null).ShouldBeTrue();
+ family.Matches(typeof(bool), null).ShouldBeTrue();
+ }
+
+ [Test]
+ public void matches_negative()
+ {
+ var family = new TypeDescripterConverterFamily();
+ family.Matches(GetType(), null).ShouldBeFalse();
+ }
+
+ [Test]
+ public void create_a_working_converter()
+ {
+ var family = new TypeDescripterConverterFamily();
+
+ var boolConverter = family.CreateConverter(typeof (bool), null);
+ var intConverter = family.CreateConverter(typeof (int), null);
+
+ boolConverter("true").ShouldEqual(true);
+ boolConverter("false").ShouldEqual(false);
+
+ intConverter("123").ShouldEqual(123);
+ intConverter("456").ShouldEqual(456);
+
+ }
+ }
+}
@@ -90,6 +90,7 @@
<Compile Include="Binding\ServiceArgumentsTester.cs" />
<Compile Include="Binding\SmartRequestTester.cs" />
<Compile Include="Binding\StandardModelBinderTester.cs" />
+ <Compile Include="Conversion\TypeDescripterConverterFamilyTester.cs" />
<Compile Include="Binding\TypeDescriptorCacheTester.cs" />
<Compile Include="Binding\ValueConverterRegistryTester.cs" />
<Compile Include="BooleanExtensionsTester.cs" />
@@ -123,9 +124,9 @@
<Compile Include="FlatFileWriterTester.cs" />
<Compile Include="FubuExceptionTester.cs" />
<Compile Include="GenericEnumerableExtensionsTester.cs" />
- <Compile Include="GetStringRequestTester.cs" />
+ <Compile Include="Conversion\GetStringRequestTester.cs" />
<Compile Include="NumberExtensionsTester.cs" />
- <Compile Include="ObjectConverterTester.cs" />
+ <Compile Include="Conversion\ObjectConverterTester.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionExtensionsTester.cs" />
<Compile Include="Reflection\Expressions\ConstructorBuilderTester.cs" />
@@ -146,7 +147,7 @@
<Compile Include="Reflection\ReflectionExtensionsTester.cs" />
<Compile Include="Reflection\ReflectionHelperTester.cs" />
<Compile Include="Reflection\SinglePropertyTester.cs" />
- <Compile Include="StringConstructorConverterFamilyTester.cs" />
+ <Compile Include="Conversion\StringConstructorConverterFamilyTester.cs" />
<Compile Include="StringExtensionsTester.cs" />
<Compile Include="StringifierTester.cs" />
<Compile Include="TemplateParserTester.cs" />
@@ -1,5 +1,6 @@
using System;
using FubuCore.Reflection.Expressions;
+using FubuCore.Testing.Conversion;
using FubuTestingSupport;
using NUnit.Framework;
@@ -0,0 +1,18 @@
+using System.Reflection;
+using FubuCore.Reflection;
+
+namespace FubuCore.Binding
+{
+ public class AttributePropertyBinder : IPropertyBinder
+ {
+ public bool Matches(PropertyInfo property)
+ {
+ return property.HasAttribute<BindingAttribute>();
+ }
+
+ public void Bind(PropertyInfo property, IBindingContext context)
+ {
+ property.ForAttribute<BindingAttribute>(att => att.Bind(property, context));
+ }
+ }
+}
@@ -0,0 +1,11 @@
+using System;
+using System.Reflection;
+
+namespace FubuCore.Binding
+{
+ [AttributeUsage(AttributeTargets.Property)]
+ public abstract class BindingAttribute : Attribute
+ {
+ public abstract void Bind(PropertyInfo property, IBindingContext context);
+ }
+}
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
+using FubuCore.Conversion;
using FubuMVC.Core;
using Microsoft.Practices.ServiceLocation;
@@ -1,51 +1,10 @@
-using System;
using System.Reflection;
-using FubuCore.Reflection;
namespace FubuCore.Binding
{
- [AttributeUsage(AttributeTargets.Property)]
- public abstract class BindingAttribute : Attribute
- {
- public abstract void Bind(PropertyInfo property, IBindingContext context);
- }
-
- public class AttributePropertyBinder : IPropertyBinder
- {
- public bool Matches(PropertyInfo property)
- {
- return property.HasAttribute<BindingAttribute>();
- }
-
- public void Bind(PropertyInfo property, IBindingContext context)
- {
- property.ForAttribute<BindingAttribute>(att => att.Bind(property, context));
- }
- }
-
public interface IPropertyBinder
{
bool Matches(PropertyInfo property);
void Bind(PropertyInfo property, IBindingContext context);
}
-
- public class IgnorePropertyBinder : IPropertyBinder
- {
- private readonly Func<PropertyInfo, bool> _filter;
-
- public IgnorePropertyBinder(Func<PropertyInfo, bool> filter)
- {
- _filter = filter;
- }
-
- public bool Matches(PropertyInfo property)
- {
- return _filter(property);
- }
-
- public void Bind(PropertyInfo property, IBindingContext context)
- {
- // no-op
- }
- }
}
@@ -0,0 +1,25 @@
+using System;
+using System.Reflection;
+
+namespace FubuCore.Binding
+{
+ public class IgnorePropertyBinder : IPropertyBinder
+ {
+ private readonly Func<PropertyInfo, bool> _filter;
+
+ public IgnorePropertyBinder(Func<PropertyInfo, bool> filter)
+ {
+ _filter = filter;
+ }
+
+ public bool Matches(PropertyInfo property)
+ {
+ return _filter(property);
+ }
+
+ public void Bind(PropertyInfo property, IBindingContext context)
+ {
+ // no-op
+ }
+ }
+}
@@ -1,4 +1,5 @@
-using System;
+using System;
+using FubuCore.Conversion;
namespace FubuCore.Binding
{
@@ -0,0 +1,62 @@
+using System;
+using System.ComponentModel;
+using System.Reflection;
+using FubuCore.Util;
+
+namespace FubuCore.Binding
+{
+ public class TypeDescriptorConverterFamily : IConverterFamily
+ {
+ private readonly Cache<Type, ValueConverter> _converters
+ = new Cache<Type, ValueConverter>(type => new BasicValueConverter(type));
+
+ public bool Matches(PropertyInfo property)
+ {
+ try
+ {
+ return TypeDescriptor.GetConverter(property.PropertyType).CanConvertFrom(typeof (string));
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+ }
+
+
+ public ValueConverter Build(IValueConverterRegistry registry, PropertyInfo property)
+ {
+ var propertyType = property.PropertyType;
+
+ return _converters[propertyType];
+ }
+
+ #region Nested type: BasicValueConverter
+
+ public class BasicValueConverter : ValueConverter
+ {
+ private readonly TypeConverter _converter;
+
+ public BasicValueConverter(Type propertyType)
+ {
+ _converter = TypeDescriptor.GetConverter(propertyType);
+ }
+
+ public object Convert(IPropertyContext context)
+ {
+ var propertyType = context.Property.PropertyType;
+
+ if (context.PropertyValue != null)
+ {
+ if (context.PropertyValue.GetType() == propertyType)
+ {
+ return context.PropertyValue;
+ }
+ }
+
+ return _converter.ConvertFrom(context.PropertyValue);
+ }
+ }
+
+ #endregion
+ }
+}
Oops, something went wrong.

0 comments on commit be44d35

Please sign in to comment.