2012-11-10 58 views
1

我打開一個新的MessagesWindow其中有這樣的代碼:混淆安裝/拆卸和不同的ObjectContext對象

namespace Newsletter.UI 
{ 
    public partial class MessagesWindow : Window 
    { 
     MessageService msgService; 
     public MessagesWindow() 
     { 
      InitializeComponent(); 
      msgService = new MessageService(); 
     } 

     private void CreateButtonClick(object sender, RoutedEventArgs e) 
     { 
      var createMessageWindow = new CreateMessageWindow { Owner = this }; 
      createMessageWindow.ShowDialog(); 
      gridMessages.ItemsSource = from msg in msgService.GetAllMessages() 
             select msg; 
     } 

     private void messagesWindow_Loaded(object sender, RoutedEventArgs e) 
     {   
      gridMessages.ItemsSource = from msg in msgService.GetAllMessages() 
             select msg; 
     } 

     private void EditButtonClick(object sender, RoutedEventArgs e) 
     { 

      if (gridMessages.SelectedItem == null) 
       MessageBox.Show("You have to choose the message first."); 
      else 
      { 
       var selectedMessage = (Message)gridMessages.SelectedItem; 

       var editMessageWindow = new EditMessageWindow(selectedMessage); 
       editMessageWindow.Owner = this; 
       editMessageWindow.ShowDialog(); 
      } 
      gridMessages.ItemsSource = from msg in msgService.GetAllMessages() 
             select msg; 
     } 
    } 
} 

我打開與EditButtonClick一個新的窗口,在這裏我想修改我Message。該EditMessageWindow的代碼:

namespace Newsletter.UI 
{ 
    public partial class EditMessageWindow : Window 
    { 
     private Message editMessage; 
     private MessageService messageService; 
     public EditMessageWindow(Message message) 
     { 
      InitializeComponent(); 
      editMessage = message; 
      messageService = new MessageService(); 

     } 

     private void CreateMessageWindowLoaded(object sender, RoutedEventArgs e) 
     { 
      subject.Text = editMessage.Subject; 
      messageContent.AppendText(editMessage.Content); 
      senderComboBox.ItemsSource = messageService.GetAllSenders(); 

      mailingListComboBox.ItemsSource = messageService.GetAllMailingLists(); 

     } 

     private void SaveClick(object sender, RoutedEventArgs e) 
     {    
      editMessage.Date = DateTime.Now; 
      editMessage.Content = messageContent.Text; 
      editMessage.MailingList.Add(messageService.GetAllMailingLists()[mailingListComboBox.SelectedIndex]); //ERROR 
      editMessage.Sender = messageService.GetAllSenders()[senderComboBox.SelectedIndex]; 
      editMessage.Subject = subject.Text; 
      editMessage.HasAttachments = false; 
      messageService.EditMessage(editMessage); 

      Close(); 
     } 
    } 
} 

我得到SaveClick的錯誤:

兩個對象之間的關係不能被定義,因爲它們 連接到不同ObjectContext的對象。

最後,MessageService代碼:

namespace Newsletter.Services 
{ 
    public class MessageService 
    { 
     private NewsletterEntities _context; 

     public MessageService() 
     { 
      _context = new NewsletterEntities(); 
     } 
     public void AddMessage(Message newMessage) 
     { 
      _context.Messages.AddObject(newMessage); 
      _context.SaveChanges(); 
     } 
     public void EditMessage(Message editMessage) 
     { 
      var messageToChange = (from m in _context.Messages where editMessage.MessageID == m.MessageID select m).FirstOrDefault(); 
      if (messageToChange != null) 
      { 
       messageToChange = editMessage; 
       _context.SaveChanges(); 
      } 
     } 

     public List<Message> GetAllMessages() 
     { 
      return (from m in _context.Messages select m).ToList(); 
     } 

     public List<MailingList> GetAllMailingLists() 
     { 
      var query = from m in _context.MailingLists select m; 
      return query.ToList(); 
     } 

     public List<Sender> GetAllSenders() 
     { 
      var query = from m in _context.Senders select m; 
      return query.ToList(); 
     } 

     public void DeleteMessage(Message message) 
     { 

      List<MailingList> mailingLists = new List<MailingList>(); 
      foreach(MailingList ml in message.MailingList.ToList()) 
      { 
       ml.Message.Remove(message); 
      } 
      _context.Messages.DeleteObject(message); 
      _context.SaveChanges(); 
     } 
    } 
} 

我越來越consfused所有這些層和背景。我知道我應該以某種方式分離信息,對它做些什麼並附加,但我不知道在哪裏以及如何。我應該在MessageService的服務層,UI層或DAL層中執行此操作嗎?還是應該重新設計我的解決方案,並在不同的地方完成上下文邏輯?

回答

1

您似乎有每個窗口上下文的設置。這將對您如何維護上下文中的對象產生兩方面的影響。

  1. 你不想從窗體傳遞對象到窗體。相反,您將從表單中傳遞ID以在上下文中形成和加載實體。
  2. 由於實體在一個上下文中更新,您需要通知其他窗口/上下文以便他們可以刷新其數據。

這裏是一個MSDN文章,詳細說明每個表單的上下文過程。文章使用NHibernate的,但過程將是幾乎相同的EF和DbCotext

http://msdn.microsoft.com/en-us/magazine/ee819139.aspx

+0

感謝,傳遞ID起來很順手。 – pmichna