2

I have a generic function GetDocuments<T> that's querying the CosmosDB API. The generic is constrained by a custom IDocument interface. At the moment, I'm passing an enum as an argument to this function that determines the type of the document -- however, my interface has the document type as a property, so it seems like I should be able to access that somehow instead of passing another arg.

Because my argument is in an Expression, I'm not sure how to access that value (I'm not sure if using the API to access expression params is the right approach). If I had an IDocument as an argument, it seems pretty straightforward to access it.

Given this code, how can I access the DocumentType without passing it to GetDocuments<T>?

Function definition:

public IEnumerable<T> GetDocuments<T>(Expression<Func<T, bool>> predicate, Enumerations.DocumentType type) where T : IDocument
{
    var results = Client.CreateDocumentQuery<T>(GetDocumentCollectionUri(), GetFeedOptions())
        .Where(predicate)
        .Where(s => s.DocumentType == type)
        .ToList();

    return results;
}

Interface definition:

public interface IDocument
{
    [JsonProperty(PropertyName = "id")]
    string Id { get; set; }
    [JsonProperty(PropertyName = "documentType")]
    Enumerations.DocumentType DocumentType { get; }
}

Function call:

var messages = mailboxRepository.GetDocuments<MailboxMessageTemplate>(
            s => s.UserId == user.ID,
            Enumerations.DocumentType.MessageTemplate);
3
  • An expression is just a tree around a predicate. What do you mean that your argument is in the expression? Can you write an example of what you'd expect the outcome to be? Jan 14, 2019 at 18:23
  • Oh I think I see what you mean. Let me write up an answer. Jan 14, 2019 at 18:26
  • 1
    I don't think this is possible. T is a type argument, not an instance of T. T is guaranteed to be IDocument, but it isn't an instance of it, so you can't query any of its properties. The only thing that might is work is created an instance of T with new T() (you will need a 'new()' generic constraint) and then ask your new instance it's document type. Seems wasteful though
    – Dave
    Jan 14, 2019 at 18:34

2 Answers 2

2

You can do that by pre-creating your expression and just adding it to your query.

Here is the expression that would do the trick.

internal static Expression<Func<T, bool>> TypeSpecificExpression<T>() where T : class
{
    var parameter = Expression.Parameter(typeof(IDocument));
    var member = Expression.Property(parameter, nameof(IDocument.Enumerations.DocumentType));
    var contant = Expression.Constant(nameof(T));
    var body = Expression.Equal(member, contant);
    var extra = Expression.Lambda<Func<T, bool>>(body, parameter);
    return extra;
}

You can then simple change your method to be:

public IEnumerable<T> GetDocuments<T>(Expression<Func<T, bool>> predicate) where T : IDocument
{
    var results = Client.CreateDocumentQuery<T>(GetDocumentCollectionUri(), GetFeedOptions())
        .Where(predicate && TypeSpecificExpression())
        .ToList();

    return results;
}

Obviously I don't have access to the Enumerations.DocumentType enum so you might need to do some tweeting on the value you are setting here: var contant = Expression.Constant(nameof(T));

On a side note, you should not be calling .ToList() like that on CreateDocumentQuery. You are synchornizing a query that can be a serious performance hit. You should be using the .AsDocumentQuery() method to get the query and then call query.ExecuteNextAsync when query.HasMoreResults.

On a second side note, it looks like you are trying to build something that the library Cosmonaut already does, including the feature you just asked a question for (you can find that method here). It's worth taking a look.

Disclaimer: I made Cosmonaut

0

So you are looking for a way to convert an Expression<Func<T, bool>> to a Func<T, bool>?

You can call Compile.

Compiles the lambda expression described by the expression tree into executable code and produces a delegate that represents the lambda expression.

var results = Client.CreateDocumentQuery<T>(GetDocumentCollectionUri(), GetFeedOptions())
    .Where(predicate.Compile())
    .Where(s => s.DocumentType == type)
    .ToList();
3
  • OP wants to remove the need to pass a value of DocumentType to the method. OP wants to get it from the information providing by the type argument <T>
    – Dave
    Jan 14, 2019 at 18:31
  • @Dave Do you mean that each type that implements IDocument corresponds to one value of Enumerations.DocumentType?
    – Sweeper
    Jan 14, 2019 at 18:36
  • 1
    I'm not sure, you'll have to ask OP. All I was saying was that OP wants to remove the need to have the DocumentType argument in their method signature (at least that's what their question seems to imply)
    – Dave
    Jan 14, 2019 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.