1

간단한 인젝터를 사용하여 클래스 생성자에 매개 변수를 동적으로 전달하는 데 문제가 있습니다.Simple Injector를 사용하여 생성자에서 인수 전달

다음과 같은 코드 구조가 있습니다.

컨트롤러 예 :.?

public class HomeController : Controller 
{ 
    private readonly ICheckService _checkService; 

    public HomeController(ICheckService checkService) 
    { 
     _checkService= checkService; 
    } 

    // GET: Home 
    public ActionResult Index() 
    { 
     var list = _checkService.GetAll(); 
     return View(list); 
    } 
} 

서비스 층 (나는 ICheckRepository<T>을 구현하고 CheckRepository<T>에 대한 두 개의 생성자 매개 변수를 전달하기 위해 필요한이 계층에서 어떻게 내가 시도했지만 해결책을 얻지 못하고이 사용하는 간단한 인젝터를 달성 할 주변의 한 예를) 정말 감사하겠습니다

public interface ICheckService 
{ 
     List<CheckType> GetAll(); 
} 

public class CheckService : ICheckService 
{ 
    private readonly ICheckRepository<CheckType> _checkRepository; 

    public CheckService(ICheckRepository<CheckType> checkRepository) 
    { 
     _checkRepository= checkRepository; 
    } 

    public List<T> GetAll() 
    { 
     return _checkRepository.GetAll().ToList(); 
    } 
} 

저장소 계층 달성하기 위하여 :.

내 간단한 인젝터 초기화 클래스 :

public static void InitializeInjector() 
{ 
    var container = new Container(); 

    InitializeContainer(container); 

    container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); 
    container.RegisterMvcIntegratedFilterProvider(); 
    container.Verify(); 

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); 
} 

private static void InitializeContainer(Container container) 
{ 
    container.Register(typeof(IFilterRepository<>), typeof(FilterRepository<>)); 
    //Here is where I am struggling to bind dynamic constructor parameter registering 

} 

사람이 위의 코드에 대한 모든 솔루션이 있습니까?

다시 한번 감사드립니다. 매개 변수가 특정 고정되는 경우

+0

두 매개 변수에 어떤 값을 넣으시겠습니까? 모든 저장소는 고유 한 연결 문자열 또는 저장 프로 시저를 갖습니까? 아니면 이러한 구성 상수이며 모든 저장소와 동일합니까? 컨테이너를 사용하지 않고 이러한 리포지토리를 만드는 방법에 대한 예제로 질문을 업데이트 할 수 있습니까? – Steven

+0

그것의 문자열과 정확히 내가 뭘 넣어 상관 없어요.하지만 이러한 문자열 매개 변수는 다른 저장소와 다를 수 있습니다. 나는 당신의 마지막 질문을 얻지 못했다. 위 코드는 내가 붙어있는 지점까지 설명했다. –

+0

그것은 당신이 찾고있는 대답이 아닙니다. Simple Injector에 등록 할 방법을 찾고 있습니다. 그러나 우리가 도울 수 있도록 Simple Injector가없는 경우에는 해당 저장소를 직접 손으로 새롭게했을 것입니다. '새로운 CheckService (새로운 FilterRepository (whatgoedhere?))'와 같은 것입니다. 생성하고자하는 다른 리포지토리의 예를 보여주십시오. 이것은 우리에게 당신이 성취하고자하는 것에 대한 지식을 주며, 당신의 질문에 대한 정답을 공식화 할 수있게 해줍니다. – Steven

답변

2

폐쇄 제네릭 다음과 같이 유형, 당신은 등록을해야한다 :

저장소가 구성 값으로 종속성을 혼합 경우
c.Register<ICheckRepo<Customer>>(() => new CheckRepository<Customer>(constr, "cust_sp")); 
c.Register<ICheckRepo<Order>>(() => new CheckRepository<Order>(constr, "order_sp")); 
c.Register<ICheckRepo<Product>>(() => new CheckRepository<Product>(constr, "prod_sp")); 
// more registrations here 

, 당신은 또한 혼합 상황에 맞는 등록을 사용할 수 있습니다 오픈 제네릭 형식의 등록을 : here 설명으로

// Registrations 
// One registration for the open generic type 
c.Register(typeof(ICheckRepository<>), typeof(CheckRepository<>)); 

// One registration for the connection string (assuming you only have one) 
container.RegisterConditional(typeof(string), CreateStringConstant(constr), 
    c => c.Consumer.Target.Name == "connectionString"); 

// Conditional registrations for each closed ICheckRepository<T> 
RegisterStoredProcForCheckRepository<Customer>("cuts_sp"); 
RegisterStoredProcForCheckRepository<Order>("order_sp"); 
RegisterStoredProcForCheckRepository<Product>("prod_sp"); 
// more registrations here 

// Helper methods 
Registration CreateStringConstant(string value) => 
    Lifestyle.Singleton.CreateRegistration(typeof(string),() => value, container); 

void RegisterStoredProcForCheckRepository<TEntity>(string spName) { 
    container.RegisterConditional(typeof(string), CreateStringConstant(container, spName), 
     c => c.Consumer.Target.Name == "segment" 
      && c.Contumer.ImplementationType == typeof(CheckRepository<TEntity>)); 
} 

연결 문자열 또는 저장 프로 시저 요청에 따라 다릅니다 경우, 당신은 디자인을 변경해야합니다.

관련 문제