2016-04-26 6 views
1

は、私はこのような汎用的なインタフェースを定義していますTornLifestyleシンプルインジェクタ

public class ProviderPlanManagementRepo : IPlanManagmentRepository<Provider> 

すべてがこのようなインターフェイスを登録することで動作します:

public interface IPlanManagmentRepository<TEntity> where TEntity:class 

は、私はこのような具体的な実装を定義するとき

container.Register(typeof(IPlanManagmentRepository<>), 
    new [] { typeof(IPlanManagmentRepository<>).Assembly}, 
    Lifestyle.Scoped); 

ただし、このクラスaその後、私はこのエラーを取得する

public interface IProviderPlanManagementRepo 
{ 
    void doSomethingSpecificToProviderHere(); 
} 
public class ProviderPlanManagementRepo : IProviderPlanManagementRepo, 
    IPlanManagmentRepository<Provider> 
{ 
} 

:LSOハンドルより多くの原料と私は余分なインターフェイスを追加

-[Torn Lifestyle] The registration for IPlanManagmentRepository maps to the same implementation and lifestyle as the registration for IProviderPlanManagementRepo does. They both map to ProviderPlanManagementRepo

私もIProviderPlanManagementRepoでIPlanManagmentRepositoryを継承しようとしましたが、同じエラーを得ました。

このクラスは、汎用インターフェイスからの実装のみを処理する必要がありますか? これを簡単なインジェクタで実現することは可能ですか?

答えて

3

あなたの質問は、このwork itemthis discussionに関連しています。通常、破損したライフスタイルはas followsで修正できますが、Simple Injector 3.1では、複数の無関係なインターフェイスを持つタイプがバッチ登録を使用して登録されている場合、違反を修正することは非常に困難です。これは、次のマイナーリリースの1つで対処するものです。

私が今お勧めできる最も簡単な修正は、IPlanManagmentRepository<T>の登録を一時的にすることです。コンポーネントは一変しないため、一時的にすることができます。したがって、通常はDbContextのみがScopedである必要がありますが、DbContextは実行時データであり、実行時データはnot be injected into the components of your object graphであるため、DbContextをリポジトリに注入したくない場合もあります。

+0

ありがとう@Steven、私のDbContextがスコープであることを述べたように、私はあなたの提案を試み、DbContextがスコープであっても機能しましたが、上記の3つのリポジトリを使用している場合、上記(一時的なもの)は、3つすべてが同じDbContextを共有しますか?彼らはすべて同じトランザクションを共有する必要があるので、これは重要です。 –

+0

@ danif430:はい、同じインスタンスを共有します。やってみて。 – Steven

関連する問題