I am using the repository pattern where I have one repository class per database table. I was wondering how you guys approach queries that only need to return a specific number of columns
For example say I have the following
Item Table (fictional table)
ItemId
Name
PurchaseDate
Description
Price
In my code I create an object with the fields above called Item.cs (currently not using an orm).
If I have multiple scenarios where I need to return
Which would be the best approach?
Now imagine this scenario with a table with over 10 fields.
Personally, I like option one, but I'm not sure if there is a better way to go about this.
Your repositories should return domain objects and the client of the repository can decide if it needs to do the mapping. By mapping the domain objects to view models (or something else) inside a repository, you prevent the client of your repositories from getting access to the underlying domain object.
The Repository pattern implements separation of concerns by abstracting the data persistence logic in your applications. Design patterns are used as a solution to recurring problems in your applications, and the Repository pattern is one of the most widely used design patterns.
Short answer: No. Long answer: repository is responsible for turning persisted data back to entities (models) and vice versa.
In most cases, it suffices to have a single entity-DTO conversion, which tends to happen in the business layer (BLL), which is one layer above the repository layer (DAL). In that sense, your repositories should not return a DTO.
I add methods to my repositories when I need them in contrast to generic repositories where you get a set of methods no matter if you need them or not.
Returning IQueryable
is a leaky abstraction.
Take a look at Domain Driven Design (the book) and you'll get a good idea of how well designed repositories should look like.
I've also written a rant about generic repositories: http://blog.gauffin.org/2012/02/generic-repositories-a-silly-abstraction-layer/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With