-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
Decorator support? #157
Comments
Ok, this may be useful. This may require a little work on the core functionality, so let me consider it. However, I may prefer to make this like a plugin. |
I'm also interested in this! This is great to attach functionality to existing code without changing it. Keeping it SOLID. |
Any update? |
In #372, I am trying to add an interface that will allow the ConotainerBuilder to be modified. public void RegisterDecorator(Type interfaceType, Type decoratorType)
{
for (var i = 0; i < Count; i++)
{
var entry = this[i];
var isTarget = entry.ImplementationType.IsInterface &&
entry.ImplementationType == interfaceType;
if (entry.InterfaceTypes is { } interfaceTypes)
{
foreach (var t in interfaceTypes)
{
if (t == interfaceType)
{
isTarget = true;
break;
}
}
}
if (isTarget)
{
this[i] = new DecoratorRegistrationBuilder(this[i], decoratorType);
}
}
} public class DecoratorRegistrationBuilder : RegistrationBuilder
{
readonly RegistrationBuilder inner;
readonly Type decorateType;
public DecoratorRegistrationBuilder(RegistrationBuilder inner, Type decoratorType)
: base(decoratorType, inner.Lifetime)
{
this.inner = inner;
decorateType = inner.InterfaceTypes != null ? inner.InterfaceTypes[0] : inner.ImplementationType;
InterfaceTypes = inner.InterfaceTypes;
As(decorateType);
}
public override Registration Build()
{
var injector = InjectorCache.GetOrBuild(ImplementationType);
var innerRegistration = inner.Build();
var provider = new FuncInstanceProvider(container =>
{
var innerInstance = container.Resolve(innerRegistration);
var parameters = new IInjectParameter[Parameters == null ? 1 : Parameters.Count];
Parameters?.CopyTo(parameters);
parameters[parameters.Length - 1] = new TypedParameter(decorateType, innerInstance);
return injector.CreateInstance(container, parameters);
});
return new Registration(ImplementationType, Lifetime, InterfaceTypes, provider);
}
} There are several considerations for this 🤔
|
any news on this? |
Any update? |
... + |
working in #625 |
Scrutor library adds a cool decorator feature for
Microsoft.Extensions.DependencyInjection
. Is it possible to provide a similar thing here?https://github.com/khellang/Scrutor
The text was updated successfully, but these errors were encountered: