private void InitializeSubjects(IEnumerable<AssociatedSubject> subjects)
        {
            TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();
            Task.Factory.StartNew(
                () =>
                {
                    var domainSubjects = _subjectQuery.All.Select(s => s).ToList();
                    List<AssociatedSubjectViewModel> viewModels = new List<AssociatedSubjectViewModel>();

                    foreach (var subject in subjects)
	                {
                        var domainSubject = domainSubjects.Where(s => s.Id == subject.SubjectId).FirstOrDefault();

                        if (domainSubject != null)
                        {
                            var viewModel = new AssociatedSubjectViewModel(
                                    subject, _subjectValidator, domainSubject, _busId
                                );

                            AttachEvents(viewModel);

                            viewModels.Add(
                                viewModel
                            );
                        }
	                }

                    return viewModels;
                }
            )
            .ContinueWith(
                task =>
                {
                    AssociatedSubjects = new ObservableCollection<AssociatedSubjectViewModel>(task.Result);
                    InitializeAvailableSubjects();
                },
                scheduler
            );
        }
        private void RemoveAssociatedSubject(AssociatedSubjectViewModel viewModel)
        {
            DettachEvents(viewModel);
            _associatedSubjects.Remove(viewModel);

            AddAvailableSubject(viewModel.AssociatedSubject.SubjectId);

            RaiseSubjectRemoved(viewModel.AssociatedSubject);
            NotifyDirtyHasChanged();
        }
 private void DettachEvents(AssociatedSubjectViewModel viewModel)
 {
     viewModel.IsDirtyChangedEvent -= IsDirtyChangedHandler;
     viewModel.RequestRemovalEvent -= RequestRemovalHandler;
 }
        private void AddAssociatedSubject(Subject subject)
        {
            var viewModel =
                new AssociatedSubjectViewModel(
                    new AssociatedSubject() { SubjectId = subject.Id, Points = 0 },
                    _subjectValidator, 
                    subject, 
                    _busId
                );

            RemoveAvailableSubject(subject);

            AttachEvents(viewModel);
            _associatedSubjects.Add(viewModel);

            RaiseSubjectAdded(viewModel.AssociatedSubject);

            NotifyDirtyHasChanged();
        }