Spring.NET 1.3.0 RC1 for .NET 2.0 API Reference

CustomConverterConfigurer Class

IObjectFactoryPostProcessor implementation that allows for convenient registration of custom TypeConverters.

For a list of all members of this type, see CustomConverterConfigurer Members .

System.Object
   AbstractConfigurer
      CustomConverterConfigurer

[Visual Basic]
<Serializable> _
Public Class CustomConverterConfigurer
    Inherits AbstractConfigurer
[C#]
[Serializable]
public class CustomConverterConfigurer : AbstractConfigurer

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

Remarks

Note    The use of this class is typically not required; the .NET mechanism of associating a TypeConverter with a Type via the use of the TypeConverterAttribute is the recommended (and standard) way. This class primarily exists to cover those cases where third party classes to which one does not have the source need to be exposed to the type conversion mechanism.

Because the CustomConverterConfigurer class implements the IObjectFactoryPostProcessor interface, instances of this class that have been exposed in the scope of an IApplicationContext will automatically be picked up by the application context and made available to the IoC container whenever type conversion is required. If one is using a CustomConverterConfigurer object definition within the scope of an IObjectFactory, no such automatic pickup of the CustomConverterConfigurer is performed (custom converters will have to be added manually using the RegisterCustomConverter method). For most application scenarios, one will get better mileage using the IApplicationContext abstraction.

Example

The following examples all assume XML based configuration, and use inner object definitions to define the custom TypeConverter objects (nominally to avoid polluting the object name space, but also because the configuration simply reads better that way).

  <object id="customConverterConfigurer" type="Spring.Objects.Factory.Config.CustomConverterConfigurer, Spring.Core">
    <property name="CustomConverters">
      <dictionary>
        <entry key="System.Date">
          <object type="MyNamespace.MyCustomDateEditor" />
        </entry>
        <entry key="MyNamespace.MyObject, MyAssembly">
          <object id="myConverter" type="MyNamespace.MObjectConverter, MyOtherAssembly">
            <property name="aProperty" value="..." />
          </object>
        </entry>
      </dictionary>
    </property>
  </object>

The following example illustrates a complete (albeit naieve) use case for this class, including a custom TypeConverter implementation, said converters domain class, and the XML configuration that hooks the converter in place and makes it available to a Spring.NET container for use during object resolution.

The domain class is a simple data-only object that contains the data required to send an email message (such as the host and user account name). A developer would prefer to use a string of the form UserName=administrator,Password=r1l0k1l3y,Host=localhost to configure the mail settings and just let the container take care of the conversion.

namespace ExampleNamespace
{
    public sealed class MailSettings
    {
        private string _userName;
        private string _password;
        private string _host;

        public string Host
        {
            get { return _host; }
            set { _host = value; }
        }

         public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
    }

    public sealed class MailSettingsConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
         {
            if (typeof (string) == sourceType)
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string text = value as string;
            if(text != null) 
            {
                MailSettings mailSettings = new MailSettings();
                string[] tokens = text.Split(',');
                for (int i = 0; i < tokens.Length; ++i)
                {
                    string token = tokens[i];
                    string[] settings = token.Split('=');
                    typeof(MailSettings).GetProperty(settings[0])
                        .SetValue(mailSettings, settings[1], null);
                }
                return mailSettings;
            }
            return base.ConvertFrom(context, culture, value);
        }
    }
    
    // a very naieve class that uses the MailSettings class...
    public sealed class ExceptionLogger
    {
        private MailSettings _mailSettings;
        
        public MailSettings MailSettings {
        {
            set { _mailSettings = value; }
        }
        
        public void Log(object value)
        {
            Exception ex = value as Exception;
            if(ex != null) 
            {
                // use _mailSettings instance...
            }
        }
    }
}

The attendant XML configuration for the above classes would be...

  <object id="emailingExceptionLogger" type="ExampleNamespace.ExceptionLogger, MyAssembly">
    <property name="MailSettings" value="UserName=administrator,Password=r1l0k1l3y,Host=localhost" />
  </object>
  <object id="customConverterConfigurer" type="Spring.Objects.Factory.Config.CustomConverterConfigurer, Spring.Core">
    <property name="CustomConverters">
      <dictionary>
        <entry key="ExampleNamespace.MailSettings, MyAssembly">
          <object type="ExampleNamespace.MailSettingsConverter, MyAssembly" />
        </entry>
      </dictionary>
    </property>
  </object>

Requirements

Namespace: Spring.Objects.Factory.Config

Assembly: Spring.Core (in Spring.Core.dll)

See Also

CustomConverterConfigurer Members | Spring.Objects.Factory.Config Namespace | IObjectFactoryPostProcessor | IApplicationContext | RegisterCustomConverter