Reference/Reuse existing Services in a Widget

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

I am novice in nopCommerce. I am writting a plugin(widget),and want to make use of existing service and extract from database the list of all manufacturers using an existing function from the Manufacturer class.

But I stumbled up on how to get the service reference Or should I recreate a service class instance ( and how ), or there are other methods of getting a existing instance of that service, can someone help?

the function i want to reuse is:  GetAllManufacturers()

Thankyou
12 years ago
Have your project reference the Nop.Services and Nop.Core projects.

In your class add to the top:

using Nop.Services.Catalog;
using Nop.Core.Domain.Catalog;


That's the namespace for the service you want and Core defines the Manufacturer object.

Then create a member to hold the service:

private readonly IManufacturerService _manufacturerService;


Then inject the interface into your constructor:



public YourClass(IManufacturerService manufacturerService)
{
   this._manufacturerService = manufacturerService;
}


The DI container will put the correct implementation class in your field, even if it gets swapped out later.

Now just use the method you want:

IList<Manufacturer> list = _manufacturerService.GetAllManufacturers();
12 years ago
Thanks , that worked fine !

I was trying different methods, recreating the class, creating a separate service for the widget and all without success.
But in the end all is so simple, I didn't know that the framework passes automatically references to service instances into constructor - that's cool and simple:)
12 years ago
double click - double post
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.