微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

这个开源解决方案中的特定方法有什么作用

如何解决这个开源解决方案中的特定方法有什么作用

最近我试图了解 nopCommerce 插件的工作原理。

我会尽量让我的问题更清楚,让您明白,而无需在您的计算机上打开解决方案。

nopCommerce 插件中,一切都始于一个类,该类必须具有 IWidgetPlugin 接口的定义并从 BasePlugin 派生。

一个预定义的插件中有一个名为 NivoSliderPlugin 的类,这个类覆盖了它的一些基类方法,并定义了 IWidgetPlugin 的方法

代码如下:

    public class NivoSliderPlugin : BasePlugin,IWidgetPlugin 
    {

     // Here there are some fields that are referencing to different interfaces and then 
        they're injected to the class throw it's constructor
     
     // This method gets a configuration page URL
     public override string GetConfigurationPageUrl()
     {
        return _webHelper.GetStoreLocation() + "Admin/WidgetsNivoSlider/Configure";
     }

    }

在上面的代码中,我只提到了我有疑问的部分代码

_webHelper 是对接口的引用,并接受 bool 类型的传入参数。

不管返回的第二部分,我的意思是“Admin/WidgetsNivoSlider/Configure”的 URL 路径,我想知道 _webHelper.GetStoreLocation() 是如何工作的?

如你所知 _webHelper.GetStoreLocation() 没有定义!

我希望我刚才问的有道理。 如果不清楚,请让我知道以使其更清楚。 我会很感激的。

解决方法

让我们从 IWidgetPlugin 开始。 NopCommerce 中的所有插件都不需要实现此接口。这在您开发 Widget type plugin 时是必需的。这里 Nivo Slider 是一个小部件类型的插件,这就是它实现 IWidgetPlugin 接口的原因。这是 Nivo 滑块的实现。

/// <summary>
/// Gets a value indicating whether to hide this plugin on the widget list page in the admin area
/// </summary>
public bool HideInWidgetList => false;

/// <summary>
/// Gets a name of a view component for displaying widget
/// </summary>
/// <param name="widgetZone">Name of the widget zone</param>
/// <returns>View component name</returns>
public string GetWidgetViewComponentName(string widgetZone)
{
    return "WidgetsNivoSlider";
}

/// <summary>
/// Gets widget zones where this widget should be rendered
/// </summary>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the widget zones
/// </returns>
public Task<IList<string>> GetWidgetZonesAsync()
{
    return Task.FromResult<IList<string>>(new List<string> { PublicWidgetZones.HomepageTop });
}

如果你想开发一个支付插件,那么你必须实现IPaymentMethod即PayPal、Stripe、货到付款)和IShippingRateComputationMethod的运输方式(即 UPS、USPS)。 nopCommerce 中还有许多其他类型的插件可用。请参阅下面的 list

enter image description here

BasePlugin 是一个抽象类,所有插件类都需要继承它。它有一个名为 virtualGetConfigurationPageUrl() 方法,默认返回 null。但是这个 virtual 方法可以被每个插件类覆盖,并且可以返回该插件的管理端配置 URL。这是 Nivo 滑块插件的重写方法。

/// <summary>
/// Gets a configuration page URL
/// </summary>
public override string GetConfigurationPageUrl()
{
    return _webHelper.GetStoreLocation() + "Admin/WidgetsNivoSlider/Configure";
}

这里的 _webHelper.GetStoreLocation() 是一个返回站点基本 url 的方法。此方法在 Nop.Core.WebHelper 类中实现。此方法的可选布尔参数 (useSsl) 用于是否考虑 https:// 用于站点基本 url。如果此方法被特定插件覆盖,则配置 按钮将显示在本地插件列表页面中。

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。