In my program, a product (or an ingredient) needs to update its own parents prices, and those parents have to do the same for their parents and so on.
I've wrote a method like this :
public static Price CalculatePrice(this IProduct pro)
{
/// calculation stuff
}
private static SystemContext innerContext;
/// <summary>
/// In the main controller this method called for updates
/// </summary>
/// <param name="pro"></param>
/// <param name="sc"></param>
public static void UpdatePrice(this IProduct pro, ref SystemContext sc)
{
if (sc.Relations.Where(t => t.SubProduct.ID == pro.ID).Any())
{
// If this returns any, this means there are some products using this product as their sub-product.
var list = sc.Relations.Where(t => t.SubProduct.ID == pro.ID).ToList();
ConcurrentQueue<Relation> quee = new ConcurrentQueue<Relation>(list);
innerContext = new SystemContext();
Task task = new Task(() => UpdatePrice(ref quee));
task.Start();
}
}
private static void UpdatePrice(ref ConcurrentQueue<Relation> queue)
{
Relation val;
while (queue.TryDequeue(out val))
{
val.Product.Price = val.Product.CalculatePrice();
var list = innerContext.Relations.Where(t => t.SubProduct.ID == val.Product.ID);
if (list.Any())
{
ConcurrentQueue<Relation> quee = new ConcurrentQueue<Relation>(list);
Task task = new Task(() => UpdatePrice(ref quee));
task.Start();
}
}
}
Although, first level of parent products get updated, second level are not.
Is there a better logic to do this ?
And by the way, any of the lowest level products have approx 1000 parents (recursively). Which means time consuming (because of the calculation of price). So it would be perfect if you consider the time also when you give advice...
Edit
While i was doing some test. I figured that, some lowest level prodducts have 4000 parents.