MicroRuleEngine icon indicating copy to clipboard operation
MicroRuleEngine copied to clipboard

Match against collection of polymorphic types

Open cocowalla opened this issue 2 years ago • 0 comments

Thanks for this library, it's wonderfully straightforward!

Now, my problem - consider a class and hierarchy like this:

public class Order
{
    public IList<IItem> Items { get; set; }
}

public interface IItem
{
    Guid Id { get; set; }
}

public class StandardItem : IItem
{
    ...
}

public class SpecialItem : IItem
{
    public string Name { get; set; }
}

What I'd like to do is create a Rule that can match on the Items collection for a specific type of IItem, for example to match only on SpecialItem instances:

var rule = Rule.Any(nameof(Order.Items), Rule.Create(nameof(SpecialItem.Name), MreOperator.Equal, "My Special Item Name"));

The above doesn't work, because:

MicroRuleEngine.RulesException: Cannot find property Name in class IItem ("Name")

I guess what's really needed here is some way to "pre-filter" the collection with Linq's .OfType<SpecialItem>(). Is something like this possible already? Otherwise, are you able to offer a few pointers about the best way to add this functionality? (happy to submit a PR)

cocowalla avatar Jun 07 '22 10:06 cocowalla