I have been given a change to rewrite an old framework we use, and to implement the repository and unit of work patterns, but in an attempt to not rewriting all queries I have ended up with a weird combination of Linq queries and Linq lambdas.
I was hoping someone could explain to me a better way of doing this, or advice what to do.
First off, the generic repository has a method Get()
defined as
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
and the old code would use a context directly like this:
Arrival arrival =
(from a in DataContextPortX.Arrival
where a.I_ARRIVALNO == arrivalNo
select a).SingleOrDefault();
And now, since the class no longer has direct access to the context I can choose to keep the old code with the Get()
method mixed in like this:
Arrival arrival =
(from a in Get()
where a.I_ARRIVALNO == arrivalNo
select a).SingleOrDefault();
or I can rewrite the whole query to lambda like this:
var arrival = Get(a => a.I_ARRIVALNO == arrivalNo).SingleOrDefault();
I would of course prefer to rewrite all queries but there are a lot of them, and this was the shortest I could find, others are like really long.
But I am worried about first using Get()
and then iterating of that, but for all I know it doesn't matter.
Get()
defers execution. If it forces execution prior to return, you may want to pass the filter expression so it is included as part of the executed SQL statement. \$\endgroup\$