how customize services, controllers

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi:

I've created a new functionality on Import/Export service, specificly at ImportManager, I've modified the IImportManager.cs interface and also the ImportManager.cs, the CustomerController controller of Admin folder, the List.cshtml view of Admin/Customer. All this is working fine, doing what is required, but I've modified the NopCommerce source code, so if I update the NopCommerce files to a new version I'll lost the changes made.

Putting the List.cshtml view in the new Theme created solves the problem of modifying the original file, but I don't know where to place the modifications made to controller and to the service in order to keep the original source files intact.

i tried by making a plugin where I put the service, the controller and the view on it, but I don't know how to make that entry point in the Admin/Customer Menu been the controller in my plugin. (show the view in the plugin instead of the default view, or the view in the theme)

Maybe it is not the best way to do this, please somebody could tell me how to do it?

Thanks
Vivian
12 years ago
Not really sure about the controller but for services I will do the following:

*Create a new class that inherits from the implementation you want to replace (not the interface)
*Override the method(s) you want to change
*Create a new DependencyRegistrar class that registers all of your versions but has a 1 for the Order property so that they are a higher priority than the standard Nop services.
*Register your services for the interface

builder.RegisterType<ASCProductService>().As<IProductService>().InstancePerHttpRequest();

I've got ASCProductService inheriting ProductService.  Any method that ASCProductService doesn't implement automatically goes up to the parent class (ProductService) and uses that one.  

Now if I upgrade, my class is untouched but everything else in ProductService gets updated.
12 years ago
The previous answer is very good for services modifications.

As for when you absolutely need to modify the source code (for instance if you need to alter the code in a controller), you can use tools such as TortoiseHg to connect to the nop mercurial repository to get the latest changes and then merge them with your local repository. This way, you won't lose your modifications. However, this can lead to situations where you need to refactor your code depending on the changes you made on your side and the changes that were made on the nop repository.

This also has the advantage that you can merge changes to a fork so you can push your changes so that the nop team can eventually merge them in the master repository for the benefits of everyone :)
12 years ago
This is similar to what I'm looking for.  To take it one step further, how would I go about handling new methods in the new class implementation?

In Andy's example, say I want to write something like:

ASCProductService.GetProductVariantBy___
- where ___ is any column in the ProductVariants table (or perhaps a new column entirely).

However, since this method is not in the interface, I get the following error:

'Nop.Services.Catalog.IProductService' does not contain a definition for 'GetProductVariantBy___' and no extension method 'GetProductVariantBy___' accepting a first argument of type 'Nop.Services.Catalog.IProductService' could be found

Which makes sense since I did not update the interface IProductService.

I had looked into something to the effect of:

// new interface with method I want to implement
IASCProductService.GetProductVariantBy___

AND

// Similar to Andy's suggestion, but including implementation of new interface
ASCProductService : ProductService, IASCProductservice

GetProductVariantBy___ ()
{
// implementation here
}

But I would run into the same error since the injected dependency is IProductService.

Note
My intentions are to adhere to the Open/Closed SOLID principle, but I feel stuck between a rock and a hard place concerning:
- 'extending' essentially the entire Product system (due to fundamental changes in Product and ProductVariant db tables)
OR
- 'modifying' the existing Product system.


Any insights out there?
12 years ago
When adding a new function like that I do modify the interface and put in a stub in the nop service then override it in my own.
9 years ago
AndyMcKenna wrote:
When adding a new function like that I do modify the interface and put in a stub in the nop service then override it in my own.


I wouldn't add custom methods in core interface as stub as you will end up with endless amount of custom methods in the interface and the core controller needs to add many stub methods.

My approach is add a CustomInteface that inherits CoreInterface with the new method. In the Register I will still register CustomConcreteClass to CoreInterface (because racing car is a type of car), but in my CustomerConcreteClass ctor, I will take ICoreService as parameter and cast it as CustomService (My car is a racing car).


http://tech.sunnyw.net/2013/12/nopcommerce-create-customized-service.html
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.