Scrutor icon indicating copy to clipboard operation
Scrutor copied to clipboard

Suggestion: Missing methods for decorate

Open mjebrahimi opened this issue 6 years ago • 4 comments

It would be great if these methods had been added for Decorator DecorateTransient<>(); DecorateScoped<>(); DecorateSingleton<>();

Currently, both Decorator and the class which is decorating has been registered by the same LifeTime.

Assume that "MyService" class is registered as Singleton and we need to register "MyServiceDecorator" as Scoped. but currently, it's not possible.

I can make the changes and create a pull request if you want

mjebrahimi avatar Jan 16 '19 17:01 mjebrahimi

As far as I can remember, this isn't really possible as we replace the existing descriptor with the decorated ones. That's how we're able to make decoration work at all. See https://github.com/khellang/Scrutor/issues/38. But by all means, give it a try. I'd love to be proven wrong :wink:

khellang avatar Jan 16 '19 20:01 khellang

I read the whole discussion of #38 issue and I believe that this does not cause any problems and it is necessary to do so.

currently, if our decorator has dependencies with a shorter lifetime than itself, that cause captive dependency problem and fixing this problem is impossible because we can not change the lifetime of that decorator.

Basically, the captive dependency problem does not depend on the decorator. This problem occurs when a service has dependencies with a shorter lifetime than itself, even if the Decorator is not used. However, this is a common scenario, especially in decorators.

I think it's better to not change the behavior of Decorate method (to transient by default) to prevent breaking changes. But it's better to add the DecorateScoped, DecorateTransient methods to prevent the captive dependency problem.

It is also better to add the DecorateSingleton method to integrity and avoid confusing, although it does not necessarily, because the lifetime of the Decorator class must be equal or shorter than to the Decoratee lifetime, and if the Decoratee is singleton and we want to Decorator be singleton, there is not different between the Decorate method(default) with the DecorateSingleton.

Anyway, I reviewed the codes and tested this feature and I believe this is possible.

I'm sorry if my English is not good enough

mjebrahimi avatar Jan 19 '19 01:01 mjebrahimi

Anyway, I reviewed the codes and tested this feature and I believe this is possible.

Awesome! Looking forward to the pull request 😎

khellang avatar Jan 21 '19 21:01 khellang

Related PRs: #78, #79 and #80

mjebrahimi avatar Mar 06 '19 10:03 mjebrahimi