Specify default parameterless constructor in XML

Apr 5, 2009 at 12:18 PM
Hi,

I'm tearing my hair out over this and many, many searches in google hasn't helped.

I'm trying to override the behaviour of Unity to get the constructor with the most parameters and know I can do it via an attribute or by registering the contructor at run time. Unfortunately neither of these are acceptable as:
  • Attribute means I tie myself into Unity, which at the moment is cleanly hidden behind an interface. I will not expose unity to the business logic
  • Registering the constructor means the class doing the registering needs access to the base class which screws the model of programming to an interface (and really messes up when I start using Mocks as I need to pollute real code with a reference to the Mock dll)
So, I want to do it in XML but all the xml examples I have are where you specify an injector and pass a value - which is not what I want. I can't work out or find an example of an XML constructor element that forces unity to use a default parameterless one. If this isn't possible then I'm stunned... what an earth were MS thinking?

Can someone help me out please?

Apr 6, 2009 at 1:42 AM
Well, in the XML you can just do:

<type type="IWhatever" mapTo="ImplementingClass">
  <typeConfig>
    <constructor />
  </typeConfig
</type>

Is that what you wanted?

Apr 15, 2009 at 9:55 AM
Thank you!

How hard would it have been for MS to have put this example in the MSDN documentation in the "Source Schema for the Unity Application Block", Constructor element - especially when the documentation states a constructor has "one or more child elements named param", when obviously I now know it can have "zero or more child elements named param".

Sheesh.

Apr 16, 2009 at 11:20 PM
We had a new writer who wasn't familiar with the project, an API that was highly in flux at the time, and a very large and complex schema to document. Sometimes things get missed.
Apr 17, 2009 at 2:08 PM
I have the same problem using a Queue:

<

 

type name="commandQueue" type="Queue">
    <
lifetime type="singleton"/>
        <
typeConfig>
            <
constructor/>
        </
typeConfig>
</
type>

 

But it always gives me the error message: "The parameter capacity could not be resolved when attempting to call constructor System.Collections.Queue(System.Int32 capacity, System.Single growFactor).".
Any ideas?