Grace icon indicating copy to clipboard operation
Grace copied to clipboard

[Question]: Can I export decorators with Attributes

Open shahabganji opened this issue 5 years ago • 10 comments

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{ } 
  • currently I wrote a hand-written method which creates factories using reflection. (borrowed from Vladimir Khorikov's CQRS In Practice course)

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?

shahabganji avatar Mar 29 '20 13:03 shahabganji

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));

ipjohnson avatar Mar 29 '20 14:03 ipjohnson

@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));

shahabganji avatar Mar 29 '20 15:03 shahabganji

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.

ipjohnson avatar Mar 29 '20 15:03 ipjohnson

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?

shahabganji avatar Mar 29 '20 15:03 shahabganji

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

ipjohnson avatar Mar 29 '20 15:03 ipjohnson

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

ipjohnson avatar Mar 29 '20 15:03 ipjohnson

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 🙂

shahabganji avatar Mar 29 '20 15:03 shahabganji

I've pushed out a nuget package with both changes

ipjohnson avatar Apr 04 '20 15:04 ipjohnson

I’ll check it tonight. 🙏🏻👍🏻

Am 04.04.2020 um 20:25 schrieb Ian Johnson [email protected]:

 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 avatar Apr 04 '20 16:04 shahabganji

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

shahabganji avatar Apr 05 '20 10:04 shahabganji