Search code examples
entity-framework-4repository-patternobjectquery

Object Query pattern using EF 4 and repository pattern


A question regarding the repository pattern and query object pattern. I'm using EF 4 and have generated my POCO classes from my database model using the ADO.NET POCO Entity Generator in VS 2010. The edmx file and the tt file (POCO classes) are in 2 different projects.

My repositories are domain specific, e.g DocumentRepository and UserRepository. My database model differs from my domain model in such a degree that I have implemented mappers in order to translate a domain object to one or more database tables (and vice versa). One example is that my Document domain class is modeled as 3 tables (and therefore POCO classes) in the database.

How would you implement the query object pattern when using domain objects in such a case? The way I see it I'll have to write the query object base on the POCO classes and not the domain classes? But wouldn't that break the repository pattern?


Solution

  • ORM is usually used in the way that it works directly with domain objects = it loads them from database and it persist them to database. You are doing one more abstraction step where you are using ORM entities only to fill your custom objects. Your custom objects are completely out of scope of your ORM tool and you cannot expect that ORM tool will provide you any support for queries build on top of your domain objects. You must built your own query support and translate domain queries to ORM queries inside your repositories. This is usually done by implementing Specification pattern.

    Btw. in such scenario POCOs doesn't make too much sense - POCOs are for scenarios where you want to use them as domain objects).