Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the parameterless constructor as the injection constructor in container creation

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){}
like image 936
Boris Callens Avatar asked Dec 22 '09 10:12

Boris Callens


1 Answers

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())
like image 128
Mark Seemann Avatar answered Nov 11 '22 09:11

Mark Seemann