2012-10-11 25 views
5

Tôi đã đọc qua một số câu hỏi khác tương tự như câu hỏi này - nhưng là mới đối với Nhibernate, không ai trong số họ dường như trả lời câu hỏi của tôi tại sao Inhibernate ném một một bộ sưu tập: Order.ShippingAddress.Items" để đoạn mã sau:Nhibernate Throws Tìm thấy các tham chiếu được chia sẻ cho một bộ sưu tập

VendorOrderNotificationAcknowledgement ICheckoutVendorService.SendOrderNotification(VendorOrderNotification request) 
{ 
     OrderRepository repo = new OrderRepository(); 
     var order =(AbstractOrder) repo.FindByCartId(request.OrderNotification.CartOrderId); 

     ShippingAddress billingAddress = order.ShippingAddresses[0]; 
     var itemsFromDb = billingAddress.Items; 
     order.ClearAllShippingAddresses(); 
     wcfFactory.UpdateOrder(order); //NO ERROR THROWN HERE! 
     ShippingAddress shippingAddress = orderHelper.CreateShippingAddress(request.ShippingDetails); 
     shippingAddress.Items = itemsFromDb; 
     order.AddShippingAddress(shippingAddress); 

     order.SourceCode = _sourceCode; 
     order.TaxAmount = 0; 
     order.GiftCertificateAmount = 0; 
     order.Status = StatusCode.Approved; 
     order.CreatedAt = request.OrderNotification.OrderTime.Year >2010 
      ? request.OrderNotification.OrderTime 
      : DateTime.Now; 
     order.PurchasedDate= 
            request.OrderNotification.OrderTime.Year>2010 
      ? request.OrderNotification.OrderTime 
      : DateTime.Now; 
     order.UpdatedAt = DateTime.Now; 

     if (request.OrderNotification.OrderCharges != null) 
     { 
      order.ShippingAmount = request.OrderNotification.OrderCharges.Shipping; 
      order.TaxAmount = request.OrderNotification.OrderCharges.DutyAndTaxes; 
     } 
     else 
     { 
      order.ShippingAmount = 0; 
      order.TaxAmount = 0; 
     } 
     order.UseGiftWrap = false; 
     order.SourceCode = _sourceCode; 
     UpdateEshopWorldOrder(order); // THROWS FOUND SHARED REFERENCES TO A COLLECTION: ORDER.SHIPPINGADDRESS.ITEMS 

     var orderDto = orderHelper.CreateOrderDto(billingAddress, orderHelper, order); 
     var dtoShippingAddresses = orderHelper.CreateDtoShippingAddresses(order); 
     orderDto.ShippingAddresses = dtoShippingAddresses; 

     ShippingMethodDto shippingMethodDto = 0; 

     var mine = wcfFactory.SendOrder(orderDto); 

     //More Code below here ... 

} 


public OrderDto CreateOrderDto(ShippingAddress billingAddress, OrderHelper orderHelper, AbstractOrder order) 
{ 
    OrderDto orderDto = new OrderDto(); 
    orderDto.AlternateOrderId = order.Id.ToString(); 
    orderDto.ConfirmationNumber = order.ConfirmationNumber; 
    orderDto.Coupons = new string[0]; 
    orderDto.DiscountAmount = order.DiscountAmount; 
    orderDto.GiftCardAmount = order.GiftCertificateAmount; 
    orderDto.PurchaseDate = order.PurchasedDate; 
    orderDto.ShippingAmount = order.ShippingAmount; 
    orderDto.SourceCode = order.SourceCode; 
    orderDto.TaxAmount = order.TaxAmount; 
    orderDto.UseGiftWrap = order.UseGiftWrap; 
    var customerDto = orderHelper.CreateCustomerDto(billingAddress); 
    orderDto.SoldTo = customerDto; 
    return orderDto; 
} 

public void UpdateEshopWorldOrder(AbstractOrder order) 
{ 
    try 
    { 
     //Session.Update(order); 
     // transaction.Commit(); 
      Session.Flush(); 
    } 
    catch (Exception ex) 
    { 
     _logger.Debug("order saved failed with an error of " + ex.Message); 
     _logger.Error(ex); 
     throw; 
     } 
} 

Bất kỳ hiểu biết sâu sắc được đánh giá cao .... thnx

Trả lời

8

tôi nghĩ vấn đề là, rằng itemsFromDB đối tượng bộ sưu tập của bạn được tham chiếu bởi shippingAddress và cũng bởi billingAddress.

Cả hai thực thể đều cần các đối tượng thu thập của riêng chúng. Cả hai bộ sưu tập tuy nhiên có thể chứa các tham chiếu đến cùng một đối tượng địa chỉ.

Vì vậy, tôi giả định thay thế shippingAddress.Items = itemsFromDb; với một cái gì đó giống như

shippingAddress.Items.AddRange(itemsFromDb)

hoặc

shippingAddress.Items = new List<ShippingAddress>(itemsFromDb) nên làm các trick

+0

Tốt, câu trả lời rõ ràng mà chỉ lưu bacon của tôi! – Greg

Các vấn đề liên quan