【发布时间】:2011-04-16 03:32:01
【问题描述】:
我在客户端和服务器端有一个 .Net 应用程序,服务器提供 REST 服务(使用 WCF)。我有这样的服务定义:
[WebGet(UriTemplate = "/Customers/{id}")]
Customer GetCustomerById(string id);
[WebGet(UriTemplate = "/Customers")]
List<Customer> GetAllCustomers();
使用 Fluent NHibernate 和延迟加载将 Customer 类及其朋友映射到数据库。如果我从会话范围之外的服务返回,则服务调用将失败,因为它无法序列化引用的延迟加载的 Orders 属性(请参阅最后的类 def)。问题是我需要延迟加载,因为我不希望我的GetAllCustomers-service 获取所有引用的订单。所以我想做的是以某种方式通知序列化程序,这样它就不会尝试在 GetAll 上序列化 Orders。但请注意,必须在 GetCustomerById 上序列化相同的属性 - 所以我必须在服务上指定它。这可以吗?!
类:
public class Customer
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Order> Orders { get; set; }
}
public class Order
{
public virtual int Id { get; set; }
// ++
}
【问题讨论】:
标签: .net wcf nhibernate rest serialization