Mulitple Constructors

Apr 24, 2008 at 7:50 PM

If a class has multiple constructors such as the following:

public PhraseDaoCache()
_CurrentCache = System.Web.HttpContext.Current.Cache;

public PhraseDaoCache(Cache currentCache)
_CurrentCache = currentCache;

With the minimal amount of configuration possible, (and definitely no config information for System.Web.Caching.Cache) I would expect for the simplest constructor to be used. However, this doesn't appear to be the case. How does unity determine which ctor to use by default?

Apr 24, 2008 at 9:45 PM
Edited Apr 24, 2008 at 9:46 PM

How Unity Resolves Target Constructors and Parameters

When a target class contains more than one constructor, Unity will use the one you decorate with the InjectionConstructor attribute. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one such constructor (more that one of the "longest" with the same number of parameters), Unity will raise an exception.
Apr 24, 2008 at 10:52 PM
Edited Apr 24, 2008 at 11:07 PM
Any idea offhand how it resolves the Cache object to inject? That also seems a little odd.

Thanks for your quick response.

Apr 24, 2008 at 11:41 PM
It goes looking for public constructors for the Cache object, and if it finds any it follows the same rules.

If you want to call a specific constructor, you'll need to configure via the API or the config file.