简体   繁体   中英

Repository Pattern and Find Functionality in Entity Framework

It is a good pattern to separate the way objects are persisted from the business logic. If I expose how objects are persisted in the database to the business logic, then when I change how these objects are persisted, I will have to change the business logic, making them tightly coupled.

With that being said, suppose that the business logic uses Foo as an object, and suppose that the DAL (using EF) uses FooDbModel as an object for persisting. Building the CRUD operations with the repository pattern is very simple: Get Foo object, build FooDbModel , do whatever is needed, and build Foo back, and return it.

But when Find functionality is little more involving. Ideally, I want to do something like:

Repository.Find(f => f.Name == "something");

where f is of type Foo (not FooDbModel ). Is that possible with EF? I don't want to pass f where if is of type FooDbModel because that is exposing the data persistence to the BL.

Is there a trick to do that?

@IvanStoev solved the problem.

Basically, I will need to do a select and then a where.

To quote him:

Not if you do it on IQueryable. Not sure how exactly your methods are structured, but the implementation of the Find method I see is something like context.Set().Select(m => new Foo { ... }).Where(f => ...) which should translate to SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM