Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

When you ask Unity to resolve a type with multiple constructors, by default it picks the constructor with the most parameters, to facilitate maximum dependency injection.

Richard Blewett has a good post on specifying a different constuctor in config, but it doesn't cover the scenario when the constructor you want is the default, parameterless constructor.

The documentation is a bit lacking, although the config is logical - here's how it looks:

 <unity>
    <containers>
      <container>
        <types>
          <typetype="x.y.z.Spec.IInterface,x.y.z"
                mapTo="x.y.z.Impl.Implementation,x.y.z">
            <constructor/> <!-- force use of default constructor -->
          </type>
        </types>
      </container>
    </containers>
 </unity>
Posted on Tuesday, October 4, 2011 10:34 AM Unity | Back to top


Comments on this post: Specifying parameterless constructors in Unity configuration

# re: Specifying parameterless constructors in Unity configuration
Requesting Gravatar...
Thanks for the fix suggestion. I could never write such a script on my own! Twilight. Blog is really informative and entertaining same time. I appreciate the efforts made by Rockets tickets.
Left by balloon tower defense 5 on Feb 03, 2012 7:09 PM

# re: Specifying parameterless constructors in Unity configuration
Requesting Gravatar...
Your blog article is really awesome
Left by Notebooks im Test on Aug 26, 2012 8:01 PM

# re: Specifying parameterless constructors in Unity configuration
Requesting Gravatar...
Very good share The Blog awesome.. very suprised with informations.
Left by Test Notebooks on Aug 26, 2012 8:02 PM

# re: Specifying parameterless constructors in Unity configuration
Requesting Gravatar...
The Blog is really awesome. Thanks for suggestion
Left by Metal Bed Frame Queen on Aug 26, 2012 8:09 PM

# re: Specifying parameterless constructors in Unity configuration
Requesting Gravatar...
I’ve been visiting your blog for a while now and I always find a gem in your new posts.
Thanks for sharing
Left by pornicihd on Oct 21, 2012 3:27 PM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net