-
Notifications
You must be signed in to change notification settings - Fork 29
Is it possible to inject from the interface of Composition itself? #33
Answered
by
NikolayPianikov
YoshihiroIto
asked this question in
Q&A
-
It seems possible to inject the Composition object itself (I looked for documentation but couldn't find it, maybe I'm looking in the wrong place).
Is it possible to inject from the interface of Composition itself?
I have written a sample below.
(A)Prepare interface IComposition
(B)specified in Composition
(1) works correctly
(2) will result in an error.
using Pure.DI;
var composition = new Composition();
var service1 = composition.CreateService(param1: "ABC", param2: 123, param3: "aaaa");
var service2 = composition.CreateService(param1: "XYZ", param2: 789, param3: "bbbb");
var otherService1 = composition.CreateOtherService(param1: new object(), param2: typeof(int));
var otherService2 = composition.CreateOtherService(param1: "vvv", param2: typeof(float));
interface IComposition // <---(A)
{
IService CreateService(string param1, int param2, string param3);
IOtherService CreateOtherService(object param1, System.Type param2);
}
sealed partial class Composition : IComposition // <---(B)
{
private void Setup()
{
DI.Setup("Composition")
.Bind<IDependency>().To<Dependency>()
.RootArg<string>("param1")
.RootArg<int>("param2")
.Bind<IService>().To<Service>().Root<IService>("CreateService")
.RootArg<object>("param1")
.RootArg<Type>("param2")
.Bind<IOtherService>().To<OtherService>().Root<IOtherService>("CreateOtherService")
.RootArg<string>("param3", "another string");
}
}
interface IDependency
{
}
class Dependency : IDependency
{
}
interface IService
{
public IDependency Dependency1 { get; }
public IDependency Dependency2 { get; }
public string Param1 { get; }
public int Param2 { get; }
}
class Service : IService
{
public Service(
Composition composition1, // <---(1)
IComposition composition2, // <---(2)
IDependency dependency1,
IDependency dependency2,
string param1,
int param2,
[Tag("another string")] string param3)
{
Dependency1 = dependency1;
Dependency2 = dependency2;
Param1 = param1;
Param2 = param2;
}
public IDependency Dependency1 { get; }
public IDependency Dependency2 { get; }
public string Param1 { get; }
public int Param2 { get; }
}
interface IOtherService
{
object Param1 { get; }
Type Param2 { get; }
}
class OtherService : IOtherService
{
public OtherService(
object param1,
Type param2)
{
Param1 = param1;
Param2 = param2;
}
public object Param1 { get; }
public Type Param2 { get; }
}
Beta Was this translation helpful? Give feedback.
All reactions
Answered by
NikolayPianikov
Aug 16, 2023
Sorry for delay, I'm away from my computer right now. Try adding a binding:
Bind<IComposition>().To(ctx => { ctx.Inject<Composition>(out var comosition); return composition; }
Replies: 1 comment 1 reply
-
Sorry for delay, I'm away from my computer right now. Try adding a binding:
Bind<IComposition>().To(ctx => { ctx.Inject<Composition>(out var comosition); return composition; }
Beta Was this translation helpful? Give feedback.
All reactions
1 reply
-
Thank you very much.
I was able to solve this problem.
Beta Was this translation helpful? Give feedback.
All reactions
Answer selected by
NikolayPianikov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment