I am developing a small ERP software. I'm wondering how to design entities as well as where to join them.
Im just gonna start with an example, that makes it way easier.
Say i'm modelling a maintenance contract.
1 Contract 1-n Machines 1-n contacts 0-n appointments 1-n purchasing prices 1-n selling prices
So i could go ahead in my domain layer, where i keep all the models and model a Contract class simply with its fields and a list of Machines.
A machine with a list of its fields and lista of contacts, appointments and prices.
If i go with this approach, which certainly makes sense, what do i do when loading these classes from the database? If i write a method that simply loads a full contract from the database, im loading so much extra information that i might not need. Maybe i only need to display the head data from the contract.
If i load it only partially, i have to #1 write several methods to load the contracts from SQL, several queries. And #2 i always need to make sure that the right amount of properties are loaded, so its a lot of extra programming effort and room for mistakes.
What i am doing right now, is keeping a lot of the entities seperate if they could logically stand on their own. (I'd go with the upper approach for example in an Order model, since neither items nor an order really make sense standalone)
Since im doing c# and MVVM i essentially have a Contract ViewModel, where i then have a list of Machine ViewModels and in there lists of Contact ViewModels etc.
This makes some sense to me, as i can freely mix entities together the way the need to be "viewed". The exact point of a viewmodel i think.
However, the class/method where i build those Contract ViewModels gets rather complicated and big. With a lot of lists, dictionaries grouping contacts by foreign keys etc. Making sure the models arein sync with the viewmodels.
And im not sure if im not misusing c# for the joining of entities when "thats SQLs job".
I am not used to seeing an architecture like this, so im wondering if im missing something?