nopSolutions / nopCommerce

ASP.NET Core eCommerce software. nopCommerce is a free and open-source shopping cart.
https://www.nopcommerce.com
Other
9.31k stars 5.33k forks source link

Code Refactoring: Use ViewComponent's type instead of name for strong typing #6081

Closed DamienLaw closed 2 years ago

DamienLaw commented 2 years ago

nopCommerce version: 4.5-rc

Instead of

[ViewComponent(Name = "CheckMoneyOrder")]
public class CheckMoneyOrderViewComponent : NopViewComponent
{
}

public class CheckMoneyOrderPaymentProcessor : BasePlugin, IPaymentMethod
{
    public string GetPublicViewComponentName()
    {
        return "CheckMoneyOrder";
    }
}

Do this instead

public class CheckMoneyOrderViewComponent : NopViewComponent
{
}

public class CheckMoneyOrderPaymentProcessor : BasePlugin, IPaymentMethod
{
    public Type GetPublicViewComponent()
    {
        return typeof(CheckMoneyOrderViewComponent);
    }
}

This extends to the entire codebase utilizing ViewComponents including Payment plugins, Shipping plugins, External Login plugins, etc. By using a stronger typing approach, the usages of ViewComponents can tracked across the solution.

According to https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-6.0, image

DmitriyKulagin commented 2 years ago

Closed #6081