具有随机 ID 的虚拟存储库数据外键

dummyrepository data foreign key with random id

我的项目中有两个虚拟存储库,一个用于问题,一个用于答案。这些问题是多项选择,所以他们可以有多个答案。我的问题模型:

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 作为问题的外键。 我应该如何 get/set 问题的答案才能将它们加载到我的视图中?

好的方法是使用Guid which will help you to merge user databases because Guid has 2^128 unique values. Use Guid.NewGuid生成新的唯一值

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

    public Guid UniqueIdentifier { get; set; }
}

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

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 });
    }
}