Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Activator For Decorator, Does Not Invoke Dependency's Factory if It Has Conditions #279

Open
falamarzijahromi opened this issue May 17, 2021 · 5 comments

Comments

@falamarzijahromi
Copy link

falamarzijahromi commented May 17, 2021

I'm trying to use the decorator feature with ctor param by using Castle Dynamic Proxy for generating dynamic proxy types as decorators for using them as interceptors. When I Export the dependency by factory under any conditions , it doesn't work. But there's no problem if the dependency does not have any conditions.

` block
.ExportDecorator(decoratorType)
.As(tServiceType)
.WithCtorParam<GenericCastleInterceptor, IInterceptor[]>(i => new IInterceptor[] { i });

      block
            .ExportFactory<IServiceProvider, GenericCastleInterceptor>(sp =>
            {
                return new GenericCastleInterceptor(applicationInterceptorsTypes, sp);
            })
            .As<GenericCastleInterceptor>()
            .When.InjectedInto(type =>
            {
                return true;
            })
            .When.MeetsCondition((strategy, context) =>
            {
                return true;
            });

`

or

` block
.ExportDecorator(decoratorType)
.As(tServiceType)
.WithCtorParam<GenericCastleInterceptor, IInterceptor[]>(i => new IInterceptor[] { i });

      block
            .ExportFactory<IServiceProvider, GenericCastleInterceptor>(sp =>
            {
                return new GenericCastleInterceptor(applicationInterceptorsTypes, sp);
            })
            .As<GenericCastleInterceptor>()
            .When.InjectedInto(decoratorType);

`

The above code snippets do not work whether using InjectedInto or hacking with MeetsCondition to make conditions for exporting. But the code snippet below works well.

` block
.ExportDecorator(decoratorType)
.As(tServiceType)
.WithCtorParam<GenericCastleInterceptor, IInterceptor[]>(i => new IInterceptor[] { i });

      block
            .ExportFactory<IServiceProvider, GenericCastleInterceptor>(sp =>
            {
                return new GenericCastleInterceptor(applicationInterceptorsTypes, sp);
            })
            .As<GenericCastleInterceptor>();

`

@falamarzijahromi falamarzijahromi changed the title Type Activator For Decorator, Does Not Invoke Dependency's Factory if The It Has Conditions Type Activator For Decorator, Does Not Invoke Dependency's Factory if It Has Conditions May 17, 2021
@ipjohnson
Copy link
Owner

Hi @falamarzijahromi

When you use just the MeetsCondition method is the method being called to test if it meets the condition? If so can you look at the static injection information and look to see what types it's being injected into?

I'm not able to replicate the problem but I also don't have the same sets of dependencies so I'm not sure what's going on.

falamarzijahromi added a commit to falamarzijahromi/Grace that referenced this issue May 17, 2021
@falamarzijahromi
Copy link
Author

falamarzijahromi commented May 17, 2021

Hi @ipjohnson
Thanks for your Quick response. I've submitted a PR to replicate the problem by test:
#280

I think the problem is StaticInjectionContext doesn't have any information about the dependent object which is activating the dependency for. Therefor InjectedInto's test does not invoke during activation. I've also added a test which shows MeetsCondtion method is called to test the condition.

@ipjohnson
Copy link
Owner

Hi @falamarzijahromi

Decorated classes never know they are being decorated and the information isn't passed onto the conditional logic. The examples you gave aren't injecting into anything so the injected into information is going to be blank.

@falamarzijahromi
Copy link
Author

falamarzijahromi commented May 20, 2021

Hi @ipjohnson

Sounds reasonable for the layers of abstraction in the operational code. But is it ok for injecting contexts too?

Practically, the service is going to be decorated will be injected into the decorator. By having this point of view, behaving decoration is some like injection, Therefore injecting interfaces should work there too.

@ipjohnson
Copy link
Owner

Hi @falamarzijahromi

I understand where you're coming from but ultimately it's not how it was implemented. Currently when a dependency is located we find a registration that matches and then decorate it. So the conditional logic for selecting a dependency is run then decorators are applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants