Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am searching the web now for a while without real success.
Lets assume following simple model, which i get from a webservice.

C#
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.

C#
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.

C#
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.
Posted
Updated 23-Jun-20 22:18pm
v2
Comments
johannesnestler 24-Jun-20 6:06am    
I don't know your real requirnment - but I think it's very likely your solution is "Overkill" - If you realy have to do it like this you need a Visitor-Pattern (to avoid rings) to go through all nodes of your graph - this is hard to get right (recursion), and a lot of code. My rule of thumb: If something gets "complicated" like this - take a step back and question your design. E.g you maintain "backlinks" to the parents - is this really needed and so on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900