Reputation: 981
My domain is composed from two classes. The first is Patient. The second is Injection. Person can have many injection instances.
This raises architecture issue regarding some OOD, relational Database design and ORM (specifically EntityFramework):
I want that a Patient will hold list of injections property and that an Injection will hold a Patient property. This looks like a good object oriented design.
This design (using EntityFramework code first), will map to two tables: Patients table, and Injections table. The Injections table will have a foreign key to a row in Patients table.
So far so good.
Now, lets assume a database case as described above, with 1 patient which has 100,000 injections.
When I ask DbContext for this patient it will retrive the patient data and the list of Injections will load only by demand.
This looks like a potential problem. I'm saying to any potential user of Patient - "hey, you have a list of injections, use them! Only do - myPatient.Injections and they will magically appear".
However, by exposing this property I am potentially enabling a user of patient to run a very heavy query which may not even be an application need.
A possible solution is to remove the list of Injections from the patient class. Regarding the database, the mapping will remain the same, and in order to query injections of patient I will use a concrete service. But now I have a Patient class that doesn't represent my domain in a good way.
Another possible solution is to map each object returned from EF context to another shallow object. I will map only what i need, and wont expose unneeded data to the user. This approach will cause duplication and the maintaining of mapping between classes.
What do you think about this issue?
Share your knowledege :)
Upvotes: 1
Views: 54
Reputation: 48230
It all depends on how you actually expose your objects.
If objects are exposed at domain model level, then yes, a lazy loaded query to injections couls probably load and materialize 100k items. But hey, creating 100k instances and inserting them or doing anything as heavy as that could also be costly.
This means that if you are worried about potential misuse, do not expose the domain model to potential clients. Instead, have your services layer, probably even exposed over http so that you never run into such issues, for example - large sets would always be exposed with paging.
Upvotes: 1