I am using Visual Studio, C# and SQLite for a Xamarin application and all is working well. However I am using a generic repository that I would like to extend to be able to add to the GET to add a check against 2 properties if the generic is of a specific type.
Here is the Generic respository pattern I am following
My GET as it sets right now looks like this:
public List<T> Get<TValue>(Expression<Func<T, bool>> predicate = null, Expression<Func<T, TValue>> orderBy = null)
{
var query = db.Table<T>();
if (predicate != null)
query = query.Where(predicate);
if (orderBy != null)
query = query.OrderBy<TValue>(orderBy);
return new List<T>(query);
}
However I would like to add something like this (pseudo code):
Where RecordOwner is the Type and RecordOwnerCompanyId and RecordOwnerUserId are the properties I would like to add to the predicate/where clause. Keep in mind that hte lambda does not work and that this is for SQLite(query is of type TableQuery<T>
)
public List<T> Get<TValue>(Expression<Func<T, bool>> predicate = null, Expression<Func<T, TValue>> orderBy = null)
{
var query = db.Table<T>();
if (predicate != null)
query = query.Where(predicate);
if (orderBy != null)
query = query.OrderBy<TValue>(orderBy);
// Perform record owner checks
if (typeof(RecordOwner).IsAssignableFrom(typeof(T)))
{
query = query.Where(x => RecordOwnerCompanyId == 1234 && x.RecordOwnerUserId == 1234);
}
return new List<T>(query);
}
Any help is greatly appreciated.
UPDATE:
I do have a working solution that will get my what I want however I would much prefer to solve this problem with the predicate , as with the way shown below it is using more memory with 2 lists. It has not been a negative impact on performance but is not the ideal solution I was looking for. If someone knows how I can do this all with the predicate that would be great.
New working example (not using predicate):
public List<T> Get<TValue>(Expression<Func<T, bool>> predicate = null, Expression<Func<T, TValue>> orderBy = null)
{
var query = db.Table<T>();
if (predicate != null)
query = query.Where(predicate);
if (orderBy != null)
query = query.OrderBy<TValue>(orderBy);
var memUser = JstMem.Default.User;
// Perform record owner checks
if (typeof(RecordOwner).IsAssignableFrom(typeof(T)))
{
return new List<T>(query).Where(x => (x as RecordOwner).RecordOwnerCompanyId == memUser.SelectedCompanyId && (x as RecordOwner).RecordOwnerUserId == memUser.UserId).ToList();
}
return new List<T>(query);
}