Is there a best-practice pattern I can/should use for this scenario? I need access to some DI services in the OnFinished()
method:
public class Product
{
public int Quantity {get;set;}
}
public abstract class Job
{
public int Duration {get;set;}
public object? Product {get;set;}
public virtual void OnFinished() {}
public void Tick() { // checks to see if Duration is met, then calls OnFinished if so. Tick() is called externally. }
}
public class BuildProduct : Job
{
public string Name {get;set;}
public override object OnFinished()
{
var product = new Product();
// need access to DI services here to populate product, for example:
var service = ServiceActivator.GetService<ProductCounter>();
product.Quantity = service.CountStuff();
//return product;
Product = product;
}
}
Above feels dirty in regards to a DI based app, testing is more complicated and any use of ServiceActivator
doesn't feel right.
Job.OnFinished
is called by itself after Job is complete, self dependent. Its on a timer, when the Duration
is met, OnFinished is called.
Jobs are instantiated normally: var job = new BuildProduct();
.
OnFinish()
? What initializes a BuildProduct object? Are you using a particular framework?