2

循環参照問題が発生しているプロジェクトがあります。外部キーを解決するための循環参照と循環参照

私は "質問"オブジェクトのリストを持つことができる "イベント"オブジェクトを持っています 私は親 "イベント"を得ることができる "質問"オブジェクトを持っています。

エンティティフレームワークを使用していた場合は、フレームワークによって管理されますが、何らかの理由で、クライアントからEntity Frameworkを使用しないという要件があります。だから、私はEFの動作をシミュレートしようとしました。なぜなら、彼らは最終的に彼らの感覚に来て、私にEFを使用させると確信しています。ここで私はどのように進めるのですか。

public class EventRepository : AbstractRepository<Event>, IEventRepository 
{ 
    private readonly IQuestionRepository questionRepository; 
    public EventRepository(IContext context, IQuestionRepository questionRepository) 
    { 
     this.questionRepository = questionRepository; 
    } 

    public Event GetNew(DataRow row) 
    { 
     return new GeEvent(this.questionRepository) { // Load the event from the datarow } 
    } 

    public class GeEvent : Event 
    { 
     private readonly IQuestionRepository questionRepository; 
     public GeEvent(IQuestionRepository questionRepository) 
     { 
      this.questionRepository = questionRepository; 
     } 
     public override List<Question> Questions { get { return this.questionRepository.GetByIdEvent(this.IdEvent); }} 
    } 
} 

public class QuestionRepository : AbstractRepository<Question>, IQuestionRepository 
{ 
    private readonly IEventRepository eventRepository; 
    public QuestionRepository(IContext context, IEventRepository eventRepository) 
    { 
     this.eventRepository = eventRepository; 
    } 

    public Question GetNew(DataRow row) 
    { 
     return new GeQuestion(this.eventRepository) { // Load the question from the datarow } 
    } 

    public class GeQuestion : Question 
    { 
     private readonly IEventRepository eventRepository; 
     public GeQuestion(IEventRepository eventRepository) 
     { 
      this.eventRepository = eventRepository; 
     } 
     public override Event Event { get { return this.eventRepository.Get(this.IdEvent); }} 
    } 
} 

私が見ることができるように、私は "鶏や卵"の場合があります。 EventRepositoryを作成するには、QuestionRepositoryが必要で、QuestionRepositoryを作成するにはEventRepositoryが必要です。リポジトリ(とサービス)を正しくテストすることができないDependencyResolverを直接使用する以外に、どうすれば依存関係を管理して、外部キー "a la" Entity Frameworkをロードできますか?

私はサンプルを単純に保つために外部キーの「遅延読み込み」を簡略化しました。

BTW2何かを助けることができるのであれば、私はオートファックを使用します。

おかげ

答えて

0

私は、あなたのデザインが間違ってやっていることを示唆してもいいですか?リポジトリに関しては、Separation of Concernsの原則に違反しているように見えます。

質問リポジトリの仕事は、親であるEventオブジェクトを提供するのではなく、ユーザーがイベントオブジェクトを取得するためにEventRepositoryに照会できるeventIdです。同様に、他のリポジトリの場合。そうすれば、あなたは依存関係を渡し周りに行く必要はありませんが、このようなとしてあなたの要求を構成することができます:あなたはhereを読むことができるよう

var question = _questionRepo.GetNew(row); 
var evnt = _eventRepo.Get(question.IdEvent); 

はまた、Autofacが正式に円形コンストラクタ\コンストラクタdepenciesをサポートしていません。

もう1つの解決方法は、依存関係の1つをプロパティセッターに変更して、ドキュメントに示すように処理することです。

+0

お返事ありがとうございます。私は知っている、デザインは理想よりも小さいです。私は通常、Entity Frameworkを使用しますが、クライアントは、彼のプログラマーはすでに多くのことを学んでいるので、クライアントは絶対に反対しています.EFはこれまでのステップにすぎません...まあ... EFは、あなたは知っています...クライアントは王様です。 私は、クライアントがその感覚に来て私にEFを使用できるようになるまで、プロパティセッターに行く必要があると思います。 もう一度お返事ありがとうございます。 –