180 likes | 512 Views
Implementing the Repository Pattern. Mike Hadlow Mikehadlow@yahoo.com http://mikehadlow.blogspot.com. Data Access Taxonomy. Record Set / Data Set Record Set / Data Set + Business Object Active Record Business Object + Data Access Object Domain Model + ??. Repository. Aggregate.
E N D
Implementing the Repository Pattern Mike Hadlow Mikehadlow@yahoo.com http://mikehadlow.blogspot.com
Data Access Taxonomy • Record Set / Data Set • Record Set / Data Set + Business Object • Active Record • Business Object + Data Access Object • Domain Model + ??
Repository • Encapsulate data access • Simple collection like interface • Should only return aggregate roots • Provide for adding and removing entities • Provide methods to select objects based on some criteria • Allow easy substitution
QueryMethods Customer[] WithSurname(string surname)
Specification Customer[] Find(ICustomerSpecification spec) boolICustomerSpecification.IsSatisfiedBy(Customer c) • Part of the Domain Model • Composable • Can define in-memory predicates and query predicates (SQL?) • Should we surface tool specific queries?
Generic Repository IRepository<T> T[] Find<T>(ISpecification<T> specification)
Is it a repository? • Does it show intent? • ICanDelete? ICanInsert? ICanUpdate? • Is polymorphism useful here? • But it’s ‘out there’!
IQueryable? IQueryable<T> GetAll<T>() var customers = repository.GetAll().ThatMatch(criteria) .AsPagedList(pageNumber, pageSize); • SQL execution outside repository boundary • Query logic can seep into non-domain parts of the application
Should Repositories control transactions? • Transactions should not be controlled by the repository. • Unit of Work (Nhibernate session, L2S DataContext) should not be controlled by the repository.
Who consumes Repositories? • Views? • Controllers? • Domain Services? • Domain Entities?
Questions? Mike Hadlow mikehadlow@yahoo.com http://mikehadlow.blogspot.com