具有随机id的dummyrepository数据外键

本文关键字:数据 dummyrepository 随机 id | 更新日期: 2023-09-27 18:28:41

在我的项目中有两个dummyrepository,一个用于提问,另一个用于回答。这些问题有多种选择,所以他们可以有多种答案。我的问题模型:

public class Question : BaseClass
{
    public Question() : base()
    {
    }
    public int QuestionId { get; set; }
    public string Value { get; set; }
    public virtual List<Answer> Answers { get; set; }
}

答案属于问题

public class Answer : BaseClass
{
    public Answer() : base()
    {
    }
    public int AnswerId { get; set; }
    public string Value { get; set; }
    public int QuestionId { get; set; }
    public virtual Question Question { get; set; } 
}

它们都扩展了BaseClass,后者具有一些自定义字段。

public abstract class BaseClass
{
    protected BaseClass()
    {
        UniqueIdentifier = RandomIdentifier(20);
    }
    public string UniqueIdentifier { get; set; }
    private static string RandomIdentifier(int length)
    {
     //returns an unique identifier 
    }
}

我的dummyQuestionRepository看起来像:

public class DummyQuestionRepository : IQuestionRepository
{
    private  List<Question> _questions;
    public DummyQuestionRepository()
    {
        _questions = new List<Question>();
        _questions.Add(new Question { Value = "Favourit food?" });
        _questions.Add(new Question { Value = "Who is the president?" });
        _questions.Add(new Question { Value = "Favourit movie?" });
    }
    public List<Question> GetAll()
    {
        return _questions;
    }
    public void Create(Question q)
    {
        _questions.Add(q);
    }
   //removed the non relevant functions
}

我的dummyAnswerRepository

class DummyAnswerRepository
{
    private  List<Answer> _answers;
    public DummyAnswerRepository()
    {
        _answers = new List<Answer>();
        _answers.Add(new Answer { Value = "pizza" });
        _answers.Add(new Answer { Value = "fries" });
        _answers.Add(new Answer { Value = "Bush" });
        _answers.Add(new Answer { Value = "Obama" });
        _answers.Add(new Answer { Value = "titanic" });
        _answers.Add(new Answer { Value = "lion king" });
    }
   public List<Answer> GetAll()
    {
        return _answers;
    }
    public void Create(Answer a)
    {
        _answers.Add(a);
    }
}

正如您可能注意到的那样,基类有一个UniqueIdentifier变量。此变量用于在联机数据库中创建一个唯一值(不能使用id,因为用户在脱机工作时可以创建相同的id),答案应将UniqueIdentifier作为问题的外键。我应该如何获得/设置问题的答案,以便将其加载到我的视图中?

具有随机id的dummyrepository数据外键

好方法是使用Guid,这将帮助您合并用户数据库,因为Guid有2^128个唯一值。使用Guid.NewGuid生成新的唯一值

public abstract class BaseClass
{
    protected BaseClass()
    {
        UniqueIdentifier = Guid.NewGuid();
    }
    public Guid UniqueIdentifier { get; set; }
}

若要添加外键,您可以实现类似于实体框架种子方法的方法,当答案和问题将在一个地方创建,然后添加到存储库中时。只需删除从存储库构造函数中创建新实体的代码,并使用如下代码:

public class DataBaseInitializer
{
    public void Seed(IQuestionRepository questionRepository, DummyAnswerRepository answerRepository)
    {
        var q1 = new Question { Value = "Favourit food?" };
        var q2 = new Question { Value = "Who is the president?" });
        var q3 = new Question { Value = "Favourit movie?" });
        questionRepository.Create(q1);
        questionRepository.Create(q2);
        questionRepository.Create(q3);
        answerRepository.Create(new Answer { Value = "pizza", Question = q1 });
        answerRepository.Create(new Answer { Value = "fries", Question = q1 });
        answerRepository.Create(new Answer { Value = "Bush", Question = q2 });
        answerRepository.Create(new Answer { Value = "Obama", Question = q2 });
        answerRepository.Create(new Answer { Value = "titanic", Question = q3 });
        answerRepository.Create(new Answer { Value = "lion king", Question = q3 });
    }
}