During a code review I looked at set of repository classes (in vb.net). I am used to seeing these repository classes full of functions that return collections (among other things) of your domain objects. However, this repository had 1 public property and 1 private variable that looked something like this:
Private _item as Collection (of Customer)
Public Item as Collection (of Customer)
Get...
Set...
In the “Get”, there is code that gets a the Customers from the DAL and loads it in the private _item.
What would the benefits be in using a property (customerRepository.Item) instead of plain old function (customerRepository.GetAllCustomers)? The “Property” way looks odd to me but odd doesn’t always mean wrong.
In this example, the getter is returning the whole collection and the user is allowed to get items from it. In a Repository Pattern the repository IS the collection, and you interact with it with collection semantics to get a particular instance of the entity the repository is supposed to be holding.
The danger in this implementation is that users of this API have the ability of replacing the collection with another collection. This is bad practice, in my opinion