I have a service, that is communicating via OData with other Services. The project is using the nuget Simple.OData.Client here and the company has provided an interface for the communication with a function, that I have to use.
the function looks like this:
private async Task<IEnumerable<T>> FetchEntitiesAsync<T>(string prefix, Action<IBoundClient<T>> configure,
CancellationToken token) where T : class
{
IBoundClient<T> query = _client.For<T>(prefix);
configure(query);
try
{
this.Logger.LogDebug($"Fetching {_baseUri}/{prefix} via OData...");
IEnumerable<T> entities = await query.FindEntriesAsync(token);
return entities;
}
catch (Exception e)
{
this.Logger.LogError(e, $"{prefix} acquisition OData exception");
this.Logger.LogError(e.StackTrace);
throw e;
}
}
Now I have to provide a Action<IBoundClient>. Actually I can't figure out how to really work with this.
I can do something like this:
Action<IBoundClient<MyClass>> filter = (q => q.Filter(i => i.Property1 == myProperty1).Expand(i => i.MyParameters));
FetchEntityAsync("MyClass", filter, token);
But for some cases i need to put more logic into the filter object. Than i came up with a solution like
Action<IBoundClient<MyClass>> filter;
if (string.IsNullOrEmpty(property2))
{
filter = (q => q.Filter(i => i.Property1 == myProperty1)
.Expand(i => i.Columns));
}
else
{
filter = (q => q.Filter(i => i.Property1 == myProperty2)
.Filter(i => i.Property2.ToLower() == property2.ToLower())
.Expand(i => i.Columns));
}
FetchEntityAsync("MyClass", filter, token);
This will become very messy with increased complexity I want to do something similar to EntityFramework where I can work with a Queryable like
myQuery = myQuery.Where(x=> x.Property1 = myProperty1);
if(!string.isNullOrEmpty(property2){
myQuery = myQueryable.Where(x=> x.Property2.toLower() = property2.toLower());
}
[...]