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/