nHibernate 从子子类中预先加载关系
假设我有以下类定义:
public class Order
{
public virtual int Id { get; protected set; }
public virtual IList<OrderItemBase> OrderItems { get; set; }
}
public class OrderItemBase
{
public virtual int Id { get; protected set; }
public virtual string OrderRef { get; set; }
}
public class ProductOrder : OrderItemBase
{
public virtual Product Product { get; set; }
}
public class OtherOrder : OrderItemBase
{
public virtual string MiscData { get; set; }
}
public class Product
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
}
映射如下:
public class OrderMap : ClassMap<Order>
{
this.Id(x => x.Id);
this.HasMany(x => x.OrderItems).Inverse().KeyColumn("OrderId").Cascade.All();
}
public class OrderItemMap : ClassMap<OrderItembase>
{
this.Id(x => x.Id);
this.Map(x => x.OrderRef);
this.DiscriminateSubClassesOnColumn("OrderTypeId");
}
public class ProductOrderMap : ClassMap<ProductOrder>
{
this.DiscriminatorValue(1);
this.References(x => x.Product).Column("ProductId");
}
public class OtherOrderMap : ClassMap<OtherOrder>
{
this.DiscriminatorValue(2);
this.Map(x => x.MiscData);
}
public class ProductMap : ClassMap<Product>
{
// ... can't be arsed to type more code in here, you get the idea
}
现在提出问题:
当我为订单存储库编写查询时,我需要一些方法来预先加载所有OrderItems:
return this.All()
.Where(o => o.Id == id)
.FetchMany(o => o.OrderItems)
以及一些还加载相关 ProductOrder 产品项目的方法:
return this.All()
.Where(o => o.Id == id)
.FetchMany(o => o.OrderItems) // Ok so far...
.ThenFetch(oi => oi.Product) // Uh-oh, the base order item doesn't have Product
如何让子类项目立即加载其关系,但仅在我需要时才加载?
Say I have the following class definitions:
public class Order
{
public virtual int Id { get; protected set; }
public virtual IList<OrderItemBase> OrderItems { get; set; }
}
public class OrderItemBase
{
public virtual int Id { get; protected set; }
public virtual string OrderRef { get; set; }
}
public class ProductOrder : OrderItemBase
{
public virtual Product Product { get; set; }
}
public class OtherOrder : OrderItemBase
{
public virtual string MiscData { get; set; }
}
public class Product
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
}
With mappings along the lines of:
public class OrderMap : ClassMap<Order>
{
this.Id(x => x.Id);
this.HasMany(x => x.OrderItems).Inverse().KeyColumn("OrderId").Cascade.All();
}
public class OrderItemMap : ClassMap<OrderItembase>
{
this.Id(x => x.Id);
this.Map(x => x.OrderRef);
this.DiscriminateSubClassesOnColumn("OrderTypeId");
}
public class ProductOrderMap : ClassMap<ProductOrder>
{
this.DiscriminatorValue(1);
this.References(x => x.Product).Column("ProductId");
}
public class OtherOrderMap : ClassMap<OtherOrder>
{
this.DiscriminatorValue(2);
this.Map(x => x.MiscData);
}
public class ProductMap : ClassMap<Product>
{
// ... can't be arsed to type more code in here, you get the idea
}
Now for the question bit:
When I come to write a query for the order repository, I want some methods to eager-load all the OrderItems:
return this.All()
.Where(o => o.Id == id)
.FetchMany(o => o.OrderItems)
And some methods to also load the related ProductOrder Product items:
return this.All()
.Where(o => o.Id == id)
.FetchMany(o => o.OrderItems) // Ok so far...
.ThenFetch(oi => oi.Product) // Uh-oh, the base order item doesn't have Product
How can I get a sub-classed item to eager-load it's relationship but only when I want it to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论