我打开一个新的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 层中执行此操作?或者我应该重新设计我的解决方案并在不同的地方执行上下文逻辑?