Introduction to AOP in C#

Sdílet
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...

Komentáře • 64

  • @jwcodendaal
    @jwcodendaal Před rokem +1

    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.

  • @ahmedelgendy5363
    @ahmedelgendy5363 Před rokem

    awesome my prof , we need more thanks a lot for this Chanel

  • @billias28
    @billias28 Před 9 dny

    Very nice presentation

  • @DasturlashniOrganamiz

    Yeah, that's great. It triggered my inspiration. Thanks for the video.

  • @linguini6232
    @linguini6232 Před rokem +1

    Wow thank you so much, this changed MY LIFE!! amazing.

    • @HassanHabib
      @HassanHabib  Před rokem +1

      Thank you so much - I appreciate you!

  • @MihaiMoisei
    @MihaiMoisei Před rokem

    Thanks a lot, Now I can customise my aspnet core middleware.

  • @mukhammadkodir_alijonov
    @mukhammadkodir_alijonov Před rokem +1

    This video is supper intersting and an amazing

  • @ParkImaging
    @ParkImaging Před rokem +9

    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.

    • @HassanHabib
      @HassanHabib  Před rokem +5

      Good idea. I will certainly consider. thanks.

    • @belmirp
      @belmirp Před rokem +10

      Without dispatch: 0.4291 ns (no memory allocation)
      With dispatch: 13.4429 ns (24 B memory allocation)

    • @ParkImaging
      @ParkImaging Před rokem

      @@belmirp wow. That is a huge difference!

  • @digitaldaridesatv1545

    Thank you Hassan!

  • @kiansadeqi7138
    @kiansadeqi7138 Před rokem

    Helpfull one👍

  • @tedchirvasiu
    @tedchirvasiu Před rokem

    Awesome, always wondered how the magic behind EF lazy loading proxies works.

  • @tuberklz
    @tuberklz Před rokem

    very helpful. kindest regards

  • @PierreH1968
    @PierreH1968 Před 28 dny

    Let's not forget the Attributes in C# that are a good implementation of AOP, and have been there forever.

  • @divyeshbhartiya
    @divyeshbhartiya Před 9 měsíci

    Awesome!!!

  • @AlFasGD
    @AlFasGD Před rokem +2

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

  • @iyadahmed3773
    @iyadahmed3773 Před rokem

    Thanks a ton 🙏

  • @aaryavartsolutions5359

    I am reminded of interception via decorator pattern.

  • @FabioAngela
    @FabioAngela Před rokem +4

    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

    • @kwwx345
      @kwwx345 Před rokem +1

      Metalama from PostSharp already does this. I got my eye on it, but haven't had a chance to try it yet.

  • @shofyansky
    @shofyansky Před rokem

    amazing 5 years ago its really hard to create aop

  • @zadrot64
    @zadrot64 Před rokem

    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)

  •  Před rokem

    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?

  • @zfold4702
    @zfold4702 Před rokem

    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)

    • @zfold4702
      @zfold4702 Před rokem +1

      BTW, for now we use MediatR and achieve such injection through Behaviors.

  • @Alex-eq2um
    @Alex-eq2um Před rokem

    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

  • @bilalhussain9631
    @bilalhussain9631 Před rokem +1

    Can be a good way for benchmarking, but I think maybe reflection would lead to inaccurate result as reflection is slow. Is it so ?

    • @HassanHabib
      @HassanHabib  Před rokem +1

      Give it a shot. Sounds like a good piece of knowledge to have. Let us know.

    • @JasonAtNovaleaf
      @JasonAtNovaleaf Před rokem +2

      ​@@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.

    • @HassanHabib
      @HassanHabib  Před rokem

      @@JasonAtNovaleaf good point. Something we must consider when leveraging this capability.

  • @sunnypatel1045
    @sunnypatel1045 Před rokem +3

    This is golden! This can be useful in a nuget package that I am writing! However can this be unit tested?

    • @HassanHabib
      @HassanHabib  Před rokem +1

      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.

    • @sunnypatel1045
      @sunnypatel1045 Před rokem

      @@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 🤟.

  • @guillermomazzari4983
    @guillermomazzari4983 Před rokem

    Thanks for sharing, even though I feel this is too advanced for me lol

  • @DataJuggler
    @DataJuggler Před rokem

    What software is that for creating diagrams?

    • @HassanHabib
      @HassanHabib  Před rokem +2

      It's called draw.io it's free and open source and also available on both desktop and web.

    • @DataJuggler
      @DataJuggler Před rokem

      @@HassanHabib Thank you. I started to write one the other day and knew there had to be plenty out there.

  • @smwnl9072
    @smwnl9072 Před rokem +1

    Seems related to the Decorator Pattern..

  • @asyrovprog
    @asyrovprog Před rokem

    Appears this method does not support recursive functions. Only top-level calls are intercepted. Is this true, or I'm missing something?

    • @HassanHabib
      @HassanHabib  Před rokem

      I noticed this too. There might be a way around it - I'm not sure.

  • @garcipat
    @garcipat Před rokem

    can this be used to guard certain methods with requireing permissions?

    • @HassanHabib
      @HassanHabib  Před rokem

      Give it a shot and let us know.

    • @garcipat
      @garcipat Před rokem

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

    • @HassanHabib
      @HassanHabib  Před rokem

      @@garcipat possibly on registration. So instead of registering the original concrete object you register the proxy

  • @alirezanet
    @alirezanet Před rokem

    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?

    • @HassanHabib
      @HassanHabib  Před rokem +1

      Correct. 100%.
      In terms of perf, I haven't really ran it extensively against something for perf. So I don't honestly know.

  • @thefront5871
    @thefront5871 Před rokem

    WoW! That's a rad monitor you got behind you! What's the make and model? I want one! LoL!

    • @HassanHabib
      @HassanHabib  Před rokem +1

      That's the Samsung Odyssey G9 49"

    • @thefront5871
      @thefront5871 Před rokem

      @@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?

    • @HassanHabib
      @HassanHabib  Před rokem

      @@thefront5871 Not at all. I have 3 of these monitors btw, and gave one away. All good here.

  • @GnomeEU
    @GnomeEU Před rokem +2

    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.

    • @henriquesouza5109
      @henriquesouza5109 Před rokem +1

      Wait, don't you already create the interfaces anyway to abstract implementation of your services?

    • @GnomeEU
      @GnomeEU Před rokem

      @@henriquesouza5109 I only use interfaces when i have to. They obscurify the code.

  • @nixer8605
    @nixer8605 Před 9 měsíci

    i need help with this, if anyone can help please just respond to this comment!

  • @christianista
    @christianista Před rokem

    Thanks fo sharing. The repo is gone.

    • @HassanHabib
      @HassanHabib  Před rokem +1

      Oh it was set to private. Now it's public. thanks for letting me know.

    • @christianista
      @christianista Před rokem

      @@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.

    • @HassanHabib
      @HassanHabib  Před rokem

      @@christianista You can either inject it in the original declaration of the service or pass it as a parameter to the `Create` function.

  • @metlic5209
    @metlic5209 Před rokem

    Can't wait to see when dotnet finally become java enterprise edition 😆

  • @odmehbb
    @odmehbb Před 6 měsíci

    Thanks, I've learnt something, but I really hope I'll never have to use this abomination.