views:

354

answers:

3

I have a class with two ctors. One parameterless and one with parameters.
Unity will by default take the gready approach and go for the last ctor.

How can I define what ctor to use (I want to parameterless) without adding dependency on Unity within my classes? I think it is possible to do it in my container creation, but I don't know how.

Currently my registration entry looks like this:

container.RegisterType<IConfigurationService, SqlConfigurationService>()

UPDATE
I'm trying to avoid programming in XML (config file) as much as possible.

EXTRA
How would registering a constructor with one parameter (which in it's turn should be injected)?
Say ILoggerService is already registered and I would want to use the constructor

public SqlConfigurationService(ILoggerService logger){}
A: 

I think you can also pass in an InjectionConstructor (including ResolvedParameter<T>s for the parameters) in your registration. Have you tried that?

Botz3000
+1  A: 

I don't have Unity nearby right now, but as far as I recall, you can do something like this:

container.RegisterType<IConfigurationService, SqlConfigurationService>(
    new InjectionConstructor())
Mark Seemann
Thanks :) Do you per chance also know how to solve my extra question (bottom of OP)?
borisCallens
Total guess after looking a bit at the API docs: `container.RegisterType<IConfigurationService, SqlConfigurationService>(new InjectionConstructor(new ResolvedParameter<ILoggerService>()));`
Mark Seemann
A: 

You can apply the InjectionConstructor attribute to the constructor you want to use. This does have the disadvantange of moving some container configuration away from a central location however.

Lee
I don't want to add any dependency in my actual classes (ref OP)
borisCallens