I haven't been able to find an answer to my question so far, and I suppose I have to ask my first question sometime. Here goes.
I have a Data Access Layer responsible for interacting with various data storage elements and returns POCOs or collections of POCOs when querying things.
I have a Business Layer that sits on top of this and is responsible for implementing business rules on objects returned from the Data Access Layer.
For instance, I have an SQL Table of Dogs, my data access layer can return that list of dogs as a collection of Dog objects. My business layer then would do things like filter out dogs below a certain age, or any other filtering or transformation that had to happen based on business rules.
My question is this. What's the best way to handle filtering objects based on related records? Let's say I want all the people who have Cats. Right now my data access layer can return all the cats, and all the people, but it doesn't do any filtering for me.
I can implement the filtering via a different data access method (i.e. DAO.GetCatPeople()) but this could get complicated if I have a multitude of related properties or relationships to handle
I can return all from both sides and do the matching myself all in the business layer, which seems like a lot of extra work and not fully utilizing the SQL server.
I can write a data filtration interface; if my data access layer changes, this layer would have to change as well.
Are there some known best practices here I could be benefiting from?
The view I take is that there's two "reasons" why you'd access data: Data centric and Use Case centric.
I think both types are valid. For the use case driven ones it's going to be mostly driven by business focused use cases, but I can see edge cases where they could be more technically driven - I'd say that was ok as long as they didn't violate any business rules or pervert your domain model.
Should Cats and Dogs know about each other? If they exist within the same domain model, and have established relationships within that model - then yes of course you should be able to make queries like GetCatPeople()
.
As far as managing the complexity goes, rather than GetCatPeople()
you could have a more generic method that took an attribute as a parameter: GetPeopleByAnimal(animal)
.