给定以下域模型
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public List<Car> Cars { get; set; }
}
public class Car
{
public int Year { get; set; }
public string Make { get; set; }
}
是否可以编写一条规则,对 30 岁以下的人拥有的所有 2016 年以后的汽车执行操作?我这样做的同时只将 Person 对象作为事实插入。
Person p1 = new Person("Jim", 31);
p1.Cars = GetCars(4);
Person p2 = new Person("Bob", 29);
p2.Cars = GetCars(4);
session.Insert(p1);
session.Insert(p2);
我试过这样的事情。我猜如果我在 Car 中添加一个对拥有它的 Person 的引用,我可以让它工作,但我的实际用例会使这变得困难。我希望我只是错过了一些东西。
public class CarTest : Rule
{
public override void Define()
{
Person person = null;
IEnumerable<Car> cars = null;
When()
.Match<Person>(() => person, p => p.Age < 30)
.Query(() => cars, x => x
.Match<Car>(c => c == person.Cars.Find(f=> f.Make == c.Make && f.Year == c.Year), c => c.Year > 2016)
.Collect()
.Where(p => p.Any()));
Then()
.Do(ctx => DoSomethingWithNewCarsThatBelongToYoungPeople(cars));
}
private static void DoSomethingWithNewCarsThatBelongToYoungPeople(IEnumerable<Car> cars)
{
foreach (var car in cars)
{
//Do Something
}
}
}