问题与不同类型的答案在NHibernate [英] Questions with different types of answer in NHibernate

查看:138
本文介绍了问题与不同类型的答案在NHibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为问卷问题找到一个整洁的解决方案。让我们说,我有一个问卷类,它有一个回答的集合,例如

I'm trying to find a tidy solution to a questionnaire problem. Let us say that I have a Questionnaire class which has a collection of Answers, e.g.

public class Questionnaire
{
    public virtual ISet<Answer> Answers {get;set;}
}

答案需要不同类型,具体取决于问题,例如

Answers need to be of different types depending on the question, e.g. date of birth, marks out of ten, why do you think etc.

我的第一个想法是这样的:

My first thought was something like this:

public class Question
{
    public virtual QuestionType TypeOfQuestion {get;set;}
    public virtual string PromptText {get;set;}
}

public class Answer
{
    public virtual Question Question {get;set;}
}

public class DateTimeAnswer : Answer
{
    public virtual DateTime Response {get;set;}
}        

public class IntegerAnswer : Answer
{
    public virtual int Response {get;set;}
}        
// etc.

明显的问题是,从问卷中,没有访问 Response 属性:

The obvious problem would be that from the questionnaire, there is no access to the Response property:

questionnaire.Answers[0].Response; // compile error

这同样适用于接口。使用通用接口将是更好的,例如:

The same would apply to an interface. It would be nicer to use a generic interface, such as:

public interface IAnswer<T> 
{
    public virtual Question Question {get;set;}
    public virtual T Response {get;set;}
}

public class DateTimeAnswer : IAnswer<DateTime> {}

问题出在问卷 class,因为必须提供IAnswer的类型:

The problem then comes in the Questionnaire class, as the type of IAnswer must be supplied:

public class Questionnaire
{
    public virtual ISet<IAnswer<???>> Answers {get;set;}
}

显然我不想有很多集合IAnswer每个具有不同类型。我可以使用

Clearly I don't want to have many collections of IAnswer each with different types. I could use

ISet<IAnswer<dynamic>> 

但是NHibernate不会喜欢。

but then NHibernate wouldn't like it.

我意识到在某个地方需要妥协,但我不确定哪个是最漂亮的。你会怎么办?

I realise a compromise is needed somewhere, but I'm not sure which is the prettiest. What would you do?

推荐答案

我认为子类化问题以及答案是一个好的计划 - 摆脱QuestionType枚举。

I think subclassing Question as well as answer is a good plan - get rid of that QuestionType enum.

您可以在Question上使用MakeAnswer(字符串)抽象方法封装很多逻辑,如果需要可以进行类型转换等。

You can then have a MakeAnswer(string) abstract method on Question which encapsulates a lot of logic for you, and can do type conversion etc if necessary.

我对 Rob的回答将是这样的:

public interface IAnswer
{
   Question Question { get; }
   void Respond(IAnswerFormatter formatter);
}

其中 IAnswerFormatter 像这样:

public interface IAnswerFormatter
{
   void Format(string value);
   void Format(DateTime value);
   ...
}

DateTimeAnswer如下所示: p>

And DateTimeAnswer would look like this:

public class DateTimeAnswer : IAnswer
{
    public DateTimeAnswer(Question question, DateTime value)
    {
        Question = question;
    }
    public Question Question { get; protected set; }

    protected DateTime Response {get; set;}

    public virtual void Respond(IAnswerFormatter formatter)
    {
        formatter.Write(Response);
    }
}

DateTimeQuestion可能如下所示:

And DateTimeQuestion might look like:

public class DateTimeQuestion : Question
{
    public IAnswer MakeAnswer(string value)
    {
        //  Ignoring error handling here
        return new DateTimeAnswer(this, DateTime.Parse(value));
    }
}

这样你的答案可以自己保存方式,你的NH映射可以指定它在数据库中的样子(我建议使用Discriminators映射)你的问题可以负责从泛型响应创建答案。

This way your answer can hold the value in their own way, and you NH mappings can specify how it looks in the database (I'd recommend mapping using Discriminators) your Question can be responsible for creating answers from generic responses.

这篇关于问题与不同类型的答案在NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆