Plugin Override Service Virtual Method

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Can anyone point me in the right direction to create a plugin that allows me to override a method in a Nop.Services dll. I want to override the CheckOrderStatus method of the OrderProcessingService class.

Is this possible without editing the base service code?
6 years ago
Sure
1. Create your custom new class derived from OrderProcessingService
2. Override the CheckOrderStatus  method
3. Create a new class implementing from IDependencyRegistrar. Set its "Order" property to some big value so it's run after the default one
4. Register your new custom class as IOrderProcessingService.

This way your implementation will be used instead of the default one
6 years ago
Thank you!

Exactly what I was looking for.
6 years ago
Hello,

I'm having the same goal, to override a methode from a Service in a Plugin.
I followed your steps, but the original Method is still invoked instead of the override Method.
5 years ago
NeleBudde wrote:
Hello,

I'm having the same goal, to override a methode from a Service in a Plugin.
I followed your steps, but the original Method is still invoked instead of the override Method.


I just successfully performed this task for a plugin I'm writing for Nop 4.1. Try following these steps:

1) Follow the instructions on creating a plugin.
2) In your plugin project make sure that you have a DependencyRegistrar class:

  public class DependencyRegistrar: IDependencyRegistrar
  {
    public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
    {
      builder.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope();
    }

    public int Order => 1000;   // Use a high number here so we get added last and take precedence.
  }


3) The important bit is probably the value you assign 'Order'.  As Andrei mentions, it should be a 'Big' number to ensure that it is registered after the default implementation.

4) The class called 'MyService' should be the one you create in your plugin and it will be doing the overriding of the original class.

Try this.  If it does not work, please post some code.
5 years ago
basically is there a way to avoid code duplication with this method? since only the method you want to override is needed.

then also what if you have 2 or more plugins overriding a method in this class, this might lead to a problem
5 years ago
towfiles wrote:
basically is there a way to avoid code duplication with this method? since only the method you want to override is needed.

then also what if you have 2 or more plugins overriding a method in this class, this might lead to a problem


You might be able to inherit from the original class and only override that method.  In the original case above, the CheckOrderStatus is virtual.

As far as I know, there would not be a conflict between multiple plugins.  The reality is that only one will 'win' and be used.  The winner is determined by the 'Order' property when you register the class using the DependencyRegistar class in your plugin.  BUT, you have a good point.  In the end there can only be one, afaik.  So, if you do have multiple plugins that are doing the same thing, only one will be executed.
5 years ago
towfiles wrote:
... is there a way to avoid code duplication with this method?

You may be able to call the base method, before or after your calculations, to adjust the returned results.
5 years ago
My Plugin Override method is working. But, also working when the plugin is not 'installed'!. I have to Delete plugin in the Admin UI to stop my code execution.

Is there a formal approach to check if the plugin is installed and/or enabled, before overriding the base class?

Thanks
Jon
5 years ago
JonQuick wrote:
My Plugin Override method is working. But, also working when the plugin is not 'installed'!. I have to Delete plugin in the Admin UI to stop my code execution.

Is there a formal approach to check if the plugin is installed and/or enabled, before overriding the base class?

Thanks
Jon


Where is the code that registers the dependency?  Is it in the plugin or someplace else?  Where is it in the plugin?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.