Introduction to AOP in C#
Vložit
- čas přidán 8. 12. 2022
- In this session, I'm going to show you how you can leverage DispatchProxy in C# to implement AOP.
This capability will allow you to add layers or aspects on top of your existing implementation of the functionality of any routine without polluting your business logic code with side-routines like tracing and logging.
Here's some useful links:
Demo Code:
github.com/hassanhabib/AOPDemo
DispatchProxy in .NET Core Blog:
devblogs.microsoft.com/dotnet...
Great never done it this way but will keep this in my knowledge bank. I have always used something like autofac and interceptors to achieve this kind of implementation.
awesome my prof , we need more thanks a lot for this Chanel
Very nice presentation
Yeah, that's great. It triggered my inspiration. Thanks for the video.
Wow thank you so much, this changed MY LIFE!! amazing.
Thank you so much - I appreciate you!
Thanks a lot, Now I can customise my aspnet core middleware.
This video is supper intersting and an amazing
Nice. Next time, show us the performance implications of using DispatchProxy vs. not using it. I'll implement this myself but it would've been nice to see here also.
Good idea. I will certainly consider. thanks.
Without dispatch: 0.4291 ns (no memory allocation)
With dispatch: 13.4429 ns (24 B memory allocation)
@@belmirp wow. That is a huge difference!
Thank you Hassan!
Helpfull one👍
Awesome, always wondered how the magic behind EF lazy loading proxies works.
very helpful. kindest regards
Let's not forget the Attributes in C# that are a good implementation of AOP, and have been there forever.
Awesome!!!
A minor correction on `unmanaged`, it's all primitive types and user-defined types that only use other unmanaged types, including other user-defined types. It's basically a way to say "this doesn't use managed memory objects like `ref` fields or reference types (classes, interfaces).
Thanks a ton 🙏
I am reminded of interception via decorator pattern.
Personally I'd avoid using such kind of implementation for any serious development where performance matters. Probably a good evolution in this field for C# could be given by source generator, I'm wondering if there is already any source generator that implements decorator patterns, could be an interesting project to play with
Metalama from PostSharp already does this. I got my eye on it, but haven't had a chance to try it yet.
amazing 5 years ago its really hard to create aop
A ready-made implementation of the proxy pattern?)
Is also not generic. This may not be good for performance.
I'm talking about boxing for passing arguments to Invoke.
Same with the return value.
It will be very sad to call the proxy method in a loop for a large number of values)
how do you inject a service into another service? Like, if I have a service calling a API but I need to get the API key from a database?
Hi Hasan
Thank you for the video. Is it possible to wrap a class with multiple proxies? Let's say, in your example, if you wanted two interceptors, one for logging and another to inspect the parameters and omit any PII information. Would the two proxies wrap the IStudentService OR one would wrap the IStudentService and another would wrap this Proxy instead of IStudentService? (Later would be like Proxy of Proxy)
BTW, for now we use MediatR and achieve such injection through Behaviors.
Hello, Hassan
Thanks for the tutorial, it's awesome and really helped me
I got a problem with it when I'm using the app on Android. I used DispatchProxy for my project in unity and want to distribute the app on mobile and when I try to call the Create method, it says: PlatformNotSupportedException: Operation is not supported on this platform.
How do I fix this or what are the alternatives? Because DispatchProxy is ideal for my needs
Can be a good way for benchmarking, but I think maybe reflection would lead to inaccurate result as reflection is slow. Is it so ?
Give it a shot. Sounds like a good piece of knowledge to have. Let us know.
@@HassanHabib because reflection happens with each call, yes very much will impact performance. So not very great for benchmarking (jsut use BenchmarkDotNet) However a constant overhead is pretty acceptable for doing performance analysis! No dotnet profiling tool I know will report outliers (only reporting averages) which has been a problem I've run into with gamedev (a render function takes 1ms normally, but 2000ms once every 10 seconds) So this is a great way to build a profiler for finding outliers. Thank you so much for the into to it Hassan. Great, very easy to understand video.
@@JasonAtNovaleaf good point. Something we must consider when leveraging this capability.
This is golden! This can be useful in a nuget package that I am writing! However can this be unit tested?
Great question. Off the top of my head - if you can inject a Mock of a func in your Proxy you can certainly verify it has been called.
@@HassanHabib Thank you mate! Hope your well! I would like to thank you for your hard work and making me a stronger developer each and everyday! Looking forward to more of your videos my man 🤟.
Thanks for sharing, even though I feel this is too advanced for me lol
What software is that for creating diagrams?
It's called draw.io it's free and open source and also available on both desktop and web.
@@HassanHabib Thank you. I started to write one the other day and knew there had to be plenty out there.
Seems related to the Decorator Pattern..
It is. Correct.
Appears this method does not support recursive functions. Only top-level calls are intercepted. Is this true, or I'm missing something?
I noticed this too. There might be a way around it - I'm not sure.
can this be used to guard certain methods with requireing permissions?
Give it a shot and let us know.
@@HassanHabib how do you recommend doing it with DI. usually the other service is already registered and you need to override it to get the proxy as your implementation
@@garcipat possibly on registration. So instead of registering the original concrete object you register the proxy
To my understanding, this is another way of implementing the decorator pattern. if not what is the difference here?!
and if this is like the decorator pattern, it must be slower because of the reflection involvement. .🤔🤔
what did I miss?
Correct. 100%.
In terms of perf, I haven't really ran it extensively against something for perf. So I don't honestly know.
WoW! That's a rad monitor you got behind you! What's the make and model? I want one! LoL!
That's the Samsung Odyssey G9 49"
@@HassanHabib I've been seeing reviews where people has reported white pixel issues and white vertical line after a few months. Have you experienced any of these problems with this monitor?
@@thefront5871 Not at all. I have 3 of these monitors btw, and gave one away. All good here.
Looks like this only works with interfaces and uses reflection.
Now imagine you have 500 classes in your project.
Now you need 500 interfaces for all those classes, and then it uses reflection to call everything.
Great if you wanna annoy the next Dev who will take over your project in 6 months.
Wait, don't you already create the interfaces anyway to abstract implementation of your services?
@@henriquesouza5109 I only use interfaces when i have to. They obscurify the code.
i need help with this, if anyone can help please just respond to this comment!
Thanks fo sharing. The repo is gone.
Oh it was set to private. Now it's public. thanks for letting me know.
@@HassanHabib Thanks. I just have a question, imagine I have one more service like ISchoolService I'd like inject this service in StudentService how can I do because I see a "new" in program.cs (line 10), how can I use DI in this case.
@@christianista You can either inject it in the original declaration of the service or pass it as a parameter to the `Create` function.
Can't wait to see when dotnet finally become java enterprise edition 😆
Thanks, I've learnt something, but I really hope I'll never have to use this abomination.