1

我打开一个新的MessagesWindow,其中包含以下代码:

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();
        }
    }
}

我对所有这些层和上下文感到困惑。我知道我可能应该以某种方式分离消息,对其进行处理并附加,但我不知道在哪里以及如何。我应该MessageService在我拥有所有实体的服务层、UI 层还是 DAL 层中执行此操作?或者我应该重新设计我的解决方案并在不同的地方执行上下文逻辑?

4

1 回答 1

1

你似乎有每个窗口的上下文设置。这将对您在上下文中维护对象的方式产生两个影响。

  1. 您不想在表单之间传递对象。相反,您将在表单之间传递 ID,并在上下文中加载实体。
  2. 当实体在一个上下文中更新时,您需要将更改通知其他窗口/上下文,以便他们可以刷新数据。

这是一篇 MSDN 文章,详细介绍了每个表单的上下文过程。本文使用 NHibernate,但 EF 和 DbCotext 的过程几乎相同

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

于 2012-11-10T21:35:25.177 回答