I have an import that needs to grab data from a REST service and import into an web store. It's basically an ETL type of service, but because the REST service can be slow and I don't want to call it for every entity in the loop, I'm finding myself having to pass in a lot of parameters into the methods needed for import. Basically anytime I need something from the service, I have to add another param for that data. Looking for ideas on how this code could be better or if maybe nobody sees any issues with it.
public void RunImport()
{
List<Product> srcProducts = new List<Product>();
List<ProductOptions> srcProductOptions = new List<ProductOptions>();
List<ProductCustomFields> srcProductCustomFields = new List<ProductCustomFields>();
List<Order> srcOrders = new List<Order>();
List<Customer> srcCustomers = new List<Customer>();
List<Country> srcCountries = new List<Country>();
if (ShouldImportProducts)
{
srcProducts = restService.GetProducts();
srcProductOptions = restService.GetProductOptions();
srcProductCustomFields = restService.GetCustomFields();
if (srcProducts.Count > 0)
{
ImportProducts(srcProducts, srcProductOptions, srcProductCustomFields);
}
}
if (ShouldImportCustomers)
{
srcCustomers = restService.GetCustomers();
if (srcCountries == null) srcCountries = restService.GetCountries();
if (srcCustomers.Count > 0)
{
ImportCustomer(srcCustomers, srcCountries);
}
}
if (ShouldImportOrders)
{
srcProducts = restService.GetProducts();
srcCustomers = restService.GetCustomers();
if (srcCountries == null) srcCountries = restService.GetCountries();
if (srcCustomers.Count > 0)
{
ImportOrders(srcOrders, srcProducts, srcCustomers, srcCountries);
}
}
}
public void ImportOrders(List<Order> srcOrders, List<Product> srcProducts, List<Customer> srcCustomers, List<Country> srcCountries)
{
foreach (Order srcOrder in srcOrders)
{
var existingOrder = FindExistingOrder(srcOrder);
var destinationOrder = ConvertToDestinationOrder(srcOrder, existingOrder, srcProducts, srcCustomers, srcCountries);
SaveDestinationOrder(destinationOrder);
}
}
public DestinationOrder ConvertToDestinationOrder(Order srcOrder, DestinationOrder existingOrder, List<Product> srcProducts, List<Customer> srcCustomers, List<Country> srcCountries)
{
var destinationOrder = new DestinationOrder();
var productId = srcProducts.Where(x => x.Sku == srcOrder.Sku);
var customerId = srcCustomers.Where(x => x.Email == srcOrder.Email);
var countryId = srcCountries.Where(x => x.Code == srcOrder.Country);
destinationOrder.ProductId = productId;
destinationOrder.CustomerId = customerId;
destinationOrder.CountryId = countryId;
//...
return destinationOrder;
}