我有以下模型(简化):
数据库模型:
diff -u <(cat * | sort) <(cat * | sort | uniq)
查看模型:
public abstract class Question
{
public int Id { get; set; }
public string Text { get; set; }
}
public class QuestionA : Question
{
public bool OptionA { get; set; }
}
public class QuestionB : Question
{
public int OptionB { get; set; }
}
基本上是将数据库实体映射到视图模型,例如,我有这个控制器:
public abstract class QuestionModel
{
public string Text { get; set; }
protected QuestionModel(Question question)
{
Text = question.Text;
}
}
public class QuestionAModel : QuestionModel
{
public bool OptionA { get; set; }
public QuestionAModel(QuestionA questionA) : base(questionA)
{
OptionA = questionA.OptionA;
}
}
public class QuestionBModel : QuestionModel
{
public int OptionB { get; set; }
public QuestionBModel(QuestionB questionB) : base(questionB)
{
OptionB = questionB.OptionB;
}
}
这很难编译,因为public class SomeController : Controller
{
public JsonResult Get()
{
// Simulating questions from the database
var questions = new List<Question>
{
new QuestionA { Id = 1, OptionA = true, Text = "fooA" },
new QuestionB { Id = 1, OptionB = 1, Text = "fooB" }
};
var model = questions.Select(q => new QuestionModel(q)).ToList();
return Json(model);
}
}
我知道数据库中每个问题的类型,我如何创建每个问题实体的派生问题模型?
感谢所有!!
答案 0 :(得分:4)
不是调用构造函数,而是创建一个factory class,它将根据您必须创建的对象的类型提供正确的实现。
请注意,在调用工厂之前,您必须从数据库中实现对象:
var model = questions.ToList().Select(q => QuestionFactory.CreateQuestion(q));
答案 1 :(得分:-1)
在尝试找到问题的替代解决方案时,我遇到了一个问题,希望有人能为我解答。
以下代码工作正常,我声明了一个QuestionA对象并创建了一个所述类型(A)的QuestionModel,我最后将其添加到通用QuestionModel类型的列表中。
QuestionA qA = new QuestionA { Id = 1, OptionA = true, Text = "fooA" };
List<QuestionModel> lst = new List<QuestionModel>();
lst.Add(new QuestionAModel(qA));
但是,据说这段代码遵循相同的逻辑,但我无法编译它
List<QuestionModel> model = new List<QuestionModel>();
questions.ForEach(q => model.Add(q.GetType().Equals(typeof(QuestionA)) ?
new QuestionAModel((QuestionA)q) : new QuestionBModel((QuestionB)q)));
我为偏离OP的问题而道歉
编辑:因此,从技术上讲,以下代码将使您能够创建通用QuestionModel列表而无需修改类
var questions = new List<Question>
{
new QuestionA { Id = 1, OptionA = true, Text = "fooA" },
new QuestionB { Id = 1, OptionB = 1, Text = "fooB" }
};
List<QuestionModel> model = new List<QuestionModel>();
questions.ForEach(q => model.Add(q.GetType().Equals(typeof(QuestionA)) ?
(QuestionModel)(new QuestionAModel((QuestionA)q)) : new QuestionBModel((QuestionB)q)));