Search code examples
repositoryobject-modelfacade

"Points of view" in the object model


We have two domain types: Users and Locations.

We have a method on the LocationRepository: GetUserLocations().

The existing implementation:

var user = UserRepository.GetUser(userId);
var locations = LocationRepository.GetUserLocations(userId);

To me it makes more sense to retrieve the locations associated with a user from the User type i.e.:

var user = UserRepository.GetUser(userId);
var locations = user.GetLocations();

I think the latter implementation reads more cleanly and as an API client I have to deal with fewer types (i.e. the LocationRepository is not required). On the other hand, there will be more code to maintain as I have to write the "facade" to the LocationRepository.

Should I act on my instinct and create a facade on the User type to the LocationRepository, or should I be happy with the status quo and live with a sequence diagram that "feels" wrong to me (i.e. retrieval of the location information feels like it is being retrieved from the wrong "point of view")?


Solution

  • I would approach this from a perspective of maintainability. I agree that doing it with the facade on LocationRepository would "feel right", and would probably make the code slightly more readable.

    The tradeoff, as you said, would be more code to maintain. But how much code are we talking about? Is it a lot, and would you have to update it often? Or can you write it once and forget it, and have it be easily unit testable? If it's the former, just swallow it, go with the current implementation, and remind yourself it doesn't really affect functionality. If it's the latter, it might be worth putting in the effort for that good feeling and more readable code elsewhere.