runxc1 / MicroRuleEngine

A .Net Rule Engine for dynamically evaluating business rules compiled on the fly.
MIT License
186 stars 74 forks source link

Match against collection of polymorphic types #33

Open cocowalla opened 2 years ago

cocowalla commented 2 years ago

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)