Constructor with null values

May 12, 2011 at 6:35 AM
Edited May 12, 2011 at 6:36 AM

Is it possible to resolve services with null values in constructor?

 

Container.Resolve<IMyService>(new ResolverOverride[] {new ParameterOverride("paramName", null)}); //ArgumentNullException!!!
May 12, 2011 at 4:49 PM

Use InjectionParameter instead. As what is also advised by the ArgumentNullException.

container.RegisterType<IMyInterface, InterfaceImplementer>(new InjectionConstructor(new InjectionParameter(typeof(object), null)));

Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 12, 2011 at 10:54 AM

It works, thanks!

Jun 24, 2011 at 11:48 AM
AvanadeSupport wrote:

Use InjectionParameter instead. As what is also advised by the ArgumentNullException.

container.RegisterType<IMyInterface, InterfaceImplementer>(new InjectionConstructor(new InjectionParameter(typeof(object), null)));

Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

How I can do this in XML-config (unity 2.0) file ?

Jun 26, 2011 at 4:20 PM

Something like this

     <register type="SamplePost2.IMyInterface, SamplePost2" mapTo="SamplePost2.InterfaceImplementer, SamplePost2">
        <constructor>
          <param name="message" value="This is a sample constructor injection . . . " />
        </constructor>
      </register>

While I have my constructor as

        public InterfaceImplementer(string message){            
            Console.WriteLine(message);            
        }

Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 27, 2011 at 7:29 AM
Edited Jun 27, 2011 at 7:29 AM
AvanadeSupport wrote:

Something like this

     <register type="SamplePost2.IMyInterface, SamplePost2" mapTo="SamplePost2.InterfaceImplementer, SamplePost2">
        <constructor>
          <param name="message" value="This is a sample constructor injection . . . " />
        </constructor>
      </register>

While I have my constructor as

        public InterfaceImplementer(string message){            
            Console.WriteLine(message);            
        }

Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Thanks!

But, how will look XML-config for this type?

public InterfaceImplementer(System.Net.IPEndPoint ep)
{ }
Jun 28, 2011 at 11:18 AM

Then you will need to use a TypeConverter to achieve this. Something like

App.config

<register type="SamplePost2.IMyInterface, SamplePost2" mapTo="SamplePost2.InterfaceImplementer, SamplePost2">
        <constructor>
          <param name="ep" type="System.Net.IPEndPoint, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <value value="Test" typeConverter="SamplePost2.MyTypeConverter, SamplePost2"/>
          </param>
        </constructor>
      </register>

My Type Converter

    public class MyTypeConverter : TypeConverter
    {
        public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
                    return null;
        }
    }

 

Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 28, 2011 at 11:27 AM

Thank you for your advice.