将机制置于抽象类的多个实现之间

时间:2014-10-08 08:03:01

标签: c# wpf inheritance polymorphism decoupling

我在.NET 4.5 WPF应用程序中工作我有3个视图正在使用带有问题的容器。 2个视图有一组问题,我现在正在努力添加另一个有两个问题容器的视图,因为它同时适用于两个客户。

班级结构:

QuestionItem是放置在viewModel中的任何问题。

public abstract class QuestionItemViewModel : ValidatingScreen, IHandle<ValidatingEventArgs>,
    IEquatable<QuestionItemViewModel>{
public event QuestionAnsweredEventHandler AnsweredHandler;
public abstract string GetQuestionAnswer();
public abstract bool HasAnswer { get; }
public virtual void OnAnswered(EventArgs e)
    {
        if (this.AnsweredHandler != null)
            this.AnsweredHandler(this, e);
    }
}

然后是4种类型的问题,我将展示1以节省空间,但它们看起来都是一样的。

放入viewModel的问题的具体实现(这是下拉列表)

public class QuestionDropDownViewModel : QuestionItemViewModel, IEquatable<QuestionDropDownViewModel>
{
public string SelectedAnswer
    {
        get
        {
            if (this._selectedAnswer.IsNullOrEmpty())
                this._selectedAnswer = this.QuestionAnswerList.FirstOrDefault();

            return this._selectedAnswer;
        }
        set
        {
            if (this._selectedAnswer != value)
            {
                this._selectedAnswer = value;

                if (this._isInitialised)
                {
                    this.IsDirty = true;
                    this.NotifyOfPropertyChange(() => this.SelectedAnswer);
                    this._eventAggregator.Publish(new QuotePropertyChangedEvent(true));
                    this.OnAnswered(EventArgs.Empty);
                }
            }
        }
    }

    public override string GetQuestionAnswer()
    {
        return this.SelectedAnswer;
    }

    public override bool HasAnswer
    {
        get { return !string.IsNullOrWhiteSpace(this.SelectedAnswer) && this.SelectedAnswer != "please select an item"; }
    }

在两个包含一组问题QuestionItemViewModel的视图中{p}放在this.QuestionItems = new CustomerQuestionCollection(){CustomerId = this.CurrentTransactionDetail.TransactionMain.CustomerID};中 由于我正在处理的视图必须有2个集合,因此还有其他集合this.QuestionItemsForSecondCustomer = new CustomerQuestionCollection() {CustomerId = this.CurrentTransactionDetail.TransactionWill.SecondCustomerId};

不是一次性询问所有问题,而是在问题得到解答时{view = 1}}在viewModel上被触发,以根据之前的答案查找下一个问题。

dropdownQuestion.AnsweredHandler += Quesion_AnsweredHandler;

问题:开始填写第二个容器我正在检查第一个容器的最后一个问题是否已被回答,是否是我切换到第二个容器

protected override void Quesion_AnsweredHandler(object sender, EventArgs e)
    {
        var questionViewModel = sender as QuestionItemViewModel;
        if (questionViewModel != null)
        {
            this.EvaluateAndAddNextQuestion(questionViewModel);
            this.NotifyParent();
        }
    }

然而,在我开始填充private void EvaluateAndAddNextQuestion(QuestionItemViewModel questionViewModel) { var collectionToBeFilled = this.IsWifForSecondCustomerVisible ? this.QuestionItemsForSecondCustomer : this.QuestionItems; this.ClearNextQuestions(collectionToBeFilled, questionViewModel); if (!(questionViewModel is QuestionDropDownViewModel) || (questionViewModel.HasAnswer)) { this.AddNextQuestion(collectionToBeFilled, questionViewModel); } NotifyOfPropertyChange(() => IsVisibleQuestionItemsForSecondCustomer); NotifyOfPropertyChange(() => QuestionItems); NotifyOfPropertyChange(() => QuestionItemsForSecondCustomer); } 之后,如果第一个容器(QuestionItemsForSecondCustomer)被修改,我无法判断它是第一个或第二个容器中的项目是否被QuestionItems。这需要我在容器和容器内的容器之间引入一些耦合,我眼中最宽松的是将Quesion_AnsweredHandler引入CustomerId,因为我已经在容器中QuestionItemViewModel并且可以识别它是否是问题来自CustomerIdthis.QuestionItems

问题:有没有办法引入this.QuestionItemsForSecondCustomer之外的其他课程,例如QuestionItemViewModel(或其他一些机制)可能会CustomerQuestionItemViewModel介于4种类型的问题(CustomerIdQuestionDropDownViewModel,..)中,不需要我复制这4个类,例如QuestionRadioGroupViewModelQuestionDropDownViewModel,但仍允许我对他们有CustomerQuestionDropDownViewModel

立即解决方案是将CustomerId添加到CustomerId,但它会泄漏不属于基类的子机制的实现,我想避免。如果某些部分不清楚,问题很复杂请告诉我,我会尽力添加缺失的细节。

0 个答案:

没有答案