grumpydev/TinyIoC

UsingConstructor() not working

Closed this issue · 2 comments

I have a class with a constructor that takes 3 arguments:

  public CrmDbCommand(CrmDbConnection connection, IOrgCommandExecutor crmCommandExecutor, IOrganisationCommandProvider organisationCommandProvider)
        {

When an instance is resolved, I want TinyIoC to pass in null as the first argument into the constructor, but still resolve the other 2 arguments as normal.

So here is my registration:

            container.Register<CrmDbCommand>().UsingConstructor(() => new CrmDbCommand(null as CrmDbConnection, container.Resolve<IOrgCommandExecutor>(), container.Resolve<IOrganisationCommandProvider>()));

However when I do:

container.Resolve<CrmDbCommand>();

The outcome is that it doesn't invoke the constructor that I registered, so it does not pass in null as the first argument as desired.

Any advice / pointers much appreciated :)

UsingConstructor is just to tell Tiny what constructor to use, it's not a factory delegate (which is what you want). Factory delegates should be in the docs, essentially you register it with a func<tinyioccontainer, T> and it will call that to resolve.

Got ya! Cheers!