1

I am new developer using C# and I am working on a project which uses C# Unity for Dependency Injection. So the current code is like this

ISomeInterface var = new SomeConcreteType(param);
ContainerFactory.Container.RegisterInstance<ISomeInterface>(var);

Now, I want to have var1 and var2 which are of type ISomeInterface. I read online the InjectionFactory can help in this case but I am not able to figure out how that works.

Can someone explain to me how InjectionFactory works?

Like if I do this

 ContainerFactory.Container.RegisterType<ISomeInterface>(
            new InjectionFactory(
                container => new SomeConcreteType(param1)));

ContainerFactory.Container.RegisterType<ISomeInterface>(
            new InjectionFactory(
                container => new SomeConcreteType(param2)));

How does then the Unity decide which one of the SomeConcreteType to inject.

1
  • I'm not familiar with Unity, but usually there is some registration overload which takes a string as parameter as well, so you can name your instance. You can then later retrieve the instancy by that name. Commented Sep 16, 2022 at 7:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.