How to implemented this using Repository Pattern (or something more appropriate)?
I have a repository called: IChocolateRepository
It derives from 开发者_开发知识库a generic repository interface IRepository<> which defines basic repository functions (FindAll, Delete, Add, etc.)
Now, I need to write a piece of code which will go and delete 'all chocolates from the database with expiry data before a supplied date' (assuming that ExpiryDate is a non-nullable property on the Chocolate model class.)
Does this belong in the repository?
Also, how would a sample implementation look like? (I am using EF CTP5)
I don't think so.
This does not belong in the Repository
but in a class that operates the Transactions
using these Repositories
.
Unless this is just too simple application where avoiding a separate layer to avoid complexity would be much better.
If you already have Remove(T item) method in base interface then it won't hurt if you add Remove(Predicate<'T'> condition) to satisfy your needs.
Therefore instead of deleting one particular item you can have a very generic interface to delete everything that satisfies the given predicate.
I had a small project (FYP for university degree) and due to time constraints and that i didn't know better I added my more complex data methods to the repository, hard coded.
Personally I would use Andrei Taptunov idea with predicates, so that data access code is contained within the repository class. I have seen transactions written in a domain driven design book, which work like predicates anyway. A transaction records what you want to achieve and a repository works on that request.
精彩评论