2017-02-22 1 views
0

ASP.NET 코어 1.0에서는 BaseRepository constructor 두 매개 변수를 삽입하려고합니다. 하나는 SwitchContext (아래에 등록하고 사용자 정의 매개 변수 - RepositoryCacheMode (그냥 enum))ASP.NET 핵심 종속성 주입 (선택 매개 변수 사용)

나는 이런 식으로 매개 변수를 등록하는 방법이 있습니까?

services.AddTransient<RepositoryCacheMode, RepositoryCacheMode.None>(); 

또는 방법 BaseRepository의 reginstration에 대한 SwitchContext의 인스턴스를 얻는 방법? BaseRepository의

public static void AddDependency(this IServiceCollection services) 
     {     
      services.AddTransient<SwitchContext, SwitchContext>(); 
      services.AddTransient<IRepository, BaseRepository>(t => new BaseRepository(// How to get instanse of SwitchContext ? // , RepositoryCacheMode.None)); 

생성자 :

public BaseRepository(SwitchContext context, RepositoryCacheMode cacheMode = RepositoryCacheMode.FirstLevel) 
     { 
      Context = context; 
      _cacheMode = cacheMode; 
     } 
+0

내가 중복으로 당신을 신고 됨. 유일한 차이점은 기본값을 사용한다는 것이며, 기본값 인 null을 원한다는 것입니다. 두 가지 방법 모두 작동하지 않으며 그 이유가 설명됩니다. –

답변

1

tServiceProvider입니다 그래서 당신은 다만 할 수 있습니다 거의 똑같은 질문처럼 보이기 때문에

services.AddTransient<IRepository, BaseRepository>(
    serviceProvider => new BaseRepository(serviceProvider.GetService<SwitchContext>()) 
); 
+1

글쎄, 그것은 나를 위해 작동하지만 해결 방법을 찾을 수 없었고 (r => r.GetService ) –

+0

@ Ilya.Sulimanov 아 죄송합니다. 이전 유니티 컨테이너에 익숙하지 않았습니다. 나는 대답을 바꿨다. –

관련 문제