2015-01-12 3 views
1

바인딩을 동적으로 추가하기 위해 Ninject.Extensions.Conventions를 사용하고 있습니다. 로드 할 .dll 이름은 구성에 저장됩니다. 구성이 잘못되어 .dll을로드 할 수 없다면 그 사실을 알면 좋습니다. 현재 .dll을로드하지 못하면 버블 링되지 않습니다. 예를 들어, 감자를 넣으려고하면 잡을 수있는 오류가 없습니다.Ninject 컨벤션 바인드 실패 여부 확인

foreach (var customModule in customModuleConfigs) 
{ 
    KeyValuePair<string, KVP> module = customModule; 

    _kernel.Bind(scanner => scanner 
     .From(module.Value.Value) 
     .SelectAllClasses().InheritedFrom<ICamModule>() 
     .BindAllInterfaces()); 

    // I need to know this failed 
    _kernel.Bind(scanner => scanner 
     .From("potato") 
     .SelectAllClasses().InheritedFrom<ICamModule>() 
     .BindAllInterfaces()); 
} 

잘못된 구성이 있음을 알 수있는 방법이 있습니까? IntelliTrace 창에서 던져진 예외가 표시되지만 거품이 생기기 전에 잡았습니다.

당신은 AllInterfacesBindingGenerator 클래스 래퍼를 생성하고, 생성 된 바인딩을 계산하려면이 옵션을 사용할 수
+1

필자는 지금까지 비교하기 전과 후에 바인딩 수를 계산하여 더 나은 방법을 찾고있었습니다. –

답변

1

어셈블리를 직접로드해야하며 예외가 throw되는지 여부를 제어 할 수 있습니다.

From(params Assembly[] assemblies) 과부하를 사용하십시오.

Assembly.LoadFrom() 또는 Assembly.Load을 사용하여 어셈블리를로드하십시오.

+0

고마워요,이게 내가 필요로했던 것입니다. 어떤 이유로 나는 Ninject 프레임 워크 외부에 어셈블리를로드하는 것에 대해 더 많은 소란이있을 것이라고 생각했지만, 이것은 매우 간단하고 더 쉬운 유지 관리를 위해 예외를 잡아서 기록 할 수 있습니다. –

1

:

public class CountingInterfaceBindingGenerator : IBindingGenerator 
{ 
    private readonly IBindingGenerator innerBindingGenerator; 

    public CountingInterfaceBindingGenerator() 
    { 
     this.innerBindingGenerator = 
      new AllInterfacesBindingGenerator(new BindableTypeSelector(), new SingleConfigurationBindingCreator()); 
    } 

    public int Count { get; private set; } 

    public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot) 
    { 
     this.Count++; 

     return this.innerBindingGenerator.CreateBindings(type, bindingRoot); 
    } 
} 

사용법 :

var kernel = new StandardKernel(); 
var bindingGenerator = new CountingInterfaceBindingGenerator(); 

kernel.Bind(b => 
{ 
    b.From("potato") 
     .SelectAllClasses() 
     .InheritedFrom<ICamModule>() 
     .BindWith(bindingGenerator); 
}); 

if (bindingGenerator.Count == 0) 
    // whatever 

이 더 이상 현재의 코드보다 아마이지만 것 생성 된 바인딩을 추가로 사용자 정의 할 수 있습니다.

+0

프랭크에게 감사드립니다. 이것은 내가 가지고있는 것보다 훨씬 더 우아한 접근법이며 그것을 사용할 것입니다. #BatteryBackupUnit에 대한 대답은 어셈블리로드 오류를 잡아서 기록 할 수 있기 때문입니다. 이는 어셈블리에서 발견 된 구현이없는 경우 로깅 외에도 찾고 있던 것입니다. –