0

Assume I have a class that represents a complex domain object with dozens of properties, such as a pension policy.

The GUI lists all pensions in a summary table that only contains a small subset of those properties.

The application uses an ORM such as .NET's entity framework.

When querying the db to populate the summary table, I don't want to return all details for every policy.

How should I handle this so that loading the summary table only queries the relevent fields? Should I use a seperate object such as PensionSummary? Is there a common approach to this scenario?

Thanks.

1

I'd use the following approach:

  • Define an IPensionSummary interface with all properties necessary;
  • Your main Pension implements this interface, and it reflects data from DB (a Pension table, eg);
  • In DB, create a View that actually queries only for a small subset of Pension, and retrieves a summarized set of columns (eg, PensionSummary view), this view will be just like another table, but will actually reflect data from other tables when queried;
  • Define a PensionSummary class that implements its interface as well, and there it will reflect the PensionSummary view from the DB.

If you have complex domain objects with complex methods, and you want to expose simpler objects with few properties and easier methods, you should use the Facade design pattern, but it seems that this is not your case.

0

I'd go for a PensionSummary (including some functionality for navigating to the full-featured PensionPolicy).

I'd definitely not create PensionPolicy instances that are only partially filled. IMHO a business object should be able to answer all questions regarding that object, independent of the circumstances (like currently being used only for display in some table). And I'd not try to create workarounds like lazy-loading should some of the empty infos be requested by some call.

So if you really don't want to provide full-featured PensionPolicy objects, then it has to be another class, closely related to the original one.

  • Thanks for the reply. The only problem with this approach is some duplication between the two classes. Do you see any issues with the main Pension class inheriting from the PensionSummary class to avoid this duplication? – John Steed Sep 14 '17 at 15:54
  • I wouldn't have a model class inherit from something that's meant for a specific view. Only if PensionSummary were replaced by something like PensionBaseData with the potential to be useful in other places as well. This way views get the choice between two detail levels of this object. And I'd consider both: inheritance or aggregation. – Ralf Kleberhoff Sep 14 '17 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.