c# - Exposing Entity Framework extensions to a Service Layer -
i've implemented data layer using irepository
pattern. 1 of these methods returns iqueryable
:
public virtual iqueryable<tentity> queryable() { return dbset; }
i have service layer calls method supplies filtering e.g.:
public user getbyname(string name) { return _repository.queryable() .where(a => a.name == name) .tolist(); }
so far good, entity framework referenced in data layer, service layer knows nothing this.
now, want 2 additional things:
- use
include
method - use
tolistasync()
method
so service method looks this:
public async task<user> getbyname(string name) { return await _repository.queryable() .where(a => a.name == name) .include(x => x.pets) .tolistasync(); }
the issue both include()
, tolistasync()
both in system.data.entity
namespace in entityframework.dll
.
i didn't want reference ef within service layer, shouldn't care or know this, can't see how else solve while keep architecture clean. solutions can see are:
- add entity framework service layer
- add new class data layer (
userdata
) wrapsirepository<user>' , handles 2 additional methods required. service take use instance of rather than
irepository`.
any suggestions how solve while still adhering best practices?
i understand want abstract away ef in repository layer, based on best practices. use cases (your main question here) contradiction of - want expose ef offers service layer.
it sounds need define strict line responsibility of repository is. if it's return iqueryable
, benefit of wrapping ef @ all? there other repository implementation plan use in production? if so, it's going hard ensure both repo types correct, when expose iqueryable
. pain point seeing right now.
in opinion, repository should define how data. should expose simple calls getallusers
returns actual models. when let business/service layer define own way data, makes repository feel redundant.
Comments
Post a Comment