Using a parameterized constructor when resolving an interface

Apr 1, 2008 at 11:36 AM

I was wandering if the following is possible.

My business object encapsulates a mail component implementing the IMailer interface.

When I run a unit test the business object should use a stub/mock implementation of the interface (MockMailer).
When I run the production code the real implementation should be used (Mailer).

In my design the Mailer class has a constructor which initializes the mailer.

Is it possiblle to call this constructor?

This is the code I'm using:
UnityContainer c = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
IMailer mailer = c.Resolve<IMailer>();

The code above works when I don't use any constructors. If I use constructors the constructors are not called and I get an error when I call the Send method because it uses the members which should be initialized in the constructor.

Apr 1, 2008 at 2:13 PM
If you're using configuration to setup your dependencies, you should have a look at the <typeConfig> section.

If you decide you'll use code, you should look at the InjectedMembers extension.

In the source code, the ConfiguringInjectionConstructor.config contains plenty of good examples on how to setup constructor through configuration.