OrderService nop 2.x

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

I am using a custom tax provider. Inside of the provider file, how do I use OrderService?

            OrderService os = new OrderService();
            os.GetOrderById(1);

What arguments do I put into OrderService? I don't understand IRespository.
12 years ago
Just inject IOrderService into constructor (similar to other plugins). For more info please look into 'Inversion of Control Containers and the Dependency Injection pattern'
12 years ago
Hi,

Thanks for the information.

I am now trying to use the TaxCategoryService, however, my IRespository<TaxCategory> is always null. When I try this:

private IRespository<TaxCategory> tc;

I am in the Provider class for the tax class library.
                
ISubscriptionService service = new SubscriptionService();
IEventPublisher _eventPublisher = new EventPublisher(service);

TaxCategoryService ts = new TaxCategoryService(CacheManager, tc, _eventPublisher);
//null reference because tc is null
var taxCat = ts.GetTaxCategoryById(orderLine.ProductVariant.TaxCategoryId);

Should I implement the interface IRepository<TaxCategory> on another class?

http://stackoverflow.com/questions/896550/how-to-implement-repository-pattern-with-interface-base-and-concrete

If so, how do I implement the table property?
12 years ago
I am trying to get the TaxCategoryName for an order item. Is there a better way?

I am also calling the code from the OrderUpdatedHandler and I can't use the dependency injection:

public class OrderUpdatedHandler : IConsumer<EntityUpdated<Order>>
    {
        private ICacheManager _cache;
        private IEventPublisher _event;
        private IRepository<Setting> _reposit;
        private ISubscriptionService _service;

        public void HandleEvent(EntityUpdated<Order> eventMessage)
        {

Provider.InsertTax(order)


}
12 years ago
joe_a84 wrote:
      
ISubscriptionService service = new SubscriptionService();
IEventPublisher _eventPublisher = new EventPublisher(service);

TaxCategoryService ts = new TaxCategoryService(CacheManager, tc, _eventPublisher);

Do not manually create instances. Pass them into contructor. Look at other plugins in order to see how it's done
12 years ago
Hi,

I am unable to pass them into the constructor. When I do, _repoOrder never gets populated. What am I doing wrong?

using System;
using System.Web.Mvc;
using Nop.Services.Configuration;
using Nop.Web.Framework.Controllers;
using Nop.Plugin.Tax.AvalaraTax.Models;
using Nop.Core.Data;
using Nop.Core.Domain.Orders;
using Nop.Core.Events;

namespace Nop.Plugin.Tax.AvalaraTax.Controllers
{
    [AdminAuthorize]
    public class TaxAvalaraController : Controller
    {
        private readonly AvalaraTaxSettings _avalaraTaxSettings;
        private readonly ISettingService _settingService;
        private IRepository<Order> _repoOrder;

        public TaxAvalaraController()
        {

        }

        public TaxAvalaraController(AvalaraTaxSettings avalaraTaxSettings, ISettingService settingService, IRespository<Order> repoOrder)
        {
            this._avalaraTaxSettings = avalaraTaxSettings;
            this._settingService = settingService;
            _repoOrder = repoOrder;
        }



        [HttpPost, ActionName("Configure")]
        [FormValueRequired("batch")]
        public ActionResult ConfigureBatch(TaxAvalaraModel model)
        {
            if (!ModelState.IsValid)
            {
                return Configure();
            }

            ISubscriptionService sub = new SubscriptionService();
            IEventPublisher _eventPublisher = new EventPublisher(sub);

            try
            {
                AvalaraTaxProvider avalaraTaxProvider = new AvalaraTaxProvider(_avalaraTaxSettings);
                var result = avalaraTaxProvider.BatchData(model.LowestOrderNumber, model.HighestOrderNumber, model.DoAvalaraReturnInvoice, this._repoOrder, _eventPublisher);
                if (result != null)
                {



                    if (result.Messages.Count > 0)
                    {
                        model.BatchResponse = "Some errors have occured. Please check your Avalara admin to ensure that this was successful. If it was, then you can ignore this message. For error details, please check the log file: avalaralog.txt";
                    }
                    else
                    {
                        model.BatchResponse = "All transactions have completed successfully!";
                    }
                }


            }
            catch (Exception exc)
            {
                model.BatchResponse = exc.ToString();
            }

            return View("Nop.Plugin.Tax.StrikeIron.Views.TaxStrikeIron.Configure", model);
        }


    }
}
12 years ago
joe_a84 wrote:
Hi,

I am unable to pass them into the constructor. When I do, _repoOrder never gets populated. What am I doing wrong?

using System;
using System.Web.Mvc;
using Nop.Services.Configuration;
using Nop.Web.Framework.Controllers;
using Nop.Plugin.Tax.AvalaraTax.Models;
using Nop.Core.Data;
using Nop.Core.Domain.Orders;
using Nop.Core.Events;

namespace Nop.Plugin.Tax.AvalaraTax.Controllers
{
    [AdminAuthorize]
    public class TaxAvalaraController : Controller
    {
        private readonly AvalaraTaxSettings _avalaraTaxSettings;
        private readonly ISettingService _settingService;
        private IRepository<Order> _repoOrder;

        public TaxAvalaraController()
        {

        }

        public TaxAvalaraController(AvalaraTaxSettings avalaraTaxSettings, ISettingService settingService, IRespository<Order> repoOrder)
        {
            this._avalaraTaxSettings = avalaraTaxSettings;
            this._settingService = settingService;
            _repoOrder = repoOrder;
        }



        [HttpPost, ActionName("Configure")]
        [FormValueRequired("batch")]
        public ActionResult ConfigureBatch(TaxAvalaraModel model)
        {
            if (!ModelState.IsValid)
            {
                return Configure();
            }

            ISubscriptionService sub = new SubscriptionService();
            IEventPublisher _eventPublisher = new EventPublisher(sub);

            try
            {
                AvalaraTaxProvider avalaraTaxProvider = new AvalaraTaxProvider(_avalaraTaxSettings);
                var result = avalaraTaxProvider.BatchData(model.LowestOrderNumber, model.HighestOrderNumber, model.DoAvalaraReturnInvoice, this._repoOrder, _eventPublisher);
                if (result != null)
                {



                    if (result.Messages.Count > 0)
                    {
                        model.BatchResponse = "Some errors have occured. Please check your Avalara admin to ensure that this was successful. If it was, then you can ignore this message. For error details, please check the log file: avalaralog.txt";
                    }
                    else
                    {
                        model.BatchResponse = "All transactions have completed successfully!";
                    }
                }


            }
            catch (Exception exc)
            {
                model.BatchResponse = exc.ToString();
            }

            return View("Nop.Plugin.Tax.StrikeIron.Views.TaxStrikeIron.Configure", model);
        }


    }
}


Remove your default constructor.



        public TaxAvalaraController()
        {
//Remove this constructor
        }


Remove everywhere you create new instances of nopCommerce dependencies. Or dependencies of your own.


//Remove these and have them injected through the constructor
            ISubscriptionService sub = new SubscriptionService();
            IEventPublisher _eventPublisher = new EventPublisher(sub);


//Remove this line and use dependency injection instead
                AvalaraTaxProvider avalaraTaxProvider = new AvalaraTaxProvider(_avalaraTaxSettings);


Your constructor should look something like the one below



        public TaxAvalaraController(AvalaraTaxSettings avalaraTaxSettings, ISettingService settingService, IOrderService orderService, ISubscriptionService subscriptionService, IEventPublisher eventPublisher, IAvalaraTaxProvider taxProvider)
        {
            this._avalaraTaxSettings = avalaraTaxSettings;
            this._settingService = settingService;
            _orderService = orderService;
            _subscriptionService = subscriptionService;
            _eventPublisher = eventPublisher;
            _taxProvider = taxProvider;
        }


In addition to reading about dependency injection in the link provided by Andrei you should review my blog post about creating plugins with data access. To get your project working you must understand dependency injection and use it in your code.

http://blog.csharpwebdeveloper.com/2011/09/23/nopcommerce-plugin-with-data-access/
12 years ago
Hi,

Thanks, that fixed it. I needed to invoke my provider in its regular way:

                AvalaraTaxProvider atp = new AvalaraTaxProvider(_avalaraTaxSettings);
                var result = atp.BatchData(model.LowestOrderNumber, model.HighestOrderNumber, model.DoAvalaraReturnInvoice, _orderService, _eventPublisher);

        public TaxAvalaraController(AvalaraTaxSettings avalaraTaxSettings, ISettingService settingService, IOrderService orderService, ISubscriptionService subscriptionService, IEventPublisher eventPublisher)
        {
            this._avalaraTaxSettings = avalaraTaxSettings;
            this._settingService = settingService;
            _orderService = orderService;
            _subscriptionService = subscriptionService;
            _eventPublisher = eventPublisher;
            JMABase.WriteLogFile("all 5 parameters.", "/avalaralog.txt");

        }

I can only enter parameters from your stuff and not mine.
12 years ago
joe_a84 wrote:
Hi,

Thanks for the info.

I have changed my code, however, I now receive this error:

No parameterless constructor defined for this object.

...



This is because your dependencies are not wired up (or not wired up correctly). You need to add a class that implements IDependencyRegistrar and map your dependency bindings. Read the section on dependency injection in my article below and be sure to review the code sample I provided.

http://blog.csharpwebdeveloper.com/2011/09/23/nopcommerce-plugin-with-data-access/
12 years ago
Hi,

Thank you for your advice. Many of the problems with the software have been solved.

I am now trying to use TaxCategoryService and CustomerService in my TaxProvider class. Here is the method:

public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest)
{

//get tax class name, based on ID


//update customer information with validated address

}

Do I use IDependencyRegistrar somehow? The problem is that I'm calling this from a handler and I cannot adjust the constructor.

I am calling my tax provider in the EntityUpdated<Order> event like this:

    public class OrderUpdatedHandler : IConsumer<EntityUpdated<Order>>
    {

...

        public void HandleEvent(EntityUpdated<Order> eventMessage)
        {
          

            var order = eventMessage.Entity;

          

            if (order.OrderStatus == OrderStatus.Complete)
            {
              
                //TO DO: Insert TaxCategoryService into this method
                api.InsertTaxIntoAvalara(order);
          
            }
          

        }

I cannot adjust the constructor because then it won't implement the interface. Do you have any advice?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.