dadhi / DryIoc

DryIoc is fast, small, full-featured IoC Container for .NET
MIT License
982 stars 122 forks source link

The issue with registering two service in DryIoc #586

Closed ABatenev closed 10 months ago

ABatenev commented 11 months ago

previously I registered the serviceas follows:

container.Register(

                typeof(IFoo<>),
                 typeof(Foo<>),

                reuse: Reuse.Scoped,

                 made: Parameters.Of.Details((request, p) =>
                     p.ParameterType
                      .GetGenericDefinitionOrNull() == typeof(IFooStrategy<>)
                && (!p.ParameterType
                      .GetGenericParamsAndArgs()
                      .FirstOrDefault()
                     ?.IsAssignableTo<IUndeletable>() ?? false) // ToDo check logic here

                      ? ServiceDetails.Of(value: null)
                      : null)); // the default injection behavior

Here I define which strategy to assign to the model's. Now the task has become more complicated, I have added a second service CompileFoo: IFoo". The base foo needs to be registered with the key FooType.Base, and compile with the key FooType.Compile. Will the following registration work correctly:

container.Register(

                    typeof(IFoo<>),
                     typeof(Foo<>),

                    reuse: Reuse.Scoped,
                    serviceKey: RepositoryType.Base,

                     made: Parameters.Of.Details((request, p) =>
                         p.ParameterType
                          .GetGenericDefinitionOrNull() == typeof(IFooStrategy<>)
                    && (!p.ParameterType
                          .GetGenericParamsAndArgs()
                          .FirstOrDefault()
                         ?.IsAssignableTo<IUndeletable>() ?? false) // ToDo check logic here

                          ? ServiceDetails.Of(value: null)
                          : null)); // the default injection behavior

container.Register(

                    typeof(IFoo<>),
                     typeof(CompileFoo<>),

                    reuse: Reuse.Scoped,

                    serviceKey: RepositoryType.Compile,

                     made: Parameters.Of.Details((request, p) =>
                         p.ParameterType
                          .GetGenericDefinitionOrNull() == typeof(IFooStrategy<>)
                    && (!p.ParameterType
                          .GetGenericParamsAndArgs()
                          .FirstOrDefault()
                         ?.IsAssignableTo<IUndeletable>() ?? false) // ToDo check logic here

                          ? ServiceDetails.Of(value: null)
                          : null)); // the default injection behavior

And how to register the second service so that the first one is always used by default, and the second one is only used if we explicitly specify the serviceKey attribute, while also correctly taking the foo strategy?

dadhi commented 11 months ago

@ABatenev Hi, could you please reformat your code sample, it is hard to read with all those indentations and spacing. And add the code for the expected resolutions and what types you are expecting.

dadhi commented 10 months ago

@ABatenev Moving this issue to the discussion as it looks more like a question.