Development Suggestion: Make PrepareModel private methods in the Nop.Web Controller classes protected or somehow callable from thridy party extensions

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 年 前
Hi Andrei and nopCommerce team,

While working on nopCommerce plugins and themes we constantly encounter one and the same problem. Let me try to describe it.
You know how sometimes you need to customize some view in a nopCommerce theme and you cannot just do it with css or razor.
For example we want to change the RecentlyViewedProductsBlock.cshtml to contain not only the product titles but also the product images and prices. Now the Nop.Web.CatalogController.RecentlyViewedProductsBlock action prepares the product models like this:

foreach (var product in products)
                    model.Add(PrepareProductOverviewModel(product, false, false));


which means that the pictures and prices are not put into the model (the two false flags define if pictures and prices should be put into the mdoel).
So we created a plugin for the theme, and in this plugin we added our own CatalogController.RecentlyViewProductsBlock action which overrides the default one from Nop.Web.CatalogController. The code in this action is pretty much the same with some minor changes but most importantly it prepares the product models like this:

foreach (var product in products)
                    model.Add(PrepareProductOverviewModel(product, true, true));


The problem is however that the Nop.Web.Controllers.CatalogController.PreparedProductOverviewModel method is private. So we need to rewrite it in the plugin. If we rewrite the PrepareProductOverviewModel method, this has a ripple effect as this method uses methods like PrepareProductPriceModel, which is private too and the PrepareProductPriceModel uses other private methods. So you can see how this becomes a problem for the plugin. We could rewrite all of them but upgrading later to new versions of nopCommerce will be difficult. We could change them directly in the Nop.Web.CatalogController class but this is not what we want for the same upgraded reasons and also because we loose the pluggable nature of the theme.

So we were wondering if in the CatalogController the PrepareModel private methods could be made protected or extracted somehow to a separate public Interface so that they can be used by third party plugins.
We believe that such methods are:

1. Nop.Web.Controllers.CatalogController.PrepareProductDetailsPageModel
2. Nop.Web.Controllers.CatalogController.PrepareProductOverviewModel
3. Nop.Web.Controllers.CatalogController.PrepareProductPriceModel
4. Nop.Web.Controllers.CatalogController.PrepareProductReviewsModel
5. Nop.Web.Controllers.CatalogController.PrepareProductVariantModel

These are the methods for the CatalogController but every controller in the Nop.Web namespace has such PreparedModel methods, which are private and could be made accessible so that the views returned from the controller actions can be customized easier.

If this could be done in some of the coming versions of nopCommerce it would be great help for third party plugin development!

Many Thanks
12 年 前
Sure. Thanks for suggestion!

UPDATE: Work item is here
12 年 前
BTW, could you provide some examples of how you exactly you're going to use a 'PrepareModel' method if it's marked as protected? Do you create a new controller inherited from CatalogController in a plugin or what?
12 年 前
a.m. wrote:
BTW, could you provide some examples of how you exactly you're going to use a 'PrepareModel' method if it's marked as protected? Do you create a new controller inherited from CatalogController in a plugin or what?


Yes, this seems like the most straightforward way to use the Prepare methods. Inherit from the controller class, CatalogController for example and call the Prepare methods in the various action methods of the plugin.

As an example we can take the related products action in the Catalog controller.
We want the related products to return the price and picture images.
So we inherit from the Catalog controller and rewrite the RelatedProducts action by calling PrepareProductOverviewModel method with different parameters i.e PrepareProductOverviewModel(x, true, true).
So having the PrepareProductOverviewModel as protected will do the job. As when it is private we need to copy almost all the code from the CatalogController.

[ChildActionOnly]
        public ActionResult RelatedProducts(int productId)
        {
           ...

            var model = products.Select(x => PrepareProductOverviewModel(x,true,true)).ToList();
           ...
        }


Please let us know if this is not clear
12 年 前
I see. Marking these methods as protected is the easiest way. But I think that maybe it's better (architecturally better) to refactor the source code in order to have some kind of ModelCreators (classes or services which will prepare the presentation layer models)
12 年 前
Yes, this would be great We did not suggest it as it seems much more work and we did not want to push it too far :-).
12 年 前
I've just marked these methods as protected. But it's the temporary solution. As I've written above it's better to have separate classes or services which will prepare the presentation layer models
11 年 前
Agreed - I think that this would be very valuable. In general, this part of the architecture strikes me as excessively monolithic, and needs to be broken up into smaller, interoperable chunks. I'm working on a similar problem involving a plugin designed to support repeating orders, and I'm running into lots of problems getting it to work without changing the existing nopCommerce code. If I can come up with a way to refactor out the various PrepareModel*** methods in a way that makes generalizable sense, I'll try to submit that as a change.
8 年 前
facing the same issue here... any news on:

Make methods in the Nop.Web Controller classes somehow callable or extendable from third party extensions #233

?

tx
7 年 前
rudgr wrote:
facing the same issue here... any news on:
...
?

And done! The upcoming version 3.90 will use model factories for Nop.Web project
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.