ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

[Question]: Can I export decorators with Attributes #253

Open shahabganji opened 4 years ago

shahabganji commented 4 years ago

Consider a situation where I have a default implementation for an interface and some decorators, now I could use grace to register my decorators like the following:

public void ConfigureContainer(IInjectionScope injection)
        {
            injection.Configure(c =>
            {
                c.ExportDecorator<ISomeInterface>(
                    request => new AuditDecorator(request));
            });
        }

Is there any possible solutions to do:

[Audit]
public SomeImpl : ISomeInterface {
// omitted code
}

public class AuditDecorator : ISomeInterface{

    public AuditDecorator(ISomeInterface next)
    }
}

// flag attribute to use to map to the decorator
public class AudtiAttribute : Attribute{ } 

I don't know how efficient this is, I want to know is there any way to give all the boilerplate code to the IoC?

ipjohnson commented 4 years ago

So essentially only apply the AuditDecorator when the implementation is attributed with the AuditAttribute?

If that's the case you can use the When.MeetsCondition method to decide when the decorator should be applied.

You could do something like this

.When.MeetsCondition((strategy,staticContext) => strategy.ActivationType.GetCustomAttributes().Any(a => a is AuditAttribute));
shahabganji commented 4 years ago

@ipjohnson Thanks for the prompt reply.

That is exactly the case, but I am afraid that c.ExportDecorator<ISomeInterface> has the return type of void and one cannot indicate the When conditions. here and its implementation

-------- EDIT -------

But the followin has worked:

c.ExportDecorator( typeof(AuditDecorator))
                    .When.MeetsCondition(
                        (strategy, staticContext)
                            => strategy.ActivationType.GetCustomAttributes(false)
                                .Any(a => a is AuditAttribute))
                    .As(typeof(ISomeInterface));
ipjohnson commented 4 years ago

Ok glad that worked with slightly different syntax. I'll open an issue up to add When configuration to the ExportDecorator func. That's just an over sight on my part.

shahabganji commented 4 years ago

Is there any way to change the order of decorators as well? for instance based on the way the attributes are applied to the concrete class?

ipjohnson commented 4 years ago

For the moment it's going to be dependent on registration order. That said this seems like a reasonable feature to ask for.

ipjohnson commented 4 years ago

I should have time in the coming week to implement these two features and get a release out.

shahabganji commented 4 years ago

That is great 💪 👍 and could you please label this issue as question so that other devs might better find it if they have the same problem 🙏

And thank you for the great support 🙂

ipjohnson commented 4 years ago

I've pushed out a nuget package with both changes

shahabganji commented 4 years ago

I’ll check it tonight. 🙏🏻👍🏻

Am 04.04.2020 um 20:25 schrieb Ian Johnson notifications@github.com:

 I've pushed out a nuget package with both changes

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

shahabganji commented 4 years ago

@ipjohnson I checked both and it looks great 👍 Thanks for the time and effort.