6

Implementing the decorator pattern using System. Reflection.DispatchProxy

 6 months ago
source link: https://bartwullems.blogspot.com/2024/02/implementing-decorator-pattern-using.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Implementing the decorator pattern using System. Reflection.DispatchProxy

Implementing the decorator pattern using System. Reflection.DispatchProxy

If you are new to the decorator pattern, let me start with a short explanation:

Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

A common use case for decorators is to implement Aspect Oriented Programming(AOP) which allows you to implement cross-cutting concerns like logging, caching, …

-0iMZbMWESmqUvqDAAkutqIjoqw0ErVKaZq-bFltx2eZITyS78rdLt7rijLCNnqgrSekF1ZK3QN4UpgYJfqqixo86RzkjSuqEjZM5q0EaciPnTfQg80qkzpwcA0A4gAX-owYVfhEv3ahxkIkNlo4ZPn3YBXbWsiqMqzMqxcFmTXwVuGJ2cNOPulYM7lKNqXEEOpViMMcpjXz7ddvflt8olOl2wm4ylSP9dvUn36HsAnwk0PG1TOXuaWT75lSFL-v4gpdnBXTmnAukJezukHI_9BJi8mFSiNXGByzUFBdVeppPxOB3tz2dt2JMds_lLd3iDG8Hves1QB5mC4GKCWjYn0PZlRP_bg0Q3etSNkFd_F2SCf-7tqvBZJzmVh3wE5omlO-AD828qkf-5aHPddfLqnSSBTkOH18KwbGFApgabXeocTMmqBSZ3b9sOx30ia8XyQlab4sYut8JdUSToCM9Jen-EvWXENHsAAqIrJXB1nsQU_NdWwT3PZe6Xui2xO0SmvpnS7kCStVWfK9m_byMLlRbJ7HyBFENHzgf1EwKaYfs-nPtLi6qc--aiiHSM41uGgUN_JO5N0m8zcv_8jtnXMmVCDIKDivPche8cheQsDFHwIkVqDC0pS_I2woQAlTjmxrGCvLgicw7RGP6Du8zXroggorRVkko59Bpr_5DHOVZplTbqx6zqxNlBaJEplLEDu_BFVLMNvP3GtUYQHAek36DmAQXguYPmLOUy6S7OaX1XczaBmRcy7Wva0Rn4ErRExJ4HGpbtwuiBR9AYTC2Qgy2PEwajpbuefQrv6-QbPz_mlrgqop8uqLjG1ylOxjBob6A_iju2GUZefRC9Qmx8DwmoraN6puEzuh_Z6AjYnvjxP3AOst5pApyKkuob-D29dc5wgHEkS2HIfwHfMtwjAmf45co1alMgFTXK9AnbQQ=w1280-h800-s-no?authuser=0

There are multiple ways to implement this pattern, you can manually implement it, use your DI container, use a source generator to write the boilerplate code or use a dynamic proxy that wraps call to the original class.

It’s this last approach I want to focus on in this blog post. You could use the great  Castle.DynamicProxy library but for simpler use cases, there is a built-in alternative through the System.Reflection.DispatchProxy class.

Let’s have a look at a small code example on how to use this class.

First we need to create a Decorator class that implements DispatchProxy:

Remark: Be aware that we cannot use constructor injection to inject parameters(like the logger in the example above).

We could now use the DispatchProxy.Create() method but I typically create a static method that allows me to wrap an existing class instance

Let's create an interface, corresponding class and apply the decorator:

If we now run this code, the result looks like this:

Decorator.png

Sweet!

There are some pros and cons when using this approach:

  • Pros
    • Works with .NET Standard 1.3+ (so it works with both .NET Framework 4.6+ and .NET Core/.NET 5).
    • No 3th party libraries needed.
    • Proxies by wrapping the target object, so a proxy can be created around an already existing object.
  • Cons
    • Proxies interfaces, not classes, so proxied types must implement an interface and access to any members not in the interface (like fields) is complicated.
    • Uses reflection, so not usable with AOT.
    • No support for async methods/functions.

More information

DispatchProxy Class (System.Reflection) | Microsoft Learn


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK