Kabua
Kabua

Reputation: 1019

LightInject - How to Register Multiple Interfaces to a Single Service?

How do I register a service that implements 4 interfaces?

For example: class Foo : IFoo, IBar, IApp, ISee { ... }

I was hoping for something like this: container.Register<IFoo, IBar, IApp, ISee, Foo>();

But it appears this signature is for passing various types into a factory, in this case a factory that takes 4 parameters.

Upvotes: 1

Views: 591

Answers (1)

Kabua
Kabua

Reputation: 1019

For those how also have this same question. Here is one possible way of solving it:

container.Register(_ => new Foo(), new PerScopeLifetime());
container.Register<IFoo>(factory => factory.GetInstance<Foo>());
container.Register<IBar>(factory => factory.GetInstance<Foo>());
container.Register<IApp>(factory => factory.GetInstance<Foo>());
container.Register<ISee>(factory => factory.GetInstance<Foo>());

In my specific case I also need to ensure that there was only one instance of Foo() within each scope. I.e. web request.

Upvotes: 3

Related Questions