Api - insertproduct

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 年 前
I'm writing a small program which adds products (and variants) in batch. So I found the function "InsertProduct" in de ProductService-class. How should I declare a Prdouctservice instance ?

This is what I'm doing now:

ProductService ps = new ProductService();
ps.InsertProduct(product);

This doesn't work, because the constructor for the ProductService doensn't allow 0 arguments. I need about 20 arguments...

Isn't there an easier way to do this ?
11 年 前
use the same controller where you find the insert method found with a new different action on that controller. so no need to worry about passing parameters.


http://www.elaamart.com
11 年 前
I don't think I understand what you are saying. Could you clarify it more ?
11 年 前
in Presentation/Nop.Admin/Controllers you can find ProductController.cs file. you can add one more action in that file and write your own logic.


http://www.elaamart.com
11 年 前
regusman wrote:
I'm writing a small program which adds products (and variants) in batch. So I found the function "InsertProduct" in de ProductService-class. How should I declare a Prdouctservice instance ?

This is what I'm doing now:

ProductService ps = new ProductService();
ps.InsertProduct(product);

This doesn't work, because the constructor for the ProductService doensn't allow 0 arguments. I need about 20 arguments...

Isn't there an easier way to do this ?


Just use Dependency Injection to do the job:

public class [YOUR_CLASS]
{
  private readonly IProductService _productService;

  public [YOUR_CLASS](IProductService productService)
  {
    _productService = productService;
  }
}


The code above does the following:
- declare a private field of type IProductService
- create a constructor that takes in IProductService
- maps the argument in the constructor to the private field
- nopCommerce will take care of the rest (i.e. resolving the dependencies)

General Rule of Thumb (correct for 90% of the time): If you have a service named [NAME]Service, then you'll have an interface I[NAME]Service, and vice versa. :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.