i am searching the web now for a while without real success.
Lets assume following simple model, which i get from a webservice.
public class A
{
public int Id { get; set; }
public ObservableCollection Bs { get; set; }
public A () { }
}
public class B
{
public int Id { get; set; }
public ObservableCollection<C> Cs { get; set; }
public A Parent { get; set; }
public B() { }
}
public class C
{
public int Id { get; set; }
public B Parent { get; set; }
public C() { }
}
In my real world scenario the model will be bigger, including more levels / relations, but for showing my issue this simple model should be enough. The application i am writing loads the aggregate (A) only once during startup and then only listens for changes. Means there is a service running which i can subscribe to that tells me when new A,B or C's gets added / deleted or modified.
Therefore i thought it would be handy to create repositories for each entity and let each repository listen for changes and thats the point where i am stuck right now.
public class Repositories
{
public Repository<A> As { get; set; }
public Repository Bs { get; set; }
public Repository<C> Cs { get; set; }
public Repositories() { }
}
The Dataservice for example tells mit that a new B was added, so what should happen now is:
Add it to the B Repository
Find the A Parent and add B to its B Collection
Set the A Parent on the new B Object
or a B was deleted, what should happen is,
Remove B from repository
Remove B from his Parent (A)
Set the parent of B to null
Whats the easiest way to maintain all the references. In my real world model an entity could have multiple relationships instead of just one parent and a list of childs.
After further investigation i came to the conclusion that this is exactly the same thing which Entity framework is doing when you for example remove something from a dbset. Remove an entity from the dbset will remove also all references within other entities. So i need the same behaviour as entityframework besides the fact that i do not use a database.
I was stumbling over this topic a couple of times the last years and for simplicity just subscribed to my services which i defined as static singletons. So for example each A entity listens to a static event and then updates itself.
public class A
{
public int Id { get; set; }
public ObservableCollection Bs { get; set; }
public A () {
DataService.As.OnAdd += OnAdd();
DataService.As.OnDelete += OnDelete();
DataService.As.OnUpdate += OnUpdate();
}
}
This always worked fine for me but it has many downsides when it comes to unit testing. At this project i am trying to separate the concerns and i think the model itself should not know anything about the dataservice. Also i think one repository should also not know anything about other repositories (which i would need to set find parents and set the references). Maybe i am wrong on this point.
So the big question at the end is, whats the best or normally used solution for having a live updating domain model.
I hope my explanation is clear and my "pseudo" models will describe my issue
What I have tried:
Searched the web for similiar scenarios, without success.