Link to home
Start Free TrialLog in
Avatar of Steve Synan
Steve Synan

asked on

Looking for high level guidance on developing a simple "rules" object of sorts. Details inside regarding the project I'm working on...

Hello guys,

I'm just looking for some higher level design guidance for this particular scenario.

The 'core' object in the system I'm developing is called a Study. This object along with the objects that hang off of it are basically what the user is manipulating.

One example of objects hanging off are the "StudyTask" objects. These are tasks which are generated when the user builds a new study and assigns it a particular "StudyType".

It's a fair bit more complex, but in a simplified manner I have something like the following:

public class Study {
	public Guid ID { get; }
	public StudyType Type { get; set; }
	public IList<StudyTask> Tasks { get; set; }
	public DiseaseInduction Induction { get;set; }
}

public enum StudyType {
	Oncology,
	Efficacy,
	Imaging
}

public class StudyTask {
	public string Name { get; set; }
	public decimal Cost { get; set; }
}

public class DiseaseInduction {
	public int NumAnimals { get; set; }
	public string Model { get; set; }
}

Open in new window


Please keep in mind that I'm really simplifying this...

Okay, finally I have a global cache object which (among other things) has a costing table, something like this:


public static class Cache {
	public static CostTable { get; }
}

public class CostTable {
	public decimal PricePerAnimal { get; }
}

Open in new window


So here's the basic workflow the user experiences:

      -> Create new study
      -> assign type of study
      -> program does a lookup and based on study type will create "StudyTasks" (they will vary based on selection)
      -> Much of the Study object is bound to the UI (using WPF)

Okay, so here's where my questions comes in:

There are many 'rules' for how these StudyTasks should be priced, which depend on the type of task. For example, imagine a StudyTask called "Animal Care". The StudyTask.Cost in this case depends on how many animals there are in DiseaseInduction.NumAnimals. This can change at any time - so if the user comes into the Study at a later date and changes NumAnimals from 10 to 20, then it needs to recalculate the costs.

So I need to find a way to know when the user changes a value, and then I need to write some sort of object that will adjust all the StudyTasks where appropriate. There could be any number of rules applied when adjusting StudyTask objects - for example sometimes it's as simple as looking at the number of animals and multiplying that by CostTable.PricePerAnimal, but in other instances more complex rules might come into play - where it might need to look at DiseaseInduction.Model to determine how to set costs. In reality some of these rules can become quite complex and will depend on many variables that the user inputs.

- What is the best way to determine when the user changes a value in any of these objects? Do I just raise OnPropertyChanged events all over the place and have some sort of "Rules" object of sorts perform calculations and set the StudyTask values?

- Any high level guidance on how to develop a "rules" object of sorts is greatly appreciated. This object needs to know in real time when properties on the Study object have changed (or changes on any object that hangs off of the Study object), and then it needs to adjust StudyTask objects based on any number of rules.

Thanks for the help! I apologize if this isn't super clear and am more than happy to clarify if required.
Avatar of Ark
Ark
Flag of Russian Federation image

You can link Parent/Child classes smth like
class Parent
{
    List<Child> children = new List<Child>();
    Child CreateChild()
    {
        children.Add(new Child(this));
    }
}

class Child
{
    Parent parent;
    Child(Parent parent)
    {
        this.parent = parent;
    }
}

Open in new window

Now when child property changes you can set either parent's property or call parent's method to recalculate properties.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve Synan
Steve Synan

ASKER

There is an interesting question left open: Must a recalculation be immediate, if required or is a delay (asynchronous behavior) possible?

I may have overstated the complexity of these rules. Recalculation should be immediate, but I don't foresee the governing rules being so complex that there will be a perceptible delay. What I meant to say was that there could by any number of conditional checks on the Study object or other objects nested within the Study object, but it will largely amount to running through numerous "if/then" checks on various properties in order to determine the "task cost" on a per-task basis. At any time they could add a new task with different rules for determining cost, but again, I don't think any of these rules are highly complex in of among themselves, the complexity lies in that this needs to be as dynamic as possible and as easy to maintain as possible so new tasks with new rules aren't too painful to implement.

Also, the list of task items will be fairly small (less than 50 in most cases, at worst I can't imagine it being more than 100), so I don't foresee incurring much of a delay in our situation.
Then using OnPropertyChanged is okay.
Alright, thanks! I guess it's good that I was heading on the right track and your confirmation gives me more confidence - I was just unaware if there is a different pattern better fit for this particular scenario.