Consider writing separate Repository
classes which are responsible for querying your database, but which also don't contain any domain data themselves. (They might contain DB connection state or something similar but no actual domain data)
When working with query results, the ideal solution is to map on to plain 'model' entities which only contain properties and no logic (i.e. don't treat mapped entities as objects, because their responsibility is only to bridge the gap to your database).
Consider changing the User
into a UserModel
which contains only data properties mapped from your database, and no logic. (also consider Job
to JobModel
for consistency)
Secondly, use several Repository
classes for querying the database and returning that data mapped on to entity classes; i.e.
UserRepository
to query and map UserModel
dataJobRepository
to query and map JobModel
dataAddressRepository
to query and map AddressModel
data
Each of your UI Views could choose what data they needs from each repository (if any) based on a query decided by the view controller, without placing a dependency on a User
object.
Lastly, if a View needs something more than a single database model (and especially if it needs extra data which isn't from the database), consider letting your View Controller build ViewModel
s based on the models from your Repositories. For example:
class UserViewModel { UserModel user; List<AddressModel> addresses; bool isReadOnly; }
ViewModels are useful for ensuring each View has the right data (and only the data it needs) without leaking UI concerns into the rest of an application. Common UI patterns such as MVC/MVP use ViewModels to decouple any special UI-behaviour or UI structures away from the rest of the app
Updated:
(In response to the comment about business logic)
Neither UI classes nor Database/persistence classes should be dealing with business logic. Most non-trivial applications need more than just the ability to pull data straight from a repository; an extra 'layer' in-between of classes and functions which contain that logic is the typical solution.
The business logic, app logic and other in-between classes would be used directly by the UI Controller classes - i.e. (Rough schematic):
View --> Controller --> Business logic classes --> Repositories / External APIs / etc.
Arrows indicating the direction of each dependency. A View Controller may depend upon classes containing Business logic but not the other way around; Business logic classes may depend upon Repositories but Repositories would still know nothing of the business logic.
Address
of the logged inUser
"? Are you talking about a GUI?