Instance of IOrderProcessingService

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Howdy!

How to get an instance of IOrderProcessingService inside a controlle?

I need to check order status.

Thanks!

-Alberto
11 years ago
Inject it using controller.

https://www.nopcommerce.com/docs/74/frequently-asked-development-questions.aspx (see "Inversion of Control and Dependency Injection" section)
11 years ago
I didnt get the point.

Could you provide some sample code of how to do that?

Thanks!



My Code:

public class MyRandomController : BaseNopController, IDependencyRegistrar
{

       [HttpPost]
        public ActionResult MyAction(FormCollection model)
        {
                //Order Instance
        }

       public void Register(Autofac.ContainerBuilder builder, Core.Infrastructure.ITypeFinder typeFinder)
        {
            
        }

        public int Order
        {
            get { throw new NotImplementedException(); }
        }

}
11 years ago
Just have a look at any other controller and its constructor

public class MyRandomController : BaseNopController
{
         private readonly IOrderProcessingService _opvService;

         MyRandomController(IOrderProcessingService opvService)
         {
            this._opvService = opvService;
         }

       [HttpPost]
        public ActionResult MyAction(FormCollection model)
        {
                //Order Instance - opvService
        }

}
11 years ago
I tried the above to add a new custom controller, but I get a 'No parameterless constructor defined for this object' error when trying to load the controller.

I tried adding the following code to the DependencyRegistrar in Nop.Web.Infrastructure, but still same error.
builder.RegisterType<ProductsController>()
                .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"));

What else do we need to do do add a custom controller?

My custom controller is based on the CatalogController, same constructor and fields.

Thanks
11 years ago
Is this in a plugin or in the main code?  You shouldn't have to register controllers individually.
11 years ago
This was in the main code, Web.

I got around it by having a constructorless parameter and then using the dependency resolver to resolve each service that i would need:

this._categoryService = DependencyResolver.Current.GetService<ICategoryService>();
this._productService = DependencyResolver.Current.GetService<IProductService>();

Not sure if this could cause problems but it works for now.
11 years ago
It's not the "right" way because you can't unit test it.
7 years ago
a.m. wrote:
Inject it using controller.

https://www.nopcommerce.com/docs/74/frequently-asked-development-questions.aspx (see "Inversion of Control and Dependency Injection" section)


FAQ URL seems to have changed to http://docs.nopcommerce.com/display/en/Inversion+of+Control+and+Dependency+Injection
7 years ago
jreate wrote:
This was in the main code, Web.

I got around it by having a constructorless parameter and then using the dependency resolver to resolve each service that i would need:

this._categoryService = DependencyResolver.Current.GetService<ICategoryService>();
this._productService = DependencyResolver.Current.GetService<IProductService>();

Not sure if this could cause problems but it works for now.


Yes it was the cause. For all your parametered new controllers you need to register it in the dependencyregistrar.cs

If it is in a plugin you need to put it in the plugin dependencyregistrar.cs
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.