首页 > 解决方案 > 在这个开源解决方案中,特定方法有什么作用

问题描述

最近我试图了解 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() 没有定义!

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

标签: asp.net-mvcasp.net-corenopcommerce

解决方案


让我们从IWidgetPlugin. NopCommerce 中的所有插件都不需要实现此接口。这在您开发Widget 类型插件时是必需的。这里的 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 中还有许多其他类型的插件可用。请参阅下面的列表

在此处输入图像描述

BasePlugin是一个抽象类,所有插件类都需要继承。它有一个virtual名为默认GetConfigurationPageUrl()返回的方法。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。如果此方法被特定插件覆盖,则配置按钮将显示在本地插件列表页面中。

在此处输入图像描述


推荐阅读